Skip to content

Commit

Permalink
(forum) minimalistic tag filtering in forum view, until it is refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed May 21, 2024
1 parent fd0d832 commit b6146b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b6146b1

Please sign in to comment.