From 4733bb3eec96ab264d01094a9574edc07dc8dc68 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sat, 9 Nov 2024 14:32:21 +0100 Subject: [PATCH] Factorisation --- src/agenda_culturel/forms.py | 2 +- src/agenda_culturel/models.py | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/agenda_culturel/forms.py b/src/agenda_culturel/forms.py index 68fbf62..5730b4d 100644 --- a/src/agenda_culturel/forms.py +++ b/src/agenda_culturel/forms.py @@ -177,7 +177,7 @@ class FixDuplicates(Form): ) ] extra = "" - if len([e for e in events if e.modified()]) != 0: + if edup.has_modified(): extra = _(" Warning: a version is already locally modified.") choices += [ ("Merge", _("Create a new version by merging.") + extra) diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 5b8e3a5..e6df161 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -237,6 +237,20 @@ class DuplicatedEvents(models.Model): def fixed(self): return not self.representative is None + def has_modified(self): + return len([e for e in self.get_duplicated() if e.modified()]) > 0 + + def get_local_version(self): + if self.representative and self.representative.modified(): + return self.representative + + l = [e for e in self.get_duplicated() if e.modified()] + if len(l) == 0: + return None + else: + l.sort(key=lambda x: -x.modified_date) + return l[0] + def merge_into(self, other): # for all objects associated to this group for e in self.get_duplicated(): @@ -668,15 +682,7 @@ class Event(models.Model): if self.other_versions is None: return None - # otherwise check the representative version - if not self.other_versions.representative is None: - if self.other_versions.representative.modified(): - return self.other_versions.representative - - # finally, get the last modified version within the other versions - e = [e for e in self.other_versions.event_set.order_by("-modified_date") if e.modified] - - return None if len(e) == 0 else e[0] + return self.other_versions.get_local_version() def nb_draft_events(): return Event.objects.filter(status=Event.STATUS.DRAFT).count()