From 0bdd8693ec91c02c62778e5948a1dfb7e789e734 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Wed, 13 Nov 2024 11:22:06 +0100 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20de=20la=20requ=C3=AAte=20de?= =?UTF-8?q?s=20tags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/models.py | 18 ++++++++---------- src/agenda_culturel/views.py | 6 +++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 1f08f56..2c45e32 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -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 diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index 6a31757..d549552 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -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}