From 47a8740011683a5f6198a88a47d474c625b429be Mon Sep 17 00:00:00 2001
From: Andreas Hubel <andi@saerdnaer.de>
Date: Wed, 25 Dec 2024 14:58:17 +0100
Subject: [PATCH] feat(scheduleimport): add legacy_id=drop option, so id can be
 recomputed from uuid

---
 src/core/schedules/base.py         | 4 ++--
 src/core/schedules/schedulejson.py | 7 ++++++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/core/schedules/base.py b/src/core/schedules/base.py
index 7735ba89e..36e763e69 100644
--- a/src/core/schedules/base.py
+++ b/src/core/schedules/base.py
@@ -7,10 +7,10 @@ from django.utils import timezone
 from django.utils.module_loading import import_string
 
 
-def filter_additional_data(data: dict, computed_fields: dict | None = None) -> dict:
+def filter_additional_data(data: dict, computed_fields: dict | None = None, others=[]) -> dict:
     computed_fields = computed_fields or {}
     return {
-        k: v for k, v in data.items() if (v and k not in ['guid', 'room', 'start', 'date', 'duration', 'title', 'abstract', 'description', 'language'])
+        k: v for k, v in data.items() if (v and k not in ['guid', 'room', 'start', 'date', 'duration', 'title', 'abstract', 'description', 'language', *others])
     } | computed_fields
 
 
diff --git a/src/core/schedules/schedulejson.py b/src/core/schedules/schedulejson.py
index 32a603c5d..a4ecf6020 100644
--- a/src/core/schedules/schedulejson.py
+++ b/src/core/schedules/schedulejson.py
@@ -22,6 +22,7 @@ class ScheduleJSONSupport(BaseScheduleSupport):
         'kind':     ('string', 'assembly', False, 'kind of events, either `assembly` or `official` or `sos` or `lightning`'),
         'headers':  ('dict',   {},         False, 'HTTP headers to send with the request e.g. Authorization'),
         'auth':     ('string', None,       False, 'HTTP Authentification header e.g. `Token 123456`'),
+        'legacy_id': ('string', None,      False, 'Use `drop` to regenerate legacy ids from the event guid, to avoid collisions with other sources'),
         'legacy_id_offset': ('int', None,  False, 'Offset to add to the legacy integer `id` to avoid collisions with other sources'),
     }
     # fmt: on
@@ -57,6 +58,10 @@ class ScheduleJSONSupport(BaseScheduleSupport):
 
         kind = self.conf_value('kind')
 
+        other_fields_to_drop = []
+        if self.conf_value('legacy_id') == 'drop':
+            other_fields_to_drop.append('id')
+
         def ensure_full_url(uri):
             if not uri:
                 return None
@@ -85,7 +90,7 @@ class ScheduleJSONSupport(BaseScheduleSupport):
                     'kind': kind,
                     'speakers': e.get('persons', []),
                     'banner_image_url': ensure_full_url(e.get('logo')),
-                    'additional_data': filter_additional_data(e, self.computed_data(e)),
+                    'additional_data': filter_additional_data(e, self.computed_data(e), other_fields_to_drop),
                 }
                 for e in schedule.events()
             },
-- 
GitLab