Amélioration de la requête des tags

This commit is contained in:
Jean-Marie Favreau 2024-11-13 11:22:06 +01:00
parent 2ce8f30275
commit 0bdd8693ec
2 changed files with 11 additions and 13 deletions

View File

@ -1,4 +1,4 @@
from django.db import models
from django.db import models, connection
from django_better_admin_arrayfield.models.fields import ArrayField
from django.utils.translation import gettext_lazy as _
from django.utils.safestring import mark_safe
@ -627,15 +627,13 @@ class Event(models.Model):
permissions = [("set_duplicated_event", "Can set an event as duplicated")]
def get_all_tags():
try:
tags = list(Event.objects.values_list("tags", flat=True))
except:
tags = []
uniq_tags = set()
for t in tags:
if t is not None:
uniq_tags = uniq_tags | set(t)
return list(uniq_tags)
cursor = connection.cursor()
raw_query = """
select unnest(subquery_alias.tags) as distinct_tags, count(*) as tags_group_by_count
from (select tags from agenda_culturel_event) as subquery_alias group by distinct_tags;
"""
cursor.execute(raw_query)
return [{"tag": row[0], "count": row[1]} for row in cursor]
def is_draft(self):
return self.status == Event.STATUS.DRAFT

View File

@ -178,7 +178,7 @@ class EventFilter(django_filters.FilterSet):
exclude_tags = django_filters.MultipleChoiceFilter(
label="Exclure les étiquettes",
choices=[(t, t) for t in Event.get_all_tags()],
choices=[(t["tag"], t["tag"]) for t in Event.get_all_tags()],
lookup_expr="icontains",
field_name="tags",
exclude=True,
@ -187,7 +187,7 @@ class EventFilter(django_filters.FilterSet):
tags = django_filters.MultipleChoiceFilter(
label="Inclure les étiquettes",
choices=[(t, t) for t in Event.get_all_tags()],
choices=[(t["tag"], t["tag"]) for t in Event.get_all_tags()],
lookup_expr="icontains",
conjoined=True,
field_name="tags",
@ -2192,7 +2192,7 @@ def view_tag(request, t):
def tag_list(request):
tags = Event.get_all_tags()
tags = [t["tag"] for t in Event.get_all_tags()]
objects = Tag.objects.order_by("name").all()
context = {"other_tags": sorted(tags, key=lambda x: remove_accents(x).lower()),
"tags": objects}