improve bda inscription form/view code

This commit is contained in:
Aurélien Delobelle 2017-04-21 18:22:53 +02:00
parent 2eee8f58aa
commit 0d8a613f28
2 changed files with 41 additions and 45 deletions

View file

@ -32,6 +32,7 @@ from bda.models import (
from bda.algorithm import Algorithm
from bda.forms import (
TokenForm, ResellForm, AnnulForm, InscriptionReventeForm, SoldForm,
InscriptionInlineFormSet,
)
@ -157,60 +158,27 @@ def inscription(request, tirage_id):
messages.error(request, "Le tirage n'est pas encore ouvert : "
"ouverture le {:s}".format(opening))
return render(request, 'bda/resume-inscription-tirage.html', {})
participant, _ = (
Participant.objects.select_related('tirage')
.get_or_create(user=request.user, tirage=tirage)
)
if timezone.now() > tirage.fermeture:
# Le tirage est fermé.
participant, _ = (
Participant.objects
.get_or_create(user=request.user, tirage=tirage)
)
choices = participant.choixspectacle_set.order_by("priority")
messages.error(request,
" C'est fini : tirage au sort dans la journée !")
return render(request, "bda/resume-inscription-tirage.html",
{"choices": choices})
def force_for(f, to_choices=None, **kwargs):
"""Overrides choices for ModelChoiceField.
Args:
f (models.Field): To render as forms.Field
to_choices (dict): If a key `f.name` exists, f._choices is set to
its value.
"""
formfield = f.formfield(**kwargs)
if to_choices:
if f.name in to_choices:
choices = [('', '---------')] + to_choices[f.name]
formfield._choices = choices
return formfield
# Restrict spectacles choices to spectacles for this tirage.
spectacles = (
tirage.spectacle_set
.select_related('location')
)
spectacles_field_choices = [(sp.pk, str(sp)) for sp in spectacles]
# Allow for spectacle choices to be set once for all.
# Form display use 1 request instead of (#forms of formset * #spectacles).
# FIXME: Validation still generates too much requests...
formfield_callback = partial(
force_for,
to_choices={
'spectacle': spectacles_field_choices,
},
)
BdaFormSet = inlineformset_factory(
Participant,
ChoixSpectacle,
fields=("spectacle", "double_choice", "priority"),
formfield_callback=formfield_callback,
)
participant, _ = (
Participant.objects
.get_or_create(user=request.user, tirage=tirage)
formset=InscriptionInlineFormSet,
)
success = False
stateerror = False
if request.method == "POST":