also forbid subscribers, old subscribers and public

This commit is contained in:
imperosol committed 2026-12-02 13:47:55 +01:00
1 parent c219958e48
commit 2c19e7b3fe
3 files changed
+47 -20

No files matched your search

+38 -17
View File
@@ -4,7 +4,9 @@ from pathlib import Path
from typing import TYPE_CHECKING, Any
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.utils.timezone import get_current_timezone
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext_lazy
@@ -18,7 +20,6 @@ from sas.models import Album, Picture, PictureModerationRequest
from sas.widgets.ajax_select import AutoCompleteSelectAlbum
if TYPE_CHECKING:
from django.db.models import QuerySet
from django.db.models.fields.files import FieldFile
from core.models import Group, User
@@ -87,23 +88,43 @@ class AlbumEditForm(forms.ModelForm):
return f
def clean_edit_groups(self):
groups: QuerySet[Group] = self.cleaned_data["edit_groups"]
member_groups = list(groups.exclude(club=None).values_list("name", flat=True))
if member_groups:
raise ValidationError(
ngettext_lazy(
"You cannot grant the edit rights "
"to the %(groups)s group. "
"It is a club simple member group, which means anyone "
"could become an editor by merely joining the club.",
"You cannot grant the edit rights "
"to the following groups : %(groups)s. "
"They are club simple member groups, which means anyone "
"could become an editor by merely joining one of the clubs.",
number=len(member_groups),
)
% {"groups": ", ".join(member_groups)}
groups: list[Group] = list(
self.cleaned_data["edit_groups"].annotate(
is_club_group=Q(club__isnull=False)
)
)
member_groups = [g.name for g in groups if g.is_club_group]
if member_groups:
self.add_error(
"edit_groups",
ValidationError(
ngettext_lazy(
"You cannot grant the edit rights "
"to the %(groups)s group. "
"It is a club simple member group, which means anyone "
"could become an editor by merely joining the club.",
"You cannot grant the edit rights "
"to the following groups : %(groups)s. "
"They are club simple member groups, which means anyone "
"could become an editor by merely joining one of the clubs.",
number=len(member_groups),
)
% {"groups": ", ".join(member_groups)}
),
)
for group in groups:
if group.id in [
settings.SITH_GROUP_SUBSCRIBERS_ID,
settings.SITH_GROUP_OLD_SUBSCRIBERS_ID,
settings.SITH_GROUP_PUBLIC_ID,
]:
self.add_error(
"edit_groups",
ValidationError(
_("%(group)s : this group cannot get album edition rights.")
% {"group": group.name}
),
)
return groups
def save(self, commit=True): # noqa: FBT002
+3 -2
View File
@@ -111,7 +111,7 @@ def test_club_member_groups_cannot_be_edit_groups(album: Album):
"name": album.name,
"parent": album.pk,
"date": localdate(),
"edit_groups": [club.members_group_id],
"edit_groups": [club.members_group_id, settings.SITH_GROUP_SUBSCRIBERS_ID],
}
form = AlbumEditForm(data=data)
assert not form.is_valid()
@@ -121,7 +121,8 @@ def test_club_member_groups_cannot_be_edit_groups(album: Album):
f"d'édition au groupe {club.members_group.name}. "
"C'est un groupe de simples membres de club, "
"ce qui signifie que n'importe qui pourrait devenir éditeur "
"simplement en rejoignant le club."
"simplement en rejoignant le club.",
"Cotisants : ce groupe ne peut pas recevoir le droit d'éditer un album.",
]
}