feat(importer): add setting to ignore calendar events before some date
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
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
|
import requests
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from dynamic_preferences.registries import global_preferences_registry
|
||||||
from icalendar import Calendar
|
from icalendar import Calendar
|
||||||
|
|
||||||
from .models import Event, Room, Shift
|
from .models import Event, Room, Shift
|
||||||
|
|
||||||
|
global_preferences = global_preferences_registry.manager()
|
||||||
|
|
||||||
|
|
||||||
def import_calendar(calendar):
|
def import_calendar(calendar):
|
||||||
try:
|
try:
|
||||||
|
@ -20,6 +23,8 @@ def import_calendar(calendar):
|
||||||
if not r.headers["content-type"].startswith("text/calendar"):
|
if not r.headers["content-type"].startswith("text/calendar"):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
not_before = global_preferences["import__not_before"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cal = Calendar.from_ical(r.text)
|
cal = Calendar.from_ical(r.text)
|
||||||
|
|
||||||
|
@ -32,8 +37,10 @@ def import_calendar(calendar):
|
||||||
start = event.decoded("dtstart").astimezone(timezone.utc)
|
start = event.decoded("dtstart").astimezone(timezone.utc)
|
||||||
end = event.decoded("dtend").astimezone(timezone.utc)
|
end = event.decoded("dtend").astimezone(timezone.utc)
|
||||||
location = event.decoded("location", None)
|
location = event.decoded("location", None)
|
||||||
if not summary:
|
|
||||||
|
if not summary or end.date() < not_before:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if location is not None:
|
if location is not None:
|
||||||
location = location.decode().strip()
|
location = location.decode().strip()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue