-
-
diff --git a/lacommunaute/forum/tests/test_subcategoryforum_listview.py b/lacommunaute/forum/tests/test_subcategoryforum_listview.py
new file mode 100644
index 00000000..bb8681b2
--- /dev/null
+++ b/lacommunaute/forum/tests/test_subcategoryforum_listview.py
@@ -0,0 +1,62 @@
+import pytest # noqa
+from django.urls import reverse
+
+from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory
+from lacommunaute.utils.testing import parse_response_to_soup
+
+
+@pytest.fixture(name="documentation_forum")
+def fixture_documentation_forum():
+ category_forum = CategoryForumFactory(with_public_perms=True, for_snapshot=True, name="Category Forum")
+ ForumFactory(
+ parent=category_forum,
+ with_public_perms=True,
+ with_image=True,
+ with_tags=["Jazz", "Blues"],
+ for_snapshot=True,
+ name="Ella Fitzgerald",
+ )
+ ForumFactory(
+ parent=category_forum, with_public_perms=True, with_image=True, for_snapshot=True, name="Mayra Andrade"
+ )
+ return category_forum
+
+
+def test_listview_filtering(client, db, documentation_forum, snapshot):
+ response = client.get(
+ reverse(
+ "forum_extension:subcategory_forums",
+ kwargs={"slug": documentation_forum.slug, "pk": documentation_forum.pk},
+ )
+ )
+ assert response.status_code == 200
+ content = parse_response_to_soup(
+ response, replace_in_href=documentation_forum.get_descendants(include_self=True), replace_img_src=True
+ )
+ assert str(content) == snapshot(name="subcategoryforum_listview_without_filter")
+
+ response = client.get(
+ reverse(
+ "forum_extension:subcategory_forums",
+ kwargs={"slug": documentation_forum.slug, "pk": documentation_forum.pk},
+ )
+ + "?forum_tag=rock"
+ )
+ assert response.status_code == 200
+ content = parse_response_to_soup(
+ response, replace_in_href=documentation_forum.get_descendants(include_self=True), replace_img_src=True
+ )
+ assert str(content) == snapshot(name="subcategoryforum_listview_filtering_on_non_existing_tag")
+
+ response = client.get(
+ reverse(
+ "forum_extension:subcategory_forums",
+ kwargs={"slug": documentation_forum.slug, "pk": documentation_forum.pk},
+ )
+ + "?forum_tag=jazz"
+ )
+ assert response.status_code == 200
+ content = parse_response_to_soup(
+ response, replace_in_href=documentation_forum.get_descendants(include_self=True), replace_img_src=True
+ )
+ assert str(content) == snapshot(name="subcategoryforum_listview_filtering_on_existing_tag")
diff --git a/lacommunaute/forum/tests/tests_views.py b/lacommunaute/forum/tests/tests_views.py
index b340034a..63b2fc72 100644
--- a/lacommunaute/forum/tests/tests_views.py
+++ b/lacommunaute/forum/tests/tests_views.py
@@ -697,21 +697,19 @@ def test_filter_subforums_on_tags(self, client, db):
category_forum.get_children()
)
- # filter on first tag
- response = client.get(category_forum.get_absolute_url() + f"?forum_tags={tags[0]}")
+ # filter on one tag
+ response = client.get(category_forum.get_absolute_url() + f"?forum_tag={tags[0]}")
assert response.status_code == 200
assert set([node.obj for node in response.context_data["sub_forums"].top_nodes]) == set(
[first_child, second_child]
)
- # filter on multiple tags
- response = client.get(category_forum.get_absolute_url() + f"?forum_tags={tags[1]},{tags[2]}")
+ # filtering on multiple tags is not supported yet
+ response = client.get(category_forum.get_absolute_url() + f"?forum_tag={tags[1]},{tags[2]}")
assert response.status_code == 200
- assert set([node.obj for node in response.context_data["sub_forums"].top_nodes]) == set(
- [second_child, third_child]
- )
+ assert set([node.obj for node in response.context_data["sub_forums"].top_nodes]) == set([])
- def test_show_subforum_tags(self, client, db, snapshot, reset_forum_sequence):
+ def test_show_subforum_tag(self, client, db, snapshot, reset_forum_sequence):
category_forum = CategoryForumFactory(with_public_perms=True, for_snapshot=True)
ForumFactory(parent=category_forum, with_public_perms=True, for_snapshot=True, name="Test-1")
ForumFactory(
@@ -726,14 +724,15 @@ def test_show_subforum_tags(self, client, db, snapshot, reset_forum_sequence):
assert response.status_code == 200
content = parse_response_to_soup(response, selector="main", replace_img_src=True)
- assert str(content) == snapshot(name="documentation_category_subforum_tags")
+ assert str(content) == snapshot(name="documentation_category_subforum_tag")
def test_numqueries_on_tags(self, client, db, django_assert_num_queries):
category_forum = CategoryForumFactory(with_public_perms=True)
ForumFactory.create_batch(
20, parent=category_forum, with_public_perms=True, with_tags=[f"tag{i}" for i in range(3)]
)
- with django_assert_num_queries(18):
+ # vincentporte TOBEFIXED : DUPLICATED QUERIES
+ with django_assert_num_queries(19):
client.get(category_forum.get_absolute_url())
diff --git a/lacommunaute/forum/urls.py b/lacommunaute/forum/urls.py
index 87b0abe4..77cd7c35 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 23e74434..c0914a8a 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
@@ -25,7 +26,32 @@
PermissionRequiredMixin = get_class("forum_permission.viewmixins", "PermissionRequiredMixin")
-class ForumView(BaseForumView, FilteredTopicsListViewMixin):
+class SubCategoryForumListMixin:
+ def get_descendants(self):
+ qs = self.get_forum().get_descendants()
+
+ forum_tag = self.request.GET.get("forum_tag") or None
+ if forum_tag:
+ qs = qs.filter(tags__slug=forum_tag)
+
+ return qs.prefetch_related("tags")
+
+ def get_tags_of_descendants(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_tag_context(self):
+ return {
+ # TODO : remove permission management, though all forums are public in our case
+ "sub_forums": forum_visibility_content_tree_from_forums(self.request, self.get_descendants()),
+ "tags_of_descendants": self.get_tags_of_descendants(),
+ "active_forum_tag_slug": self.request.GET.get("forum_tag") or None,
+ }
+
+
+class ForumView(BaseForumView, FilteredTopicsListViewMixin, SubCategoryForumListMixin):
paginate_by = settings.FORUM_TOPICS_NUMBER_PER_PAGE
def get_template_names(self):
@@ -46,15 +72,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_context_data(self, **kwargs):
forum = self.get_forum()
@@ -84,8 +101,8 @@ 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"] = forum_visibility_content_tree_from_forums(self.request, self.get_descendants())
+ if self.will_render_documentation_category_variant():
+ context = context | self.forum_tag_context()
if self.will_render_documentation_variant():
context["sibling_forums"] = forum.get_siblings(include_self=True)
@@ -95,6 +112,14 @@ def get_context_data(self, **kwargs):
return context
+class SubCategoryForumListView(BaseForumView, SubCategoryForumListMixin):
+ template_name = "forum/partials/subcategory_forum_list.html"
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs) | self.forum_tag_context()
+ return context
+
+
class ForumUpdateView(UserPassesTestMixin, UpdateView):
template_name = "forum/forum_create_or_update.html"
form_class = ForumForm
diff --git a/lacommunaute/partner/tests/__snapshots__/tests_partner_detailview.ambr b/lacommunaute/partner/tests/__snapshots__/tests_partner_detailview.ambr
index 5f9c4639..bd6e7761 100644
--- a/lacommunaute/partner/tests/__snapshots__/tests_partner_detailview.ambr
+++ b/lacommunaute/partner/tests/__snapshots__/tests_partner_detailview.ambr
@@ -238,24 +238,36 @@
Leurs contributions dans la communauté
-
+
-
-
-
-
-
Test Forum
-
sunmoon
-
Test description
-
-
diff --git a/lacommunaute/templates/forum/forum_documentation_category.html b/lacommunaute/templates/forum/forum_documentation_category.html
index 0be0c3f9..e12bfabe 100644
--- a/lacommunaute/templates/forum/forum_documentation_category.html
+++ b/lacommunaute/templates/forum/forum_documentation_category.html
@@ -1,6 +1,6 @@
{% extends "forum/forum_detail.html" %}
{% block subforum_list %}
- {% include "forum/partials/category_subforums_list.html" with sub_forums=sub_forums only %}
+ {% include "forum/partials/subcategory_forum_list.html" with forum=forum sub_forums=sub_forums tags_of_descendants=tags_of_descendants 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/category_subforums_list.html b/lacommunaute/templates/forum/partials/category_subforums_list.html
deleted file mode 100644
index 1589cd86..00000000
--- a/lacommunaute/templates/forum/partials/category_subforums_list.html
+++ /dev/null
@@ -1,34 +0,0 @@
-
- {% for node in sub_forums.top_nodes %}
-
-
- {% if node.obj.image %}
-
- {% endif %}
-
-
{{ node.obj.name }}
- {% spaceless %}
- {% for tag in node.obj.tags.all %}
- {% if forloop.first %}
{% endif %}
- {% include "partials/tag.html" with tag=tag only %}
- {% if forloop.last %}
{% endif %}
- {% empty %} {% comment %} no div {% endcomment %}
- {% endfor %}
- {% endspaceless %}
- {% if node.obj.short_description %}
{{ node.obj.short_description }}
{% endif %}
-
-
-
-
- {% endfor %}
-
diff --git a/lacommunaute/templates/forum/partials/forum_tags.html b/lacommunaute/templates/forum/partials/forum_tags.html
new file mode 100644
index 00000000..4e3f3769
--- /dev/null
+++ b/lacommunaute/templates/forum/partials/forum_tags.html
@@ -0,0 +1,32 @@
+{% for tag in tags %}
+ {% if forum %}
+ {% if tag.slug == active_forum_tag_slug %}
+
+ {% else %}
+
+ {% endif %}
+ {% else %}
+
{{ tag.name }}
+ {% endif %}
+{% endfor %}
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 00000000..f29c589c
--- /dev/null
+++ b/lacommunaute/templates/forum/partials/subcategory_forum_list.html
@@ -0,0 +1,37 @@
+
+ {% if tags_of_descendants %}
+
+
Afficher les fiches contenant l'étiquette
+
+ {% include "forum/partials/forum_tags.html" with forum=forum tags=tags_of_descendants active_forum_tag_slug=active_forum_tag_slug only %}
+
+
+ {% endif %}
+
+ {% for node in sub_forums.top_nodes %}
+
+
+ {% if node.obj.image %}
+
+ {% endif %}
+
+
{{ node.obj.name }}
+
{% include "forum/partials/forum_tags.html" with tags=node.obj.tags.all only %}
+ {% if node.obj.short_description %}
{{ node.obj.short_description }}
{% endif %}
+
+
+
+
+ {% endfor %}
+
+
diff --git a/lacommunaute/templates/forum_conversation/partials/topic_tags.html b/lacommunaute/templates/forum_conversation/partials/topic_tags.html
index f3b86cad..1f3cb861 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 4f28d911..00000000
--- a/lacommunaute/templates/partials/tag.html
+++ /dev/null
@@ -1,5 +0,0 @@
-{% if url %}
-
{{ tag.name }}
-{% else %}
-
{{ tag.name }}
-{% endif %}
diff --git a/lacommunaute/templates/partner/detail.html b/lacommunaute/templates/partner/detail.html
index 1625c19c..a9c3a0bc 100644
--- a/lacommunaute/templates/partner/detail.html
+++ b/lacommunaute/templates/partner/detail.html
@@ -43,7 +43,7 @@
{{ partner.short_description }}
Leurs contributions dans la communauté
-
{% include "forum/partials/category_subforums_list.html" with sub_forums=sub_forums only %}
+
{% include "forum/partials/subcategory_forum_list.html" with sub_forums=sub_forums only %}
{% endif %}
diff --git a/lacommunaute/utils/testing.py b/lacommunaute/utils/testing.py
index b60ab81e..8757a714 100644
--- a/lacommunaute/utils/testing.py
+++ b/lacommunaute/utils/testing.py
@@ -43,7 +43,7 @@ def parse_response_to_soup(response, selector=None, no_html_body=False, replace_
)
for replacement in replace_in_href
]
- for attr in ["href", "hx-post"]:
+ for attr in ["href", "hx-post", "hx-get"]:
for links in soup.find_all(attrs={attr: True}):
[links.attrs.update({attr: links.attrs[attr].replace(*replacement)}) for replacement in replacements]
if replace_img_src:
diff --git a/lacommunaute/utils/tests/tests_utils.py b/lacommunaute/utils/tests/tests_utils.py
index 281648ed..b81f0ff5 100644
--- a/lacommunaute/utils/tests/tests_utils.py
+++ b/lacommunaute/utils/tests/tests_utils.py
@@ -632,6 +632,24 @@ def test_replace_in_hxpost(self):
"