mirror of
https://github.com/ae-utbm/sith.git
synced 2026-11-27 19:32:48 +00:00
feat: responsibility ban
This commit is contained in:
1 parent
df92ab39c0
commit
96800a249d
8 files changed
+99
-12
No files matched your search
+30
-2
@@ -25,6 +25,7 @@ import itertools
|
||||
from operator import attrgetter
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.db.models import Exists, OuterRef, Q, QuerySet
|
||||
from django.db.models.functions import Lower
|
||||
from django.forms.models import ModelChoiceField, ModelChoiceIterator
|
||||
@@ -182,7 +183,12 @@ class ClubOldMemberForm(forms.Form):
|
||||
|
||||
|
||||
class ClubMemberForm(forms.ModelForm):
|
||||
"""Form to add a member to the club, as a board member."""
|
||||
"""Base form to create a Membership.
|
||||
|
||||
Two subclasses exist :
|
||||
- one for adding any user to a club, that is shown to board members
|
||||
- the other for users without privileges to join the club as simple members.
|
||||
"""
|
||||
|
||||
error_css_class = "error"
|
||||
required_css_class = "required"
|
||||
@@ -247,9 +253,29 @@ class ClubAddMemberForm(ClubMemberForm):
|
||||
)
|
||||
return user
|
||||
|
||||
def clean(self):
|
||||
res = super().clean()
|
||||
if self.errors:
|
||||
return res
|
||||
user, role = self.cleaned_data["user"], self.cleaned_data["role"]
|
||||
if (
|
||||
role.is_board
|
||||
and user.ban_groups.filter(
|
||||
id=settings.SITH_GROUP_BANNED_RESPONSIBILITY_ID
|
||||
).exists()
|
||||
):
|
||||
raise forms.ValidationError(
|
||||
_("This user is banned from being in the board of a club"),
|
||||
code="banned",
|
||||
)
|
||||
return res
|
||||
|
||||
|
||||
class JoinClubForm(ClubMemberForm):
|
||||
"""Form to join a club."""
|
||||
"""Form to join a club.
|
||||
|
||||
Only member roles can be obtained through this form.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, club: Club, request_user: User, **kwargs):
|
||||
super().__init__(*args, club=club, request_user=request_user, **kwargs)
|
||||
@@ -261,6 +287,8 @@ class JoinClubForm(ClubMemberForm):
|
||||
|
||||
def clean(self):
|
||||
"""Check that the user is subscribed and isn't already in the club."""
|
||||
# note: we don't check here if users are ban from joining a club board,
|
||||
# because this form manages only member roles.
|
||||
if not self.request_user.is_subscribed:
|
||||
raise forms.ValidationError(
|
||||
_("You must be subscribed to join a club"), code="invalid"
|
||||
|
||||
+12
-6
@@ -421,18 +421,24 @@ class MembershipQuerySet(models.QuerySet):
|
||||
"""Remove users from club groups they are no more in
|
||||
and add them in the club groups they should be in.
|
||||
|
||||
Be aware that this adds three db queries :
|
||||
Be aware that this adds four db queries (on top of the UPDATE query) :
|
||||
|
||||
- one to retrieve the updated memberships
|
||||
- two to retrieve the updated memberships
|
||||
- one to perform group removal
|
||||
- and one to perform group attribution.
|
||||
"""
|
||||
nb_rows = super().update(**kwargs)
|
||||
if nb_rows == 0:
|
||||
# if no row was affected, no need to edit club groups
|
||||
memberships = set(self.values_list("id", flat=True))
|
||||
if not memberships:
|
||||
# if there is no selected membership,there is nothing to do
|
||||
return 0
|
||||
nb_rows = super().update(**kwargs)
|
||||
|
||||
memberships = set(self.select_related("club"))
|
||||
# re-fetch the updated objects, with their new state.
|
||||
memberships = set(
|
||||
Membership.objects.filter(id__in=memberships)
|
||||
.select_related("club", "role")
|
||||
.prefetch_related("role__linked_groups")
|
||||
)
|
||||
# delete all User-Group relations and recreate the necessary ones
|
||||
Membership._remove_club_groups(memberships)
|
||||
Membership._add_club_groups(memberships)
|
||||
|
||||
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
from bs4 import BeautifulSoup
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.core.cache import cache
|
||||
from django.db.models import Max
|
||||
@@ -17,7 +18,7 @@ from club.forms import ClubAddMemberForm, JoinClubForm
|
||||
from club.models import Club, ClubRole, Membership
|
||||
from club.tests.base import TestClub
|
||||
from core.baker_recipes import subscriber_user
|
||||
from core.models import AnonymousUser, Group, User
|
||||
from core.models import AnonymousUser, Group, User, UserBan
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable
|
||||
@@ -386,6 +387,25 @@ class TestMembership(TestClub):
|
||||
"user": ["Vous ne pouvez pas ajouter deux fois le même utilisateur"]
|
||||
}
|
||||
|
||||
def test_add_banned_user(self):
|
||||
user = subscriber_user.make()
|
||||
baker.make(
|
||||
UserBan,
|
||||
ban_group_id=settings.SITH_GROUP_BANNED_RESPONSIBILITY_ID,
|
||||
user=user,
|
||||
)
|
||||
form = ClubAddMemberForm(
|
||||
data={"user": user, "role": self.board_role},
|
||||
request_user=self.root,
|
||||
club=self.club,
|
||||
)
|
||||
assert not form.is_valid()
|
||||
assert form.errors == {
|
||||
"__all__": [
|
||||
"Cette personne a l'interdiction de rejoindre le bureau d'un club."
|
||||
]
|
||||
}
|
||||
|
||||
def test_add_other_member_forbidden(self):
|
||||
non_member = subscriber_user.make()
|
||||
simple_member = baker.make(
|
||||
|
||||
Reference in new issue
Block a user