From a0e88de074faa04e4701509fba393de7d94540e0 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Tue, 13 Aug 2024 18:07:20 +0200 Subject: [PATCH] feat(tags): show tags user can filter on (tests in progress) --- lacommunaute/forum/views.py | 25 +++++++++++++------ .../forum/forum_documentation_category.html | 6 +++++ .../templates/forum/partials/forum_tags.html | 16 ++++++++++++ lacommunaute/templates/partials/tag.html | 2 +- 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 lacommunaute/templates/forum/partials/forum_tags.html diff --git a/lacommunaute/forum/views.py b/lacommunaute/forum/views.py index 191c7920..6439c0b2 100644 --- a/lacommunaute/forum/views.py +++ b/lacommunaute/forum/views.py @@ -10,6 +10,7 @@ from django.views.generic import CreateView, ListView, UpdateView from machina.apps.forum.views import ForumView as BaseForumView from machina.core.loading import get_class +from taggit.models import Tag from lacommunaute.forum.enums import Kind as ForumKind from lacommunaute.forum.forms import ForumForm @@ -56,6 +57,12 @@ def get_descendants(self): return qs.prefetch_related("tags") + def get_descendants_tags(self): + return Tag.objects.filter( + taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(Forum), + taggit_taggeditem_items__object_id__in=self.get_forum().get_descendants().values_list("id", flat=True), + ).distinct() + def get_context_data(self, **kwargs): forum = self.get_forum() @@ -85,13 +92,17 @@ def get_context_data(self, **kwargs): ) context = context | self.get_topic_filter_context() - # vincentporte, overide the method to add the sub_forums, not testing permissions ^v^ - context["sub_forums"] = ForumVisibilityContentTree.from_forums( - self.request.forum_permission_handler.forum_list_filter( - self.get_descendants(), - self.request.user, - ), - ) + if self.will_render_documentation_category_variant(): + # vincentporte, overide the method to add the sub_forums, not testing permissions ^v^ + # only in the documentation category which is public + context["sub_forums"] = ForumVisibilityContentTree.from_forums( + self.request.forum_permission_handler.forum_list_filter( + self.get_descendants(), + self.request.user, + ), + ) + context["sub_forums_tags"] = self.get_descendants_tags() + context["active_forum_tag_slug"] = self.request.GET.get("forum_tags") or None if self.will_render_documentation_variant(): context["sibling_forums"] = forum.get_siblings(include_self=True) diff --git a/lacommunaute/templates/forum/forum_documentation_category.html b/lacommunaute/templates/forum/forum_documentation_category.html index dd1f2eb7..9b89cbfc 100644 --- a/lacommunaute/templates/forum/forum_documentation_category.html +++ b/lacommunaute/templates/forum/forum_documentation_category.html @@ -1,4 +1,10 @@ {% extends "forum/forum_detail.html" %} +{% block forum_head_content %} + {{ block.super }} + {% if sub_forums_tags %} + {% include "forum/partials/forum_tags.html" with tags=sub_forums_tags only %} + {% endif %} +{% endblock forum_head_content %} {% block subforum_list %}
{% for node in sub_forums.top_nodes %} diff --git a/lacommunaute/templates/forum/partials/forum_tags.html b/lacommunaute/templates/forum/partials/forum_tags.html new file mode 100644 index 00000000..c69b9b64 --- /dev/null +++ b/lacommunaute/templates/forum/partials/forum_tags.html @@ -0,0 +1,16 @@ +{% load url_add_query %} +
+ Filtrez par sous-catégorie :  + {% for tag in tags %} + + {% if tag.slug == active_forum_tag_slug %} + {% include "partials/tag.html" with tag=tag highlight=1 only %} + {% else %} + {% include "partials/tag.html" with tag=tag only %} + {% endif %} +   + {% if forloop.last and active_forum_tag_slug %} + supprimer le filtre + {% endif %} + {% endfor %} +
diff --git a/lacommunaute/templates/partials/tag.html b/lacommunaute/templates/partials/tag.html index 62e3e352..b2510366 100644 --- a/lacommunaute/templates/partials/tag.html +++ b/lacommunaute/templates/partials/tag.html @@ -1 +1 @@ -{{ tag.name }} +{{ tag.name }}