gestioCOF/bda/views.py

41 lines
1.7 KiB
Python
Raw Normal View History

2012-07-11 17:39:20 +02:00
# coding: utf-8
from django.contrib.auth.decorators import login_required
from django import forms
from django.forms.models import inlineformset_factory, BaseInlineFormSet
from gestioncof.shared import render_page
from bda.models import Spectacle, Participant, ChoixSpectacle
class BaseBdaFormSet(BaseInlineFormSet):
def clean(self):
"""Checks that no two articles have the same title."""
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)
@login_required
def inscription(request):
BdaFormSet = inlineformset_factory(Participant, ChoixSpectacle, fields = ("spectacle","double","autoquit","priority",), formset = BaseBdaFormSet)
participant, created = Participant.objects.get_or_create(user = request.user)
success = False
if request.method == "POST":
formset = BdaFormSet(request.POST, instance = participant)
if formset.is_valid():
formset.save()
success = True
formset = BdaFormSet(instance = participant)
else:
formset = BdaFormSet(instance = participant)
return render_page(request, {"formset": formset, "success": success}, "inscription-bda.html")