Optimisation de requêtes
This commit is contained in:
parent
13daec625b
commit
7556b39b43
@ -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)
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user