You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

56 lines
1.9 KiB

  1. from django import forms
  2. from .models import Game, Player
  3. class PlayerForm(forms.Form):
  4. name = forms.CharField(max_length=80, label="Name", required=False)
  5. spirit = forms.TypedChoiceField(label="Spirit",
  6. choices=enumerate(Player.SPIRIT_NAMES),
  7. empty_value=None, coerce=int)
  8. class BasePlayerFormSet(forms.BaseFormSet):
  9. def clean(self):
  10. if any(self.errors):
  11. return
  12. names = set()
  13. for form in self.forms:
  14. if self.can_delete and self._should_delete_form(form):
  15. continue
  16. name = form.cleaned_data.get('name')
  17. if name:
  18. if name in names:
  19. raise forms.ValidationError(
  20. "Players must have distinct names.")
  21. names.add(name)
  22. if not names:
  23. raise forms.ValidationError("Must have at least one player.")
  24. PlayerFormSet = forms.formset_factory(PlayerForm, formset=BasePlayerFormSet)
  25. class NewGameForm(forms.Form):
  26. combined_growth_spirit = forms.BooleanField(
  27. required=False, initial=True,
  28. label="Combine Growth and Spirit phases into a single phase")
  29. england_build = forms.BooleanField(
  30. required=False, initial=False,
  31. label="High Immigration (extra build phase for England level 3+)")
  32. fear_per_player = forms.IntegerField(
  33. label="Fear per player", initial=4, min_value=1, max_value=99)
  34. class JoinGameForm(forms.Form):
  35. game = forms.CharField(label='Access code',
  36. max_length=Game.ACCESS_CODE_LENGTH)
  37. def clean_game(self):
  38. data = self.cleaned_data['game']
  39. try:
  40. return Game.objects.get(access_code=data.lower())
  41. except Game.DoesNotExist:
  42. raise forms.ValidationError("Invalid access code.")