Skip to content

Commit

Permalink
Déplace get_authorized_forums() vers zds/forum/utils.py (#6522)
Browse files Browse the repository at this point in the history
- ce n'est pas (plus ?) un templatetag
- renommage en get_authorized_forums_pk() pour être plus explicite sur ce qui est retourné
- ajout de tests
  • Loading branch information
philippemilink authored Aug 26, 2023
1 parent 46b253e commit aaacd06
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
31 changes: 31 additions & 0 deletions zds/forum/tests/tests_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.contrib.auth.models import Group
from django.test import TestCase

from zds.forum.tests.factories import create_category_and_forum
from zds.forum.utils import get_authorized_forums_pk
from zds.member.tests.factories import ProfileFactory, StaffProfileFactory


class GetAuthorizedForumsTests(TestCase):
def test_get_authorized_forums_pk(self):
user = ProfileFactory().user
staff = StaffProfileFactory().user

# 1. Create a hidden forum belonging to a hidden staff group:
group = Group.objects.create(name="Les illuminatis anonymes de ZdS")
_, hidden_forum = create_category_and_forum(group)

staff.groups.add(group)
staff.save()

# 2. Create a public forum:
_, public_forum = create_category_and_forum()

# 3. Regular user can access only the public forum:
self.assertEqual(get_authorized_forums_pk(user), [public_forum.pk])

# 4. Staff user can access all forums:
self.assertEqual(sorted(get_authorized_forums_pk(staff)), sorted([hidden_forum.pk, public_forum.pk]))

# 5. By default, only public forums are available:
self.assertEqual(get_authorized_forums_pk(None), [public_forum.pk])
19 changes: 18 additions & 1 deletion zds/forum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.views.generic import CreateView
from django.views.generic.detail import SingleObjectMixin
from django.utils.translation import gettext as _
from zds.forum.models import Topic, Post
from zds.forum.models import Forum, Topic, Post
from zds.member.views import get_client_ip
from zds.utils.misc import contains_utf8mb4
from zds.utils.mixins import QuoteMixin
Expand Down Expand Up @@ -198,3 +198,20 @@ def post(self, request, *args, **kwargs):

def create_forum(self, form_class, **kwargs):
raise NotImplementedError("`create_forum()` must be implemented.")


def get_authorized_forums_pk(user):
"""
Find forums the user is allowed to visit.
:param user: concerned user.
:return: pk of authorized forums
"""
forums_pub = Forum.objects.filter(groups__isnull=True).all()
if user and user.is_authenticated:
forums_private = Forum.objects.filter(groups__isnull=False, groups__in=user.groups.all()).all()
list_forums = list(forums_pub | forums_private)
else:
list_forums = list(forums_pub)

return [f.pk for f in list_forums]
8 changes: 4 additions & 4 deletions zds/searchv2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from zds.searchv2.forms import SearchForm
from zds.searchv2.models import ESIndexManager
from zds.utils.paginator import ZdSPagingListView
from zds.utils.templatetags.authorized_forums import get_authorized_forums
from zds.forum.utils import get_authorized_forums_pk
from functools import reduce


Expand All @@ -38,7 +38,7 @@ def get(self, request, *args, **kwargs):

results = []
if self.index_manager.connected_to_es and self.search_query:
self.authorized_forums = get_authorized_forums(self.request.user)
self.authorized_forums = get_authorized_forums_pk(self.request.user)

search_queryset = Search()
query = (
Expand Down Expand Up @@ -90,7 +90,7 @@ def get(self, request, *args, **kwargs):
excluded_content_ids = request.GET.get("excluded", "").split(",")
results = []
if self.index_manager.connected_to_es and self.search_query:
self.authorized_forums = get_authorized_forums(self.request.user)
self.authorized_forums = get_authorized_forums_pk(self.request.user)

search_queryset = Search()
if len(excluded_content_ids) > 0 and excluded_content_ids != [""]:
Expand Down Expand Up @@ -174,7 +174,7 @@ def get_queryset(self):

if self.search_query:
# Searches forums the user is allowed to visit
self.authorized_forums = get_authorized_forums(self.request.user)
self.authorized_forums = get_authorized_forums_pk(self.request.user)

search_queryset = Search()

Expand Down
18 changes: 0 additions & 18 deletions zds/utils/templatetags/authorized_forums.py

This file was deleted.

0 comments on commit aaacd06

Please sign in to comment.