2016-05-27 17:52:02 +02:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from django import forms
|
2016-06-05 02:16:14 +02:00
|
|
|
from django.forms.models import BaseInlineFormSet
|
2016-06-10 23:59:41 +02:00
|
|
|
from bda.models import Spectacle
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class BaseBdaFormSet(BaseInlineFormSet):
|
|
|
|
def clean(self):
|
|
|
|
"""Checks that no two articles have the same title."""
|
|
|
|
super(BaseBdaFormSet, self).clean()
|
|
|
|
if any(self.errors):
|
2016-07-09 21:19:37 +02:00
|
|
|
# Don't bother validating the formset unless each form is valid on
|
|
|
|
# its own
|
2016-05-27 17:52:02 +02:00
|
|
|
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:
|
2016-07-09 22:31:56 +02:00
|
|
|
raise forms.ValidationError(
|
|
|
|
"Vous ne pouvez pas vous inscrire deux fois pour le "
|
|
|
|
"même spectacle.")
|
2016-05-27 17:52:02 +02:00
|
|
|
spectacles.append(spectacle)
|
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class TokenForm(forms.Form):
|
2016-06-05 02:16:14 +02:00
|
|
|
token = forms.CharField(widget=forms.widgets.Textarea())
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class SpectacleModelChoiceField(forms.ModelChoiceField):
|
|
|
|
def label_from_instance(self, obj):
|
2016-06-05 02:16:14 +02:00
|
|
|
return u"%s le %s (%s) à %.02f€" % (obj.title, obj.date_no_seconds(),
|
|
|
|
obj.location, obj.price)
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class ResellForm(forms.Form):
|
2016-07-09 21:19:37 +02:00
|
|
|
count = forms.ChoiceField(choices=(("1", "1"), ("2", "2"),))
|
2016-06-05 02:16:14 +02:00
|
|
|
spectacle = SpectacleModelChoiceField(queryset=Spectacle.objects.none())
|
2016-05-27 17:52:02 +02:00
|
|
|
|
|
|
|
def __init__(self, participant, *args, **kwargs):
|
|
|
|
super(ResellForm, self).__init__(*args, **kwargs)
|
2016-07-09 23:59:25 +02:00
|
|
|
self.fields['spectacle'].queryset = participant.attributions.all() \
|
|
|
|
.distinct()
|