Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions server/bingo/admin.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions server/bingo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 0 additions & 26 deletions server/bingo/tests/test_bingogrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down