forked from DGNum/gestioCOF
Déplace les formulaires vers des forms.py
Les formulaires de chaque application sont désormais dans un fichier `forms.py`.
This commit is contained in:
parent
dee6310465
commit
65667f6652
8 changed files with 422 additions and 395 deletions
39
bda/forms.py
Normal file
39
bda/forms.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
# coding: utf-8
|
||||
|
||||
from django import forms
|
||||
from django.forms.models import inlineformset_factory, BaseInlineFormSet
|
||||
from bda.models import Spectacle, Participant, ChoixSpectacle, Attribution
|
||||
|
||||
class BaseBdaFormSet(BaseInlineFormSet):
|
||||
def clean(self):
|
||||
"""Checks that no two articles have the same title."""
|
||||
super(BaseBdaFormSet, self).clean()
|
||||
if any(self.errors):
|
||||
# Don't bother validating the formset unless each form is valid on its own
|
||||
return
|
||||
spectacles = []
|
||||
for i in range(0, self.total_form_count()):
|
||||
form = self.forms[i]
|
||||
if not form.cleaned_data:
|
||||
continue
|
||||
spectacle = form.cleaned_data['spectacle']
|
||||
delete = form.cleaned_data['DELETE']
|
||||
if not delete and spectacle in spectacles:
|
||||
raise forms.ValidationError("Vous ne pouvez pas vous inscrire deux fois pour le même spectacle.")
|
||||
spectacles.append(spectacle)
|
||||
|
||||
class TokenForm(forms.Form):
|
||||
token = forms.CharField(widget = forms.widgets.Textarea())
|
||||
|
||||
class SpectacleModelChoiceField(forms.ModelChoiceField):
|
||||
def label_from_instance(self, obj):
|
||||
return u"%s le %s (%s) à %.02f€" % (obj.title, obj.date_no_seconds(), obj.location, obj.price)
|
||||
|
||||
class ResellForm(forms.Form):
|
||||
count = forms.ChoiceField(choices = (("1","1"),("2","2"),))
|
||||
spectacle = SpectacleModelChoiceField(queryset = Spectacle.objects.none())
|
||||
|
||||
def __init__(self, participant, *args, **kwargs):
|
||||
super(ResellForm, self).__init__(*args, **kwargs)
|
||||
self.fields['spectacle'].queryset = participant.attributions.all().distinct()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue