diff --git a/server/bingo/admin.py b/server/bingo/admin.py index e3d242e..0b4f10e 100644 --- a/server/bingo/admin.py +++ b/server/bingo/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin from django.contrib.auth.models import Group +from django.forms import ModelForm, ValidationError from .models import User, Challenge, Friendship, BingoGrid, TileInteraction @@ -44,8 +45,26 @@ class FriendshipAdmin(admin.ModelAdmin): fields = list_display +class BingoGridAdminForm(ModelForm): + class Meta: + model = BingoGrid + fields = ('grid_id', 'challenges', 'is_active') + + def clean_challenges(self): + challenges = self.cleaned_data['challenges'] + if len(challenges) != 16: + raise ValidationError( + "BingoGrid must have exactly 16 challenges." + ) + return challenges + + @admin.register(BingoGrid) class BingoGridAdmin(admin.ModelAdmin): + + # use the above form + form = BingoGridAdminForm + # disable the export2csv action def get_actions(self, request): actions = super().get_actions(request) diff --git a/server/bingo/models.py b/server/bingo/models.py index 7fa57c8..5c052a7 100644 --- a/server/bingo/models.py +++ b/server/bingo/models.py @@ -180,16 +180,6 @@ class BingoGrid(models.Model): is_active = models.BooleanField(default=False) def clean(self): - # Ensure exactly 16 challenges - # This only makes sense if the object is saved at least once (has a PK). - # If it's brand new, you won't have the M2M relationships set until after save. - if self.pk: - if self.challenges.count() != 16: - raise ValidationError( - f"BingoGrid must have exactly 16 challenges (found { - self.challenges.count()})." - ) - # Ensure only one active BingoGrid if self.is_active: active_count = BingoGrid.objects.filter( diff --git a/server/bingo/tests/test_bingogrid.py b/server/bingo/tests/test_bingogrid.py index 100ad51..6e0d72b 100644 --- a/server/bingo/tests/test_bingogrid.py +++ b/server/bingo/tests/test_bingogrid.py @@ -15,32 +15,6 @@ def setUp(self): ) self.challenges.append(c) - def test_exactly_16_challenges(self): - # Verify a BingoGrid with exactly 16 challenges passes validation, but 15 or 17 fails. - grid = BingoGrid.objects.create(is_active=False) - - grid.challenges.add(*self.challenges) - grid.full_clean() - grid.save() - # 15 challenge scenario - grid_15 = BingoGrid.objects.create(is_active=False) - grid_15.challenges.add(*self.challenges[:15]) - with self.assertRaises(ValidationError): - grid_15.full_clean() - - # 17 challenge scenario - extra_challenge = Challenge.objects.create( - name="Extra Challenge", - description="Extra sample challenge", - challenge_type="act", - points=10 - ) - grid_17 = BingoGrid.objects.create(is_active=False) - grid_17.challenges.add(*self.challenges) - grid_17.challenges.add(extra_challenge) - with self.assertRaises(ValidationError): - grid_17.full_clean() - def test_preserve_challenge_order(self): # With SortedManyToManyField, the order we add them is retained. grid = BingoGrid.objects.create(is_active=False)