On n'importe que les événements qui ont lieu aujourd'hui ou les jours futurs

This commit is contained in:
Jean-Marie Favreau 2023-12-30 14:07:52 +01:00
parent a48bbdc14e
commit bfbf2cf58d

View File

@ -1,7 +1,7 @@
from agenda_culturel.models import Event from agenda_culturel.models import Event
import json import json
from datetime import datetime from datetime import datetime
from django.utils import timezone
class EventsImporter: class EventsImporter:
@ -9,6 +9,7 @@ class EventsImporter:
self.celery_id = celery_id self.celery_id = celery_id
self.error_message = "" self.error_message = ""
self.init_result_properties() self.init_result_properties()
self.today = timezone.now().date().isoformat()
def init_result_properties(self): def init_result_properties(self):
self.event_objects = [] self.event_objects = []
@ -53,17 +54,33 @@ class EventsImporter:
# get events # get events
for event in structure["events"]: for event in structure["events"]:
# only process events if they are today or the days after
if self.event_takes_place_today_or_after(event):
# set a default "last modified date"
if "last_modified" not in event and self.date is not None: if "last_modified" not in event and self.date is not None:
event["last_modified"] = self.date event["last_modified"] = self.date
if not self.import_event(event): # conversion to Event, and return an error if it failed
if not self.load_event(event):
return (False, self.error_message) return (False, self.error_message)
# import them # finally save the loaded events in database
self.save_imported() self.save_imported()
return (True, "") return (True, "")
def event_takes_place_today_or_after(self, event):
if "start_day" not in event:
return False
if event["start_day"] >= self.today:
return True
if "end_day" not in event:
return False
return event["end_day"] >= self.today
def save_imported(self): def save_imported(self):
self.db_event_objects, self.nb_updated, self.nb_removed = Event.import_events(self.event_objects, remove_missing=True) self.db_event_objects, self.nb_updated, self.nb_removed = Event.import_events(self.event_objects, remove_missing=True)
@ -78,7 +95,7 @@ class EventsImporter:
return True return True
def import_event(self, event): def load_event(self, event):
if self.is_valid_event_structure(event): if self.is_valid_event_structure(event):
event_obj = Event.from_structure(event) event_obj = Event.from_structure(event)
self.event_objects.append(event_obj) self.event_objects.append(event_obj)