From e4d19c58df43beafad371a5c00c4b19723b4dbe4 Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Sun, 30 Jul 2023 14:04:33 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9place=20get=5Fauthorized=5Fforums()=20ve?= =?UTF-8?q?rs=20zds/forum/utils.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- zds/forum/tests/tests_utils.py | 31 +++++++++++++++++++++ zds/forum/utils.py | 19 ++++++++++++- zds/searchv2/views.py | 8 +++--- zds/utils/templatetags/authorized_forums.py | 18 ------------ 4 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 zds/forum/tests/tests_utils.py delete mode 100644 zds/utils/templatetags/authorized_forums.py diff --git a/zds/forum/tests/tests_utils.py b/zds/forum/tests/tests_utils.py new file mode 100644 index 0000000000..f0f03bf128 --- /dev/null +++ b/zds/forum/tests/tests_utils.py @@ -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]) diff --git a/zds/forum/utils.py b/zds/forum/utils.py index 353910605b..f24dfbd9b5 100644 --- a/zds/forum/utils.py +++ b/zds/forum/utils.py @@ -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 @@ -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] diff --git a/zds/searchv2/views.py b/zds/searchv2/views.py index e78c9f7c43..73beed8d19 100644 --- a/zds/searchv2/views.py +++ b/zds/searchv2/views.py @@ -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 @@ -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 = ( @@ -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 != [""]: @@ -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() diff --git a/zds/utils/templatetags/authorized_forums.py b/zds/utils/templatetags/authorized_forums.py deleted file mode 100644 index 8dcc78b39d..0000000000 --- a/zds/utils/templatetags/authorized_forums.py +++ /dev/null @@ -1,18 +0,0 @@ -from zds.forum.models import Forum - - -def get_authorized_forums(user): - """ - Find forums the user is allowed to visit. - - :param user: concerned user. - :return: 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]