Skip to content

Commit

Permalink
feat(tags): filtrer les forum enfants à partir des tags (#750)
Browse files Browse the repository at this point in the history
## Description

🎸 Réduire la liste des `Forum` enfants affichés en filtrant sur leurs
tags

suite #746 

## Type de changement

🎢 Nouvelle fonctionnalité (changement non cassant qui ajoute une
fonctionnalité).

### Points d'attention

🦺 Affichage des enfants, les petits-enfants ne sont pas considérés,
qu'il possède le tag ou non

### Captures d'écran (optionnel)


![image](https://github.com/user-attachments/assets/6042fdf1-90e6-4e20-abd9-829bab8e8d0f)
  • Loading branch information
vincentporte authored Aug 26, 2024
1 parent 7f435bb commit eb6683a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,39 @@ def test_documentation_category_foot_content(
assert len(add_documentation_control) == 1
assert str(add_documentation_control[0]) == snapshot(name="documentation_category_add_file_control")

def test_filter_subforums_on_tags(self, client, db):
tags = [faker.word() for _ in range(3)]
category_forum = CategoryForumFactory(with_public_perms=True)
first_child = ForumFactory(parent=category_forum, with_public_perms=True, with_tags=[tags[0]])
second_child = ForumFactory(parent=category_forum, with_public_perms=True, with_tags=[tags[0], tags[1]])
third_child = ForumFactory(parent=category_forum, with_public_perms=True, with_tags=[tags[2]])
# forum without tags
ForumFactory(parent=category_forum, with_public_perms=True)

# edge case: grand_child is filtered out. No actual use case to display them in the subforum list
ForumFactory(parent=third_child, with_public_perms=True, with_tags=[tags[2]])

# no filter
response = client.get(category_forum.get_absolute_url())
assert response.status_code == 200
assert [node.obj for node in response.context_data["sub_forums"].top_nodes] == list(
category_forum.get_children()
)

# filter on first tag
response = client.get(category_forum.get_absolute_url() + f"?forum_tags={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]}")
assert response.status_code == 200
assert set([node.obj for node in response.context_data["sub_forums"].top_nodes]) == set(
[second_child, third_child]
)


@pytest.fixture(name="discussion_area_forum")
def discussion_area_forum_fixture():
Expand Down
15 changes: 15 additions & 0 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
logger = logging.getLogger(__name__)

PermissionRequiredMixin = get_class("forum_permission.viewmixins", "PermissionRequiredMixin")
ForumVisibilityContentTree = get_class("forum.visibility", "ForumVisibilityContentTree")


class ForumView(BaseForumView, FilteredTopicsListViewMixin):
Expand All @@ -46,6 +47,12 @@ 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):
forum_tags = self.request.GET.get("forum_tags")
if forum_tags:
return self.get_forum().get_descendants().filter(tags__slug__in=forum_tags.split(","))
return self.get_forum().get_descendants()

def get_context_data(self, **kwargs):
forum = self.get_forum()

Expand Down Expand Up @@ -75,6 +82,14 @@ 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_variant():
context["sibling_forums"] = forum.get_siblings(include_self=True)

Expand Down

0 comments on commit eb6683a

Please sign in to comment.