From 5b86cbc43afd90a6fd6a92ad4815b5dee313c6a3 Mon Sep 17 00:00:00 2001 From: vincent porte Date: Wed, 14 Aug 2024 17:13:20 +0200 Subject: [PATCH] wip --- .../forum/management/commands/populate.py | 12 ++- lacommunaute/forum/urls.py | 2 + lacommunaute/forum/views.py | 73 ++++++++++++------- .../forum/forum_documentation_category.html | 41 +---------- .../templates/forum/partials/forum_tags.html | 34 +++++---- .../partials/subcategory_forum_list.html | 37 ++++++++++ .../partials/topic_tags.html | 2 +- lacommunaute/templates/partials/tag.html | 5 -- 8 files changed, 118 insertions(+), 88 deletions(-) create mode 100644 lacommunaute/templates/forum/partials/subcategory_forum_list.html delete mode 100644 lacommunaute/templates/partials/tag.html diff --git a/lacommunaute/forum/management/commands/populate.py b/lacommunaute/forum/management/commands/populate.py index c245abc76..4479a0024 100644 --- a/lacommunaute/forum/management/commands/populate.py +++ b/lacommunaute/forum/management/commands/populate.py @@ -28,9 +28,19 @@ def handle(self, *args, **options): for i in range(1, 3): parent = CategoryForumFactory(with_public_perms=True, name=f"Thème {i}") + TopicFactory.create_batch( + 2, forum=ForumFactory(parent=parent, with_public_perms=True, name=f"Fiche {i}-0"), with_post=True + ) for j in range(1, 3): TopicFactory.create_batch( - 2, forum=ForumFactory(parent=parent, with_public_perms=True, name=f"Fiche {i}-{j}"), with_post=True + 2, + forum=ForumFactory( + parent=parent, + with_public_perms=True, + name=f"Fiche {i}-{j}", + with_tags=[f"tag_{i}", f"tag_{i}{j}"], + ), + with_post=True, ) sys.stdout.write("documentation created\n") diff --git a/lacommunaute/forum/urls.py b/lacommunaute/forum/urls.py index 87b0abe48..77cd7c35c 100644 --- a/lacommunaute/forum/urls.py +++ b/lacommunaute/forum/urls.py @@ -8,6 +8,7 @@ ForumUpdateView, ForumView, SubCategoryForumCreateView, + SubCategoryForumListView, ) @@ -18,6 +19,7 @@ path("forum/-/", ForumView.as_view(), name="forum"), path("forum/-/update/", ForumUpdateView.as_view(), name="edit_forum"), path("forum/-/rate/", ForumRatingView.as_view(), name="rate"), + path("forum/-/subs/", SubCategoryForumListView.as_view(), name="subcategory_forums"), path("forums/", IndexView.as_view(), name="index"), path("documentation/", CategoryForumListView.as_view(), name="documentation"), path("documentation/category/create/", CategoryForumCreateView.as_view(), name="create_category"), diff --git a/lacommunaute/forum/views.py b/lacommunaute/forum/views.py index 6439c0b2c..8974922d2 100644 --- a/lacommunaute/forum/views.py +++ b/lacommunaute/forum/views.py @@ -4,6 +4,7 @@ from django.contrib.auth.mixins import UserPassesTestMixin from django.contrib.contenttypes.models import ContentType from django.db.models.query import QuerySet +from django.http import Http404 from django.shortcuts import get_object_or_404, render from django.urls import reverse, reverse_lazy from django.views import View @@ -27,7 +28,37 @@ ForumVisibilityContentTree = get_class("forum.visibility", "ForumVisibilityContentTree") -class ForumView(BaseForumView, FilteredTopicsListViewMixin): +class SubCategoryForumListMixin: + def get_descendants(self): + qs = self.get_forum().get_descendants() + + forum_tags = self.request.GET.get("forum_tags") + if forum_tags: + qs = qs.filter(tags__slug__in=forum_tags.split(",")) + + 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 forum_tags_context(self): + return { + # TODO : remove permission management, though all forums are public in our case + "sub_forums": ForumVisibilityContentTree.from_forums( + self.request.forum_permission_handler.forum_list_filter( + self.get_descendants(), + self.request.user, + ), + ), + "sub_forums_tags": self.get_descendants_tags(), + "active_forum_tag_slug": self.request.GET.get("forum_tags") or None, + } + + +class ForumView(BaseForumView, FilteredTopicsListViewMixin, SubCategoryForumListMixin): paginate_by = settings.FORUM_TOPICS_NUMBER_PER_PAGE def get_template_names(self): @@ -48,21 +79,6 @@ def will_render_documentation_category_variant(self): def get_queryset(self): return self.filter_queryset(self.get_forum().topics.optimized_for_topics_list(self.request.user.id)) - def get_descendants(self): - qs = self.get_forum().get_descendants() - - forum_tags = self.request.GET.get("forum_tags") - if forum_tags: - qs = qs.filter(tags__slug__in=forum_tags.split(",")) - - 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() @@ -93,16 +109,7 @@ def get_context_data(self, **kwargs): context = context | self.get_topic_filter_context() 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 + context = context | self.forum_tags_context() if self.will_render_documentation_variant(): context["sibling_forums"] = forum.get_siblings(include_self=True) @@ -112,6 +119,20 @@ def get_context_data(self, **kwargs): return context +class SubCategoryForumListView(BaseForumView, SubCategoryForumListMixin): + template_name = "forum/partials/subcategory_forum_list.html" + + def get(self, request, **kwargs): + if self.get_forum().is_in_documentation_area and self.forum.level == 0: + return super().get(request, **kwargs) + raise Http404 + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context = context | self.forum_tags_context() + return context + + class ForumUpdateView(UserPassesTestMixin, UpdateView): template_name = "forum/forum_create_or_update.html" form_class = ForumForm diff --git a/lacommunaute/templates/forum/forum_documentation_category.html b/lacommunaute/templates/forum/forum_documentation_category.html index d5ddc6ab0..0e9b207fa 100644 --- a/lacommunaute/templates/forum/forum_documentation_category.html +++ b/lacommunaute/templates/forum/forum_documentation_category.html @@ -1,45 +1,6 @@ {% 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 %} -
- -
- {% endfor %} -
+ {% include "forum/partials/subcategory_forum_list.html" with request=request sub_forums=sub_forums sub_forums_tags=sub_forums_tags active_forum_tag_slug=active_forum_tag_slug only %} {% endblock subforum_list %} {% block forum_foot_content %} {% if user.is_superuser %} diff --git a/lacommunaute/templates/forum/partials/forum_tags.html b/lacommunaute/templates/forum/partials/forum_tags.html index c69b9b645..b59006695 100644 --- a/lacommunaute/templates/forum/partials/forum_tags.html +++ b/lacommunaute/templates/forum/partials/forum_tags.html @@ -1,16 +1,20 @@ {% 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 %} -
+{% for tag in tags %} + {% if path and tag.slug == active_forum_tag_slug %} + + + {{ tag.name }} + + {% elif path %} + {% url_add_query path forum_tags=tag.slug as url_with_query_params %} + {{ tag.name }} + {% else %} + {{ tag.name }} + {% endif %} +{% endfor %} +{% comment %} + + +hx-target"#topicsarea" hx-swap="outerHTML" hx-push-url="true" hx-get="{% url forum_extension:subcategory_forums forum.pk forum.slug %}?forum_tags={{ tag.slug }}" class="tag bg-info text-white" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Supprimer ce filtre" +{% endcomment %} diff --git a/lacommunaute/templates/forum/partials/subcategory_forum_list.html b/lacommunaute/templates/forum/partials/subcategory_forum_list.html new file mode 100644 index 000000000..3b88fb065 --- /dev/null +++ b/lacommunaute/templates/forum/partials/subcategory_forum_list.html @@ -0,0 +1,37 @@ +
+ {% if sub_forums_tags %} +
+
Afficher les fiches contenant l'étiquette
+
+ {% include "forum/partials/forum_tags.html" with tags=sub_forums_tags active_forum_tag_slug=active_forum_tag_slug path=request.path only %} +
+
+ {% endif %} +
+ {% for node in sub_forums.top_nodes %} +
+ +
+ {% endfor %} +
+
diff --git a/lacommunaute/templates/forum_conversation/partials/topic_tags.html b/lacommunaute/templates/forum_conversation/partials/topic_tags.html index f3b86cad4..1f3cb8619 100644 --- a/lacommunaute/templates/forum_conversation/partials/topic_tags.html +++ b/lacommunaute/templates/forum_conversation/partials/topic_tags.html @@ -1,5 +1,5 @@ {% load url_add_query %} {% for tag in tags %} {% url_add_query request.path tags=tag.slug page=1 as url_with_query_params %} - {% include "partials/tag.html" with tag=tag url=url_with_query_params only %} + {{ tag.name }} {% endfor %} diff --git a/lacommunaute/templates/partials/tag.html b/lacommunaute/templates/partials/tag.html deleted file mode 100644 index 02d482e7c..000000000 --- a/lacommunaute/templates/partials/tag.html +++ /dev/null @@ -1,5 +0,0 @@ -{% if url %} - {{ tag.name }} -{% else %} - {{ tag.name }} -{% endif %}