diff --git a/src/agenda_culturel/importation.py b/src/agenda_culturel/importation.py index c35994a..9678de9 100644 --- a/src/agenda_culturel/importation.py +++ b/src/agenda_culturel/importation.py @@ -1,7 +1,7 @@ from agenda_culturel.models import Event import json from datetime import datetime - +from django.utils import timezone class EventsImporter: @@ -9,6 +9,7 @@ class EventsImporter: self.celery_id = celery_id self.error_message = "" self.init_result_properties() + self.today = timezone.now().date().isoformat() def init_result_properties(self): self.event_objects = [] @@ -53,17 +54,33 @@ class EventsImporter: # get events for event in structure["events"]: - if "last_modified" not in event and self.date is not None: - event["last_modified"] = self.date + # 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: + event["last_modified"] = self.date - if not self.import_event(event): - return (False, self.error_message) + # conversion to Event, and return an error if it failed + if not self.load_event(event): + return (False, self.error_message) - # import them + # finally save the loaded events in database self.save_imported() 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): 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 - def import_event(self, event): + def load_event(self, event): if self.is_valid_event_structure(event): event_obj = Event.from_structure(event) self.event_objects.append(event_obj)