From e46116fe0fa776fc1ce9159931597c084f297469 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Tue, 21 Jan 2025 23:34:58 +0100 Subject: [PATCH] =?UTF-8?q?On=20int=C3=A8gre=20les=20=C3=A9tiquettes=20r?= =?UTF-8?q?=C3=A9cemment=20cr=C3=A9=C3=A9es=20aux=20=C3=A9tiquettes=20prop?= =?UTF-8?q?os=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #280 --- src/agenda_culturel/models.py | 12 ++++++++++++ src/agenda_culturel/views.py | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index fa43df0..f887fac 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -232,6 +232,15 @@ class Tag(models.Model): def get_absolute_url(self): return reverse("view_tag", kwargs={"t": self.name}) + + def clear_cache(): + for exclude in [False, True]: + for include in [False, True]: + for nb_suggestions in [10]: + id_cache = 'all_tags ' + str(exclude) + ' ' + str(include) + ' ' + str(nb_suggestions) + id_cache = hashlib.md5(id_cache.encode("utf8")).hexdigest() + cache.delete(id_cache) + def get_tag_groups(nb_suggestions=10, exclude=False, include=False, all=False): id_cache = 'all_tags ' + str(exclude) + ' ' + str(include) + ' ' + str(nb_suggestions) id_cache = hashlib.md5(id_cache.encode("utf8")).hexdigest() @@ -240,6 +249,7 @@ class Tag(models.Model): if not result: free_tags = Event.get_all_tags(False) + f_tags = [t["tag"] for t in free_tags] obj_tags = Tag.objects @@ -260,6 +270,8 @@ class Tag(models.Model): nb_suggestions = len(obj_tags) tags = [{"tag": t["tag"], "count": 1000000 if t["tag"] in obj_tags else t["count"]} for t in free_tags] + tags += [{"tag": o, "count": 0} for o in Tag.objects.filter(~Q(name__in=f_tags)).values_list("name", flat=True)] + tags.sort(key=lambda x: -x["count"]) diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index 1822fd0..409ab12 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -2097,6 +2097,10 @@ class TagCreateView(PermissionRequiredMixin, SuccessMessageMixin, CreateView): initial["name"] = self.request.GET.get("name") return initial + def form_valid(self, form): + Tag.clear_cache() + return super().form_valid(form) + class TagDeleteView(PermissionRequiredMixin, DeleteView): model = Tag @@ -2135,12 +2139,14 @@ def view_tag(request, t, past=False): def tag_list(request): tags = Event.get_all_tags() + r_tags = [t["tag"] for t in tags] objects = Tag.objects.order_by("name").all() d_objects = dict() for o in objects: d_objects[o.name] = o tags = [t | {'obj': d_objects[t["tag"]]} if t["tag"] in d_objects else t for t in tags] + tags += [{'obj': o, "tag": o.name, "count": 0} for o in objects if o.name not in r_tags] context = {"tags": sorted(tags, key=lambda x: emoji.demojize(remove_accents(x["tag"]).lower(), delimiters=('000', '')))} return render(request, "agenda_culturel/tags.html", context)