feat(importer): add setting to ignore calendar events before some date

main
Luca
parent e8cd15bf67
commit d9b5dfe5d6

@ -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"

@ -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()

Loading…
Cancel
Save