mirror of
https://github.com/ae-utbm/sith.git
synced 2026-03-31 15:59:42 +00:00
put roles at the right place when they are created
This commit is contained in:
@@ -278,6 +278,7 @@ class ClubRole(OrderedModel):
|
||||
)
|
||||
if (
|
||||
self.is_board
|
||||
and self.order
|
||||
and self.club.roles.filter(is_board=False, order__lt=self.order).exists()
|
||||
):
|
||||
errors.append(
|
||||
@@ -290,6 +291,19 @@ class ClubRole(OrderedModel):
|
||||
raise ValidationError(errors)
|
||||
return super().clean()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
auto_order = self.order is None and self.is_board
|
||||
if not auto_order:
|
||||
super().save(*args, **kwargs)
|
||||
return
|
||||
filters = Q(is_board=False)
|
||||
if self.is_presidency:
|
||||
filters |= Q(is_board=True, is_presidency=False)
|
||||
next_role = self.club.roles.filter(filters).order_by("order").first()
|
||||
super().save(*args, **kwargs)
|
||||
if next_role:
|
||||
self.above(next_role)
|
||||
|
||||
|
||||
class MembershipQuerySet(models.QuerySet):
|
||||
def ongoing(self) -> Self:
|
||||
|
||||
33
club/tests/test_clubrole.py
Normal file
33
club/tests/test_clubrole.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import pytest
|
||||
from model_bakery import baker, seq
|
||||
from model_bakery.recipe import Recipe
|
||||
|
||||
from club.models import Club, ClubRole
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_order_auto():
|
||||
"""Test that newly created roles are put in the right place."""
|
||||
club = baker.make(Club)
|
||||
recipe = Recipe(ClubRole, club=club, name=seq("role "))
|
||||
# bulk create initial roles
|
||||
roles = recipe.make(
|
||||
is_board=iter([True, True, False]),
|
||||
is_presidency=iter([True, False, False]),
|
||||
order=iter([1, 2, 3]),
|
||||
_quantity=3,
|
||||
_bulk_create=True,
|
||||
)
|
||||
# then create the remaining roles one by one (like they will be in prod)
|
||||
# each new role should be placed at the end of its category
|
||||
role_a = recipe.make(is_board=True, is_presidency=True, order=None)
|
||||
role_b = recipe.make(is_board=True, is_presidency=False, order=None)
|
||||
role_c = recipe.make(is_board=False, is_presidency=False, order=None)
|
||||
assert list(club.roles.order_by("order")) == [
|
||||
roles[0],
|
||||
role_a,
|
||||
roles[1],
|
||||
role_b,
|
||||
roles[2],
|
||||
role_c,
|
||||
]
|
||||
Reference in New Issue
Block a user