end barmanship on ban

This commit is contained in:
imperosol committed 2026-11-10 20:21:50 +01:00
1 parent 079ec8a8e5
commit e45b4eb9e0
2 files changed
+30 -5

No files matched your search

+8
View File
@@ -769,6 +769,14 @@ class UserBan(models.Model):
def __str__(self): def __str__(self):
return f"Ban of user {self.user.id}" return f"Ban of user {self.user.id}"
def save(self, *args, **kwargs):
if self.ban_group_id in [
settings.SITH_GROUP_BANNED_COUNTER_ID,
settings.SITH_GROUP_BANNED_SUBSCRIPTION_ID,
]:
self.user.counters.clear()
return super().save(*args, **kwargs)
class GlobalPermissionRights(models.Model): class GlobalPermissionRights(models.Model):
"""Little hack to have permissions not linked to a specific db table.""" """Little hack to have permissions not linked to a specific db table."""
+22 -5
View File
@@ -8,7 +8,10 @@ from django.utils.timezone import localtime
from model_bakery import baker from model_bakery import baker
from pytest_django.asserts import assertRedirects from pytest_django.asserts import assertRedirects
from core.baker_recipes import subscriber_user
from core.models import BanGroup, User, UserBan from core.models import BanGroup, User, UserBan
from counter.models import Counter
from sith import settings
if TYPE_CHECKING: if TYPE_CHECKING:
from django.test import Client from django.test import Client
@@ -33,11 +36,7 @@ def test_ban_user(client: Client, operator: User, expires_at: datetime):
client.force_login(operator) client.force_login(operator)
user = baker.make(User) user = baker.make(User)
ban_group = BanGroup.objects.first() ban_group = BanGroup.objects.first()
data = { data = {"user": user.id, "ban_group": ban_group.id, "reason": "Being naughty"}
"user": user.id,
"ban_group": ban_group.id,
"reason": "Being naughty",
}
if expires_at is not None: if expires_at is not None:
data["expires_at"] = expires_at.strftime("%Y-%m-%d %H:%M") data["expires_at"] = expires_at.strftime("%Y-%m-%d %H:%M")
response = client.post(reverse("rootplace:ban_create"), data) response = client.post(reverse("rootplace:ban_create"), data)
@@ -49,6 +48,24 @@ def test_ban_user(client: Client, operator: User, expires_at: datetime):
assert bans[0].ban_group == ban_group assert bans[0].ban_group == ban_group
@pytest.mark.django_db
def test_ban_counter_ends_barmanship():
user = subscriber_user.make()
counter = baker.make(Counter)
counter.sellers.add(user, through_defaults={"is_regular": True})
UserBan.objects.create(
user=user,
ban_group_id=settings.SITH_GROUP_BANNED_SUBSCRIPTION_ID,
reason="ban comptoir",
)
# check that we did effectively delete the barmanship
# and not the bar itself (better safe than sorry)
assert Counter.objects.filter(id=counter.id).exists()
assert not user.counters.exists()
@pytest.mark.django_db @pytest.mark.django_db
def test_remove_ban(client: Client, operator: User): def test_remove_ban(client: Client, operator: User):
client.force_login(operator) client.force_login(operator)