Skip to content

Commit e18d8b6

Browse files
authored
validation works properly for admin dashboard in admin dashboard (#246)
* validation works properly for admin dashboard in admin dashboard * delete test
1 parent 1c69d4e commit e18d8b6

File tree

3 files changed

+19
-36
lines changed

3 files changed

+19
-36
lines changed

server/bingo/admin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.contrib import admin
22
from django.contrib.auth.models import Group
3+
from django.forms import ModelForm, ValidationError
34

45
from .models import User, Challenge, Friendship, BingoGrid, TileInteraction
56

@@ -44,8 +45,26 @@ class FriendshipAdmin(admin.ModelAdmin):
4445
fields = list_display
4546

4647

48+
class BingoGridAdminForm(ModelForm):
49+
class Meta:
50+
model = BingoGrid
51+
fields = ('grid_id', 'challenges', 'is_active')
52+
53+
def clean_challenges(self):
54+
challenges = self.cleaned_data['challenges']
55+
if len(challenges) != 16:
56+
raise ValidationError(
57+
"BingoGrid must have exactly 16 challenges."
58+
)
59+
return challenges
60+
61+
4762
@admin.register(BingoGrid)
4863
class BingoGridAdmin(admin.ModelAdmin):
64+
65+
# use the above form
66+
form = BingoGridAdminForm
67+
4968
# disable the export2csv action
5069
def get_actions(self, request):
5170
actions = super().get_actions(request)

server/bingo/models.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,6 @@ class BingoGrid(models.Model):
180180
is_active = models.BooleanField(default=False)
181181

182182
def clean(self):
183-
# Ensure exactly 16 challenges
184-
# This only makes sense if the object is saved at least once (has a PK).
185-
# If it's brand new, you won't have the M2M relationships set until after save.
186-
if self.pk:
187-
if self.challenges.count() != 16:
188-
raise ValidationError(
189-
f"BingoGrid must have exactly 16 challenges (found {
190-
self.challenges.count()})."
191-
)
192-
193183
# Ensure only one active BingoGrid
194184
if self.is_active:
195185
active_count = BingoGrid.objects.filter(

server/bingo/tests/test_bingogrid.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,6 @@ def setUp(self):
1515
)
1616
self.challenges.append(c)
1717

18-
def test_exactly_16_challenges(self):
19-
# Verify a BingoGrid with exactly 16 challenges passes validation, but 15 or 17 fails.
20-
grid = BingoGrid.objects.create(is_active=False)
21-
22-
grid.challenges.add(*self.challenges)
23-
grid.full_clean()
24-
grid.save()
25-
# 15 challenge scenario
26-
grid_15 = BingoGrid.objects.create(is_active=False)
27-
grid_15.challenges.add(*self.challenges[:15])
28-
with self.assertRaises(ValidationError):
29-
grid_15.full_clean()
30-
31-
# 17 challenge scenario
32-
extra_challenge = Challenge.objects.create(
33-
name="Extra Challenge",
34-
description="Extra sample challenge",
35-
challenge_type="act",
36-
points=10
37-
)
38-
grid_17 = BingoGrid.objects.create(is_active=False)
39-
grid_17.challenges.add(*self.challenges)
40-
grid_17.challenges.add(extra_challenge)
41-
with self.assertRaises(ValidationError):
42-
grid_17.full_clean()
43-
4418
def test_preserve_challenge_order(self):
4519
# With SortedManyToManyField, the order we add them is retained.
4620
grid = BingoGrid.objects.create(is_active=False)

0 commit comments

Comments
 (0)