From 7556b39b434698299627d40708dae4617cff04ab Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Fri, 20 Sep 2024 14:28:53 +0200 Subject: [PATCH] =?UTF-8?q?Optimisation=20de=20requ=C3=AAtes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/models.py | 15 +++++++++------ .../templatetags/duplicated_extra.py | 10 +++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index af1e373..ab8ba29 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -17,6 +17,7 @@ import recurrence.fields import recurrence import copy import unicodedata +from collections import defaultdict from django.template.defaultfilters import date as _date from datetime import time, timedelta, date @@ -209,17 +210,19 @@ class DuplicatedEvents(models.Model): singletons = DuplicatedEvents.objects.annotate(nb_events=Count("event")).filter( nb_events__lte=1 ) - nb = len(singletons) - if nb > 0: - logger.warning("Removing: " + str(nb) + " empty or singleton duplicated") - singletons.delete() + nb, d = singletons.delete() return nb def remove_similar_entries(): to_be_removed = [] - duplicates = DuplicatedEvents.objects.all() + + dup_events = Event.objects.order_by('possibly_duplicated').prefetch_related('possibly_duplicated', 'category') + duplicates = defaultdict(list) + for e in dup_events: + duplicates[e.possibly_duplicated].append(e) + for d in duplicates: - comp = d.get_items_comparison() + comp = Event.get_comparison(duplicates[d]) similar = len([c for c in comp if not c["similar"]]) == 0 if similar: to_be_removed.append(d) diff --git a/src/agenda_culturel/templatetags/duplicated_extra.py b/src/agenda_culturel/templatetags/duplicated_extra.py index fe26708..f583426 100644 --- a/src/agenda_culturel/templatetags/duplicated_extra.py +++ b/src/agenda_culturel/templatetags/duplicated_extra.py @@ -2,6 +2,8 @@ from django import template from django.utils.safestring import mark_safe from django.urls import reverse_lazy from django.template.defaultfilters import pluralize +from django.db.models import Count + from agenda_culturel.models import DuplicatedEvents @@ -13,14 +15,8 @@ register = template.Library() @register.simple_tag def show_badge_duplicated(placement="top"): - duplicated = DuplicatedEvents.objects.all() - nb_duplicated = 0 - for d in duplicated: - if d.nb_duplicated() >= 1: - nb_duplicated += 1 - else: - d.delete() + nb_duplicated = DuplicatedEvents.objects.annotate(nb_duplicated=Count('event')).filter(nb_duplicated__gte=1).count() if nb_duplicated != 0: return mark_safe(