Skip to content

Commit

Permalink
(forum_conversation) filtering on multiple tags, displaying their label
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed May 21, 2024
1 parent e0af551 commit 59b65a1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
17 changes: 9 additions & 8 deletions lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions lacommunaute/forum_conversation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="d-flex align-items-center">
<div class="flex-grow-1" id="topic-list-filter-header">
<span class="h5 m-0">{{ total }} question{{ total|pluralizefr }}
{% if active_tag %}avec l'étiquette {{ active_tag }}{% endif %}
{% if active_tags %}avec l'étiquette {{ active_tags_label }}{% endif %}
</span>
</div>
<div>
Expand All @@ -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"
Expand Down

0 comments on commit 59b65a1

Please sign in to comment.