put roles at the right place when they are created

This commit is contained in:
imperosol
2026-03-31 16:58:26 +02:00
parent 3c484778d8
commit 11779231e1
2 changed files with 47 additions and 0 deletions

View File

@@ -278,6 +278,7 @@ class ClubRole(OrderedModel):
) )
if ( if (
self.is_board self.is_board
and self.order
and self.club.roles.filter(is_board=False, order__lt=self.order).exists() and self.club.roles.filter(is_board=False, order__lt=self.order).exists()
): ):
errors.append( errors.append(
@@ -290,6 +291,19 @@ class ClubRole(OrderedModel):
raise ValidationError(errors) raise ValidationError(errors)
return super().clean() 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): class MembershipQuerySet(models.QuerySet):
def ongoing(self) -> Self: def ongoing(self) -> Self:

View 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,
]