112 lines
3.6 KiB
Python
112 lines
3.6 KiB
Python
# -*- encoding: utf-8 -*-
|
|
|
|
"""
|
|
Created on 04.10.2011
|
|
|
|
@author: christian
|
|
"""
|
|
from django.contrib.auth import get_user_model
|
|
import django.forms
|
|
from django.forms.models import BaseInlineFormSet, inlineformset_factory
|
|
from django.utils import timezone
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from utils.html5 import forms
|
|
from . import models
|
|
|
|
|
|
class HanchanForm(forms.ModelForm):
|
|
error_css_class = 'error'
|
|
required_css_class = 'required'
|
|
start = forms.DateTimeField(label=_('start'), required=True)
|
|
|
|
class Meta(object):
|
|
model = models.Hanchan
|
|
fields = ('event', 'start', 'comment')
|
|
widgets = {
|
|
'event': forms.HiddenInput(),
|
|
'comment': forms.widgets.Textarea(attrs={'rows': 4, 'cols': 40})
|
|
}
|
|
|
|
def clean_start(self):
|
|
u"""
|
|
Das Datum darf nicht in der Zukunft liegen und es muss innerhalb der
|
|
Dauer des Events liegen.
|
|
"""
|
|
start = self.cleaned_data['start']
|
|
event = self.cleaned_data['event']
|
|
if start > timezone.now():
|
|
raise django.forms.ValidationError(
|
|
_("It's not allowed to enter future games."))
|
|
if not event.start <= start <= event.end:
|
|
raise django.forms.ValidationError(
|
|
_("Only games running during this event are allowed."))
|
|
return start
|
|
|
|
|
|
class HanchanAdminForm(HanchanForm):
|
|
class Meta(object):
|
|
model = models.Hanchan
|
|
fields = ('event', 'start', 'comment', 'confirmed')
|
|
widgets = {
|
|
'event': forms.HiddenInput(),
|
|
'comment': forms.widgets.Textarea(attrs={'rows': 4, 'cols': 40})
|
|
}
|
|
|
|
|
|
class PlayerForm(forms.ModelForm):
|
|
error_css_class = 'error'
|
|
required_css_class = 'required'
|
|
player_choices = get_user_model().objects.filter(groups__in=(1, 2))
|
|
player_choices = player_choices.order_by('username').distinct()
|
|
user = forms.ModelChoiceField(player_choices, required=True)
|
|
comment = forms.CharField(
|
|
widget=forms.widgets.TextInput(attrs={'maxlength': 255}),
|
|
required=False
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(PlayerForm, self).__init__(*args, **kwargs)
|
|
self.fields['bonus_points'].widget.attrs['readonly'] = True
|
|
self.fields['bonus_points'].widget.attrs['size'] = 2
|
|
self.fields['comment'].widget.attrs['readonly'] = True
|
|
self.fields['score'].widget.attrs['size'] = 6
|
|
self.fields['score'].widget.attrs['type'] = 'number'
|
|
|
|
|
|
class Meta(object):
|
|
model = models.Player
|
|
fields = ('hanchan', 'user', 'score', 'bonus_points', 'comment')
|
|
|
|
|
|
class PlayerInlineFormSet(BaseInlineFormSet):
|
|
def clean(self):
|
|
"""Checks that no two articles have the same title."""
|
|
for form in self.forms:
|
|
if form.is_valid() and not form.cleaned_data.get('user'):
|
|
raise forms.ValidationError(
|
|
_("A valid Hanchan needs 4 players"))
|
|
self.validate_unique()
|
|
return False
|
|
|
|
|
|
class SeasonSelectForm(django.forms.Form):
|
|
season = django.forms.ChoiceField(label='', choices=('a', 'b', 'c'))
|
|
|
|
def __init__(self, user, data=None, *args, **kwargs):
|
|
super(SeasonSelectForm, self).__init__(args, kwargs)
|
|
season_list = models.LadderRanking.objects.filter(user=user)
|
|
season_list = season_list.select_related('user')
|
|
season_list = season_list.values_list('season__id', 'season__name')
|
|
self.fields['season'] = django.forms.ChoiceField(choices=season_list)
|
|
|
|
|
|
PlayerFormSet = inlineformset_factory(
|
|
models.Hanchan,
|
|
models.Player,
|
|
max_num=4,
|
|
extra=4,
|
|
form=PlayerForm,
|
|
formset=PlayerInlineFormSet
|
|
)
|