From b6146b1600fc955b3103b7e47fd18a4389f386e2 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Tue, 14 May 2024 18:19:53 +0200 Subject: [PATCH] (forum) minimalistic tag filtering in forum view, until it is refactored --- lacommunaute/forum/tests/tests_views.py | 10 ++++++++++ lacommunaute/forum/views.py | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/lacommunaute/forum/tests/tests_views.py b/lacommunaute/forum/tests/tests_views.py index 0e1bec01f..a0590af0d 100644 --- a/lacommunaute/forum/tests/tests_views.py +++ b/lacommunaute/forum/tests/tests_views.py @@ -411,3 +411,13 @@ def test_can_view_update_forum_link(self): self.user.save() response = self.client.get(self.url) self.assertContains(response, url) + + def test_filtered_queryset_on_tag(self): + tag = faker.word() + topic = TopicFactory(forum=self.forum, with_tags=[tag], with_post=True) + + response = self.client.get( + reverse("forum_extension:forum", kwargs={"pk": self.forum.pk, "slug": self.forum.slug}), {"tags": tag} + ) + self.assertContains(response, topic.subject) + self.assertNotContains(response, self.topic.subject) diff --git a/lacommunaute/forum/views.py b/lacommunaute/forum/views.py index dc2b5fc7c..540d83932 100644 --- a/lacommunaute/forum/views.py +++ b/lacommunaute/forum/views.py @@ -26,10 +26,18 @@ class ForumView(BaseForumView): paginate_by = settings.FORUM_TOPICS_NUMBER_PER_PAGE + def get_tags(self): + if not hasattr(self, "tags"): + self.tags = self.request.GET.get("tags", "").lower() + return self.tags + def get_queryset(self): forum = self.get_forum() qs = forum.topics.optimized_for_topics_list(self.request.user.id) + if self.get_tags(): + qs = qs.filter(tags__slug__in=self.get_tags().split(",")) + return qs def get_context_data(self, **kwargs):