40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
"""Django Forms to add and edit Mai-Star games."""
|
|
|
|
from django import forms
|
|
from django.utils.translation import gettext as _
|
|
|
|
from . import models
|
|
|
|
|
|
class GameForm(forms.ModelForm):
|
|
"""To add/edit a Mai-Star game for the ladder ranking."""
|
|
error_css_class = 'error'
|
|
required_css_class = 'required'
|
|
|
|
class Meta(object):
|
|
"""hide the metadata in the form."""
|
|
fields = [
|
|
'player1', 'player1_score',
|
|
'player2', 'player2_score',
|
|
'player3', 'player3_score',
|
|
'player4', 'player4_score',
|
|
'player5', 'player5_score',
|
|
'player6', 'player6_score',
|
|
'comment'
|
|
]
|
|
model = models.Game
|
|
|
|
def clean(self):
|
|
"""Check that every player occours only once per game."""
|
|
cleaned_data = super(GameForm, self).clean()
|
|
players_in_game = set()
|
|
for player_no in range(1, 6):
|
|
player = 'player{nr}'.format(nr=player_no)
|
|
current_player = cleaned_data.get('player')
|
|
if current_player and current_player in players_in_game:
|
|
msg = _("%s may only participate once." % current_player)
|
|
self._errors[player] = self.error_class([msg])
|
|
del cleaned_data[player]
|
|
players_in_game.add(current_player)
|
|
return cleaned_data
|