From d9b5dfe5d60d1fa3d4c44d25c87e479ffc7f06b9 Mon Sep 17 00:00:00 2001 From: Luca Date: Sat, 11 May 2024 20:56:28 +0200 Subject: [PATCH] feat(importer): add setting to ignore calendar events before some date --- .../importer/dynamic_preferences_registry.py | 14 ++++++++++++++ shiftregister/importer/importer.py | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 shiftregister/importer/dynamic_preferences_registry.py diff --git a/shiftregister/importer/dynamic_preferences_registry.py b/shiftregister/importer/dynamic_preferences_registry.py new file mode 100644 index 0000000..dcf9820 --- /dev/null +++ b/shiftregister/importer/dynamic_preferences_registry.py @@ -0,0 +1,14 @@ +import datetime + +from dynamic_preferences import types +from dynamic_preferences.registries import global_preferences_registry + +_import = types.Section("import") + + +@global_preferences_registry.register +class NotBefore(types.DatePreference): + section = _import + name = "not_before" + default = datetime.date.today() + help_text = "Do not import shifts ending before this date" diff --git a/shiftregister/importer/importer.py b/shiftregister/importer/importer.py index 9451f6c..94d663a 100644 --- a/shiftregister/importer/importer.py +++ b/shiftregister/importer/importer.py @@ -3,10 +3,13 @@ from datetime import timezone import requests from django.conf import settings from django.db import transaction +from dynamic_preferences.registries import global_preferences_registry from icalendar import Calendar from .models import Event, Room, Shift +global_preferences = global_preferences_registry.manager() + def import_calendar(calendar): try: @@ -20,6 +23,8 @@ def import_calendar(calendar): if not r.headers["content-type"].startswith("text/calendar"): return False + not_before = global_preferences["import__not_before"] + try: cal = Calendar.from_ical(r.text) @@ -32,8 +37,10 @@ def import_calendar(calendar): start = event.decoded("dtstart").astimezone(timezone.utc) end = event.decoded("dtend").astimezone(timezone.utc) location = event.decoded("location", None) - if not summary: + + if not summary or end.date() < not_before: continue + if location is not None: location = location.decode().strip()