mirror of
https://github.com/ae-utbm/sith.git
synced 2026-11-20 16:09:54 +00:00
forbid users with a ban or no birthdate to be barmen
This commit is contained in:
1 parent
bfa5fa45b8
commit
079ec8a8e5
4 files changed
+106
-6
No files matched your search
+37
-2
@@ -3,7 +3,7 @@ import math
|
|||||||
import uuid
|
import uuid
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from datetime import date, datetime, timezone
|
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 dateutil.relativedelta import relativedelta
|
||||||
from django import forms
|
from django import forms
|
||||||
@@ -19,7 +19,7 @@ from phonenumber_field.widgets import RegionalPhoneNumberWidget
|
|||||||
|
|
||||||
from club.models import Club
|
from club.models import Club
|
||||||
from club.widgets.ajax_select import AutoCompleteSelectClub
|
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 import LoginForm
|
||||||
from core.views.forms import (
|
from core.views.forms import (
|
||||||
FutureDateTimeField,
|
FutureDateTimeField,
|
||||||
@@ -257,6 +257,41 @@ class CounterEditForm(forms.ModelForm):
|
|||||||
countersellers__is_regular=False
|
countersellers__is_regular=False
|
||||||
).all()
|
).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):
|
def clean(self):
|
||||||
regular: UserQuerySet = self.cleaned_data["sellers_regular"]
|
regular: UserQuerySet = self.cleaned_data["sellers_regular"]
|
||||||
temporary: UserQuerySet = self.cleaned_data["sellers_temporary"]
|
temporary: UserQuerySet = self.cleaned_data["sellers_temporary"]
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from dateutil.utils import today
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
@@ -6,7 +9,7 @@ from model_bakery import baker
|
|||||||
|
|
||||||
from club.models import Membership
|
from club.models import Membership
|
||||||
from core.baker_recipes import subscriber_user
|
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.baker_recipes import product_recipe
|
||||||
from counter.forms import CounterEditForm
|
from counter.forms import CounterEditForm
|
||||||
from counter.models import Counter, CounterSellers
|
from counter.models import Counter, CounterSellers
|
||||||
@@ -18,7 +21,11 @@ class TestEditCounterSellers(TestCase):
|
|||||||
cls.counter = baker.make(Counter, type="BAR")
|
cls.counter = baker.make(Counter, type="BAR")
|
||||||
cls.products = product_recipe.make(_quantity=2, _bulk_create=True)
|
cls.products = product_recipe.make(_quantity=2, _bulk_create=True)
|
||||||
cls.counter.products.add(*cls.products)
|
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.regular_barmen = users[:2]
|
||||||
cls.tmp_barmen = users[2:4]
|
cls.tmp_barmen = users[2:4]
|
||||||
cls.not_barmen = users[4:]
|
cls.not_barmen = users[4:]
|
||||||
@@ -128,6 +135,54 @@ class TestEditCounterSellers(TestCase):
|
|||||||
self.counter.sellers.filter(countersellers__is_regular=False)
|
self.counter.sellers.filter(countersellers__is_regular=False)
|
||||||
) == set(self.tmp_barmen)
|
) == 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):
|
class TestEditCounterProducts(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ class TestStudentCard(TestCase):
|
|||||||
|
|
||||||
self.login_in_counter()
|
self.login_in_counter()
|
||||||
barman = subscriber_user.make()
|
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 want to test sending requests from another counter while
|
||||||
# we are currently registered to another counter
|
# we are currently registered to another counter
|
||||||
# so we connect to a counter and
|
# so we connect to a counter and
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
msgid ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Report-Msgid-Bugs-To: \n"
|
"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"
|
"PO-Revision-Date: 2016-07-18\n"
|
||||||
"Last-Translator: Maréchal <thomas.girod@utbm.fr\n"
|
"Last-Translator: Maréchal <thomas.girod@utbm.fr\n"
|
||||||
"Language-Team: AE info <ae.info@utbm.fr>\n"
|
"Language-Team: AE info <ae.info@utbm.fr>\n"
|
||||||
@@ -3257,6 +3257,16 @@ msgstr ""
|
|||||||
"Si vous souhaitez ajouter sur ce comptoir un produit qui n'appartient pas à "
|
"Si vous souhaitez ajouter sur ce comptoir un produit qui n'appartient pas à "
|
||||||
"votre club, vous devriez demander à un admin."
|
"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
|
#: counter/forms.py
|
||||||
#, python-format
|
#, python-format
|
||||||
msgid ""
|
msgid ""
|
||||||
|
|||||||
Reference in new issue
Block a user