From 9a7733566cc8cc6c2f10aa3f1b4fb44d67f3ed38 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sun, 28 Apr 2024 14:04:53 +0200 Subject: [PATCH] =?UTF-8?q?suppression=20des=20non=20dupliqu=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/models.py | 21 ++++++++++++++++++++- src/agenda_culturel/views.py | 5 +++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 6a77e61..8ab47ee 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -151,10 +151,29 @@ class DuplicatedEvents(models.Model): def remove_singletons(): singletons = DuplicatedEvents.objects.annotate(nb_events=Count("event")).filter(nb_events__lte=1) nb = len(singletons) - logger.warning("Removing: " + str(nb) + " empty or singleton duplicated") + if nb > 0: + logger.warning("Removing: " + str(nb) + " empty or singleton duplicated") singletons.delete() return nb + def remove_similar_entries(): + to_be_removed = [] + duplicates = DuplicatedEvents.objects.all() + for d in duplicates: + comp = d.get_items_comparison() + similar = len([c for c in comp if not c["similar"]]) > 0 + if similar: + to_be_removed.append(d) + + nb = len(to_be_removed) + if nb > 0: + logger.warning("Removing: " + str(nb) + " similar duplicated") + for s in to_be_removed: + s.delete() + return nb + + + class Place(models.Model): name = models.CharField(verbose_name=_('Name'), help_text=_('Name of the place')) address = models.CharField(verbose_name=_('Address'), help_text=_('Address of this place (without city name)'), blank=True, null=True) diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index 9816969..0f9a1b9 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -873,8 +873,9 @@ class DuplicatedEventsUpdateView(LoginRequiredMixin, UpdateView): @permission_required('agenda_culturel.view_duplicatedevents') def duplicates(request): nb_removed = DuplicatedEvents.remove_singletons() - if nb_removed: - messages.success(request, _("Cleaning up duplicates: {} item(s) removed.").format(nb_removed)) + nb_similar = DuplicatedEvents.remove_similar_entries() + if nb_removed > 0 or nb_similar > 0: + messages.success(request, _("Cleaning up duplicates: {} item(s) removed.").format(nb_removed + nb_similar)) paginator = Paginator(DuplicatedEvents.objects.all(), 10) page = request.GET.get('page')