From 59b65a17dfe522891774c15a1c7b177f4ccaad8e Mon Sep 17 00:00:00 2001 From: vincent porte Date: Tue, 14 May 2024 17:06:57 +0200 Subject: [PATCH] (forum_conversation) filtering on multiple tags, displaying their label --- .../forum_conversation/tests/tests_views.py | 17 +++++++++-------- lacommunaute/forum_conversation/views.py | 17 ++++++++++++----- .../partials/topic_filter.html | 4 ++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lacommunaute/forum_conversation/tests/tests_views.py b/lacommunaute/forum_conversation/tests/tests_views.py index 52b548606..0380ce4d9 100644 --- a/lacommunaute/forum_conversation/tests/tests_views.py +++ b/lacommunaute/forum_conversation/tests/tests_views.py @@ -756,9 +756,10 @@ def test_context(self): self.assertEqual(response.context_data["active_filter_name"], Filters.ALL.label) def test_context_with_tag(self): - tag = faker.word() - response = self.client.get(self.url + f"?tags={tag}") - self.assertEqual(response.context_data["active_tags"], tag) + tags = [Tag.objects.create(name=faker.sentence()) for i in range(2)] + response = self.client.get(self.url, {"tags": ",".join([tag.slug for tag in tags])}) + self.assertEqual(response.context_data["active_tags"], ",".join([tag.slug for tag in tags])) + self.assertEqual(response.context_data["active_tags_label"], " ou ".join([tag.name for tag in tags])) def test_queryset(self): TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.PRIVATE_FORUM, with_public_perms=True)) @@ -851,11 +852,11 @@ def test_filter_dropdown_visibility(self): self.assertEqual(response.context_data["display_filter_dropdown"], False) def test_filter_dropdown_with_tags(self): - tag = faker.word() - response = self.client.get(self.url + f"?tags={tag}") - self.assertContains(response, f'hx-get="/topics/?filter=ALL&tags={tag}"') - self.assertContains(response, f'hx-get="/topics/?filter=NEW&tags={tag}"') - self.assertContains(response, f'hx-get="/topics/?filter=CERTIFIED&tags={tag}"') + tag = Tag.objects.create(name=faker.words(nb=3)) + response = self.client.get(self.url + f"?tags={tag.slug}") + self.assertContains(response, f'hx-get="/topics/?filter=ALL&tags={tag.slug}"') + self.assertContains(response, f'hx-get="/topics/?filter=NEW&tags={tag.slug}"') + self.assertContains(response, f'hx-get="/topics/?filter=CERTIFIED&tags={tag.slug}"') def test_template_name(self): response = self.client.get(self.url) diff --git a/lacommunaute/forum_conversation/views.py b/lacommunaute/forum_conversation/views.py index be647881a..2bf7d969f 100644 --- a/lacommunaute/forum_conversation/views.py +++ b/lacommunaute/forum_conversation/views.py @@ -7,6 +7,7 @@ from django.views.generic import ListView from machina.apps.forum_conversation import views from machina.core.loading import get_class +from taggit.models import Tag from lacommunaute.forum.enums import Kind as ForumKind from lacommunaute.forum.models import Forum @@ -110,9 +111,14 @@ def get_filter(self): self.filter = self.request.GET.get("filter", None) return self.filter - def get_tags(self): + def get_tags(self, flat=None): if not hasattr(self, "tags"): - self.tags = self.request.GET.get("tags", "").lower() + self.tags = Tag.objects.filter(slug__in=self.request.GET.get("tags", "").lower().split(",")) + + if flat == "name": + return " ou ".join(self.tags.values_list("name", flat=True)) + if flat == "slug": + return ",".join(self.tags.values_list("slug", flat=True)) return self.tags def get_queryset(self): @@ -124,7 +130,7 @@ def get_queryset(self): qs = qs.filter(certified_post__isnull=False) if self.get_tags(): - qs = qs.filter(tags__slug__in=self.get_tags().split(",")) + qs = qs.filter(tags__in=self.get_tags()) return qs @@ -133,7 +139,7 @@ def get_context_data(self, **kwargs): context["form"] = PostForm(user=self.request.user) encoded_params = urlencode( - {k: v for k, v in {"filter": self.get_filter(), "tags": self.get_tags()}.items() if v} + {k: v for k, v in {"filter": self.get_filter(), "tags": self.get_tags(flat="slug")}.items() if v} ) context["loadmoretopic_url"] = reverse("forum_conversation_extension:topics") if encoded_params: @@ -142,7 +148,8 @@ def get_context_data(self, **kwargs): context["active_filter_name"] = ( getattr(Filters, self.get_filter(), Filters.ALL).label if self.get_filter() else Filters.ALL.label ) - context["active_tags"] = self.get_tags() + context["active_tags"] = self.get_tags(flat="slug") + context["active_tags_label"] = self.get_tags(flat="name") context["display_filter_dropdown"] = False if self.request.GET.get("page") else True context["loadmoretopic_suffix"] = "topics" diff --git a/lacommunaute/templates/forum_conversation/partials/topic_filter.html b/lacommunaute/templates/forum_conversation/partials/topic_filter.html index 070c47895..0b9b92cd1 100644 --- a/lacommunaute/templates/forum_conversation/partials/topic_filter.html +++ b/lacommunaute/templates/forum_conversation/partials/topic_filter.html @@ -5,7 +5,7 @@
{{ total }} question{{ total|pluralizefr }} - {% if active_tag %}avec l'étiquette {{ active_tag }}{% endif %} + {% if active_tags %}avec l'étiquette {{ active_tags_label }}{% endif %}
@@ -21,7 +21,7 @@ hx-target="#topicsarea" hx-swap="outerHTML" hx-push-url="true" - hx-get="{% url 'forum_conversation_extension:topics' %}?filter={{ filter.0 }}{% if active_tag %}&tags={{ active_tag }}{% endif %}" + hx-get="{% url 'forum_conversation_extension:topics' %}?filter={{ filter.0 }}{% if active_tags %}&tags={{ active_tags }}{% endif %}" class="dropdown-item matomo-event" data-matomo-category="engagement" data-matomo-action="filter"