From f9984f92acf058d5c75c74a152d138082fdcdcb4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Thu, 12 May 2022 19:32:18 +0200
Subject: [PATCH] Add a utility script to create json exports for individual
 events

This consists of a bash script to call and prepare as well as a python script to process the dump created by django and restrict it to the entries related to the event
---
 Utils/json_export.py | 57 ++++++++++++++++++++++++++++++++++++++++++++
 Utils/json_export.sh | 24 +++++++++++++++++++
 2 files changed, 81 insertions(+)
 create mode 100644 Utils/json_export.py
 create mode 100644 Utils/json_export.sh

diff --git a/Utils/json_export.py b/Utils/json_export.py
new file mode 100644
index 00000000..a3a8af71
--- /dev/null
+++ b/Utils/json_export.py
@@ -0,0 +1,57 @@
+import json
+import sys
+
+event_id = int(sys.argv[1])
+target_name = sys.argv[2]
+
+print(f"Creating export for event '{event_id}' as '{target_name}'")
+
+# Load json file just created by django
+with open('backups/akplanning_only.json', 'r') as json_file:
+    exported_entries = json.load(json_file)
+print(f"Loaded {len(exported_entries)} entries in total, restricting to event...")
+
+entries_without_event = 0
+entries_out = []
+virtual_rooms_to_preserve = set()
+
+# Loop over all dumped entries
+for entry in exported_entries:
+    # Handle all entries with event reference
+    if "event" in entry['fields']:
+        event = int(entry['fields']['event'])
+
+        # Does this entry belong to the event we are looking for?
+        if event == event_id:
+            # Store for backup
+            entries_out.append(entry)
+
+            # Remember the primary keys of all rooms of this event
+            # Required for special handling of virtual rooms,
+            # since they inherit from normal rooms and have no direct event reference
+            if entry['model'] == "AKModel.room":
+                virtual_rooms_to_preserve.add(entry['pk'])
+    # Handle entries without event reference
+    else:
+        # Backup virtual rooms of that event
+        if entry['model'] == "AKOnline.virtualroom":
+            if entry['pk'] in virtual_rooms_to_preserve:
+                entries_out.append(entry)
+        # Backup the event itself
+        elif entry['model'] == "AKModel.event":
+            if int(entry['pk']) == event_id:
+                entries_out.append(entry)
+        # Backup tags
+        elif entry['model'] == "AKModel.aktag":
+            # No restriction needed, backup all tags
+            entries_out.append(entry)
+        else:
+            # This should normally not happen (all other models should have a reference to the event)
+            entries_without_event += 1
+            print(entry)
+
+print(f"Ignored entries without event: {entries_without_event}")
+print(f"Exporting {len(entries_out)} entries for event")
+
+with open(f'backups/{target_name}.json', 'w') as json_file:
+    json.dump(entries_out, json_file, indent=2)
diff --git a/Utils/json_export.sh b/Utils/json_export.sh
new file mode 100644
index 00000000..d2ca618a
--- /dev/null
+++ b/Utils/json_export.sh
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+# Update AKPlanning
+# execute as Utils/update.sh id_to_export target_name_to_export_to [--prod]
+
+# abort on error, print executed commands
+set -ex
+
+# activate virtualenv if necessary
+if [ -z ${VIRTUAL_ENV+x} ]; then
+    source venv/bin/activate
+fi
+
+# set environment variable when we want to update in production
+if [ "$3" = "--prod" ]; then
+    export DJANGO_SETTINGS_MODULE=AKPlanning.settings_production
+fi
+
+# before potentially breaking anything, create a data backup
+mkdir -p ../backups/
+python manage.py dumpdata AKDashboard AKModel AKOnline AKPlan AKScheduling AKSubmission --indent=2 > "backups/akplanning_only.json" --traceback
+
+python ./Utils/json_export.py $1 $2
+
+rm backups/akplanning_only.json
-- 
GitLab