Optimisation de requêtes
This commit is contained in:
parent
13daec625b
commit
7556b39b43
@ -17,6 +17,7 @@ import recurrence.fields
|
|||||||
import recurrence
|
import recurrence
|
||||||
import copy
|
import copy
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from django.template.defaultfilters import date as _date
|
from django.template.defaultfilters import date as _date
|
||||||
from datetime import time, timedelta, date
|
from datetime import time, timedelta, date
|
||||||
@ -209,17 +210,19 @@ class DuplicatedEvents(models.Model):
|
|||||||
singletons = DuplicatedEvents.objects.annotate(nb_events=Count("event")).filter(
|
singletons = DuplicatedEvents.objects.annotate(nb_events=Count("event")).filter(
|
||||||
nb_events__lte=1
|
nb_events__lte=1
|
||||||
)
|
)
|
||||||
nb = len(singletons)
|
nb, d = singletons.delete()
|
||||||
if nb > 0:
|
|
||||||
logger.warning("Removing: " + str(nb) + " empty or singleton duplicated")
|
|
||||||
singletons.delete()
|
|
||||||
return nb
|
return nb
|
||||||
|
|
||||||
def remove_similar_entries():
|
def remove_similar_entries():
|
||||||
to_be_removed = []
|
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:
|
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
|
similar = len([c for c in comp if not c["similar"]]) == 0
|
||||||
if similar:
|
if similar:
|
||||||
to_be_removed.append(d)
|
to_be_removed.append(d)
|
||||||
|
@ -2,6 +2,8 @@ from django import template
|
|||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
from django.template.defaultfilters import pluralize
|
from django.template.defaultfilters import pluralize
|
||||||
|
from django.db.models import Count
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from agenda_culturel.models import DuplicatedEvents
|
from agenda_culturel.models import DuplicatedEvents
|
||||||
@ -13,14 +15,8 @@ register = template.Library()
|
|||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
def show_badge_duplicated(placement="top"):
|
def show_badge_duplicated(placement="top"):
|
||||||
duplicated = DuplicatedEvents.objects.all()
|
|
||||||
|
|
||||||
nb_duplicated = 0
|
nb_duplicated = DuplicatedEvents.objects.annotate(nb_duplicated=Count('event')).filter(nb_duplicated__gte=1).count()
|
||||||
for d in duplicated:
|
|
||||||
if d.nb_duplicated() >= 1:
|
|
||||||
nb_duplicated += 1
|
|
||||||
else:
|
|
||||||
d.delete()
|
|
||||||
|
|
||||||
if nb_duplicated != 0:
|
if nb_duplicated != 0:
|
||||||
return mark_safe(
|
return mark_safe(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user