kpsul/bda/forms.py

55 lines
1.9 KiB
Python
Raw Normal View History

2016-07-15 00:02:56 +02:00
# -*- coding: utf-8 -*-
2016-07-15 00:02:56 +02:00
from __future__ import division
from __future__ import print_function
2016-07-08 23:04:34 +02:00
from __future__ import unicode_literals
from django import forms
from django.forms.models import BaseInlineFormSet
2016-07-25 23:03:33 +02:00
from django.utils import timezone
from bda.models import 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):
2016-07-08 23:04:34 +02:00
return "%s le %s (%s) à %.02f" % (obj.title, obj.date_no_seconds(),
2016-07-10 04:49:24 +02:00
obj.location, obj.price)
class ResellForm(forms.Form):
2016-07-25 23:03:33 +02:00
attributions = forms.ModelMultipleChoiceField(
queryset=Attribution.objects.none(),
widget=forms.CheckboxSelectMultiple)
def __init__(self, participant, *args, **kwargs):
super(ResellForm, self).__init__(*args, **kwargs)
2016-07-25 23:03:33 +02:00
self.fields['attributions'].queryset = participant.attribution_set\
.filter(spectacle__date__gte=timezone.now(),
revente__isnull=True)