diff --git a/counter/forms.py b/counter/forms.py index d80466e8..0ca6da0a 100644 --- a/counter/forms.py +++ b/counter/forms.py @@ -3,7 +3,7 @@ import math import uuid from collections import defaultdict from datetime import date, datetime, timezone -from typing import TYPE_CHECKING, ClassVar +from typing import TYPE_CHECKING, ClassVar, LiteralString from dateutil.relativedelta import relativedelta from django import forms @@ -19,7 +19,7 @@ from phonenumber_field.widgets import RegionalPhoneNumberWidget from club.models import Club from club.widgets.ajax_select import AutoCompleteSelectClub -from core.models import User, UserQuerySet +from core.models import User, UserBan, UserQuerySet from core.views import LoginForm from core.views.forms import ( FutureDateTimeField, @@ -257,6 +257,41 @@ class CounterEditForm(forms.ModelForm): countersellers__is_regular=False ).all() + def clean_sellers(self, field: LiteralString): + sellers = self.cleaned_data[field] + bans = [ + settings.SITH_GROUP_BANNED_COUNTER_ID, + settings.SITH_GROUP_BANNED_SUBSCRIPTION_ID, + ] + invalid_sellers = sellers.annotate( + is_banned=Exists( + UserBan.objects.filter(user=OuterRef("pk"), ban_group__in=bans) + ) + ).filter(Q(is_banned=True) | Q(date_of_birth=None)) + for seller in invalid_sellers: + if seller.is_banned: + self.add_error( + field, + ValidationError( + _("%(user)s is banned.") % {"user": seller.get_full_name()} + ), + ) + if not seller.date_of_birth: + self.add_error( + field, + ValidationError( + _("%(user)s has not filled its birthdate.") + % {"user": seller.get_full_name()} + ), + ) + return sellers + + def clean_sellers_regular(self): + return self.clean_sellers("sellers_regular") + + def clean_sellers_temporary(self): + return self.clean_sellers("sellers_temporary") + def clean(self): regular: UserQuerySet = self.cleaned_data["sellers_regular"] temporary: UserQuerySet = self.cleaned_data["sellers_temporary"] diff --git a/counter/tests/test_counter_admin.py b/counter/tests/test_counter_admin.py index 91832786..34834a66 100644 --- a/counter/tests/test_counter_admin.py +++ b/counter/tests/test_counter_admin.py @@ -1,3 +1,6 @@ +from datetime import timedelta + +from dateutil.utils import today from django.conf import settings from django.contrib.auth.models import Permission from django.test import TestCase @@ -6,7 +9,7 @@ from model_bakery import baker from club.models import Membership from core.baker_recipes import subscriber_user -from core.models import Group, User +from core.models import Group, User, UserBan from counter.baker_recipes import product_recipe from counter.forms import CounterEditForm from counter.models import Counter, CounterSellers @@ -18,7 +21,11 @@ class TestEditCounterSellers(TestCase): cls.counter = baker.make(Counter, type="BAR") cls.products = product_recipe.make(_quantity=2, _bulk_create=True) cls.counter.products.add(*cls.products) - users = subscriber_user.make(_quantity=6, _bulk_create=True) + users = subscriber_user.make( + date_of_birth=today() - timedelta(days=365 * 20), + _quantity=6, + _bulk_create=True, + ) cls.regular_barmen = users[:2] cls.tmp_barmen = users[2:4] cls.not_barmen = users[4:] @@ -128,6 +135,54 @@ class TestEditCounterSellers(TestCase): self.counter.sellers.filter(countersellers__is_regular=False) ) == set(self.tmp_barmen) + def test_barman_without_birthdate(self): + """Test that users without a birthdate cannot be sellers.""" + user = subscriber_user.make() + form = CounterEditForm( + data={ + "sellers_regular": [*self.regular_barmen, user], + "sellers_temporary": self.tmp_barmen, + "products": self.products, + }, + instance=self.counter, + user=self.operator, + ) + assert not form.is_valid() + assert form.errors == { + "sellers_regular": [ + f"{user.get_full_name()} n'a pas renseigné sa date de naissance." + ] + } + + def test_banned_barman_forbidden(self): + """Test that users with a counter ban cannot be sellers.""" + UserBan.objects.create( + user=self.not_barmen[0], + ban_group_id=settings.SITH_GROUP_BANNED_COUNTER_ID, + reason="ban comptoir", + ) + UserBan.objects.create( + user=self.not_barmen[1], + ban_group_id=settings.SITH_GROUP_BANNED_SUBSCRIPTION_ID, + reason="ban comptoir", + ) + form = CounterEditForm( + data={ + "sellers_regular": [*self.regular_barmen, *self.not_barmen[0:2]], + "sellers_temporary": self.tmp_barmen, + "products": self.products, + }, + instance=self.counter, + user=self.operator, + ) + assert not form.is_valid() + assert form.errors == { + "sellers_regular": [ + f"{self.not_barmen[0].get_full_name()} est banni.", + f"{self.not_barmen[1].get_full_name()} est banni.", + ] + } + class TestEditCounterProducts(TestCase): @classmethod diff --git a/counter/tests/test_customer.py b/counter/tests/test_customer.py index ca1b2b85..f1d6b0a6 100644 --- a/counter/tests/test_customer.py +++ b/counter/tests/test_customer.py @@ -145,7 +145,7 @@ class TestStudentCard(TestCase): self.login_in_counter() barman = subscriber_user.make() - self.counter.sellers.add(barman) + self.counter.sellers.add(barman, through_defaults={"is_regular": True}) # We want to test sending requests from another counter while # we are currently registered to another counter # so we connect to a counter and diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index 913708cf..d595bbac 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-11-06 17:22+0100\n" +"POT-Creation-Date: 2026-11-10 13:16+0100\n" "PO-Revision-Date: 2016-07-18\n" "Last-Translator: Maréchal \n" @@ -3257,6 +3257,16 @@ msgstr "" "Si vous souhaitez ajouter sur ce comptoir un produit qui n'appartient pas à " "votre club, vous devriez demander à un admin." +#: counter/forms.py +#, python-format +msgid "%(user)s is banned." +msgstr "%(user)s est banni." + +#: counter/forms.py +#, python-format +msgid "%(user)s has not filled its birthdate." +msgstr "%(user)s n'a pas renseigné sa date de naissance." + #: counter/forms.py #, python-format msgid ""