From 7d48d74ba3fe307169b59317bdd0e15121657230 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sun, 31 Dec 2023 18:43:57 +0100 Subject: [PATCH] =?UTF-8?q?On=20ne=20met=20=C3=A0=20jour=20que=20si=20la?= =?UTF-8?q?=20date=20de=20l'import=20est=20plus=20r=C3=A9cente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/models.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 4a4b5d7..444c9a7 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -15,7 +15,10 @@ from django.db.models import Q from django.template.defaultfilters import date as _date -from datetime import datetime, time, timedelta +from datetime import time, timedelta +from django.utils.timezone import datetime +from django.utils import timezone + from .calendar import CalendarList import logging @@ -257,7 +260,7 @@ class Event(models.Model): self.created_date = now if self.is_in_importation_process(): self.imported_date = now - if self.is_in_importation_process() or self.modified_date is None: + if self.modified_date is None: self.modified_date = now @@ -311,10 +314,13 @@ class Event(models.Model): del event_structure["url_human"] if "last_modified" in event_structure and event_structure["last_modified"] is not None: - event_structure["modified_date"] = event_structure["last_modified"] + d = datetime.fromisoformat(event_structure["last_modified"]) + if d.tzinfo is None or d.tzinfo.utcoffset(d) is None: + d = timezone.make_aware(d, timezone.get_default_timezone()) + event_structure["modified_date"] = d del event_structure["last_modified"] else: - event_structure["created_date"] = None + event_structure["modified_date"] = None if "start_time" in event_structure: event_structure["start_time"] = time.fromisoformat(event_structure["start_time"]) @@ -435,11 +441,13 @@ class Event(models.Model): same_imported = Event.find_last_imported_not_modified(same_events) if same_imported: - # if this event exists, it will be updated with new data - same_imported.update(event) - same_imported.set_in_importation_process() - same_imported.prepare_save() - to_update.append(same_imported) + # if this event exists, it will be updated with new data only if the data is fresher + logger.warning("{} vs {}".format(same_imported.modified_date, event.modified_date)) + if same_imported.modified_date < event.modified_date: + same_imported.update(event) + same_imported.set_in_importation_process() + same_imported.prepare_save() + to_update.append(same_imported) else: # otherwise, the new event possibly a duplication of the others. event.set_possibly_duplicated(same_events) @@ -475,6 +483,7 @@ class Event(models.Model): nb_removed = 0 if remove_missing: # events that are missing from the import but in database are turned into drafts + # only if they are in the future # TODO # TODO: ajouter self.source, ou faire référence à l'objet BatchImportation pass