use forms

This commit is contained in:
Ludovic Stephan 2016-07-27 13:08:00 +02:00
parent d12a21d44c
commit 460a135fa5
4 changed files with 94 additions and 27 deletions

View file

@ -26,7 +26,8 @@ from bda.models import Spectacle, Participant, ChoixSpectacle, Attribution,\
Tirage, render_template, SpectacleRevente
from bda.algorithm import Algorithm
from bda.forms import BaseBdaFormSet, TokenForm, ResellForm
from bda.forms import BaseBdaFormSet, TokenForm, ResellForm, AnnulForm,\
InscriptionReventeForm
@cof_required
@ -304,16 +305,35 @@ def revente(request, tirage_id):
if not participant.paid:
return render(request, "bda-notpaid.html", {})
if request.method == 'POST':
form = ResellForm(participant, request.POST)
if form.is_valid():
attributions = form.cleaned_data["attributions"]
for attribution in attributions:
revente = SpectacleRevente(attribution=attribution)
revente.save()
if 'resell' in request.POST:
resellform = ResellForm(participant, request.POST, prefix='resell')
annulform = AnnulForm(participant, prefix='annul')
if resellform.is_valid():
attributions = resellform.cleaned_data["attributions"]
for attribution in attributions:
revente = SpectacleRevente(attribution=attribution)
revente.save()
elif 'annul' in request.POST:
annulform = AnnulForm(participant, request.POST, prefix='annul')
resellform = ResellForm(participant, prefix='resell')
if annulform.is_valid():
attributions = annulform.cleaned_data["attributions"]
for attribution in attributions:
attribution.revente.delete()
else:
resellform = ResellForm(participant, prefix='resell')
annulform = AnnulForm(participant, prefix='annul')
else:
form = ResellForm(participant)
resellform = ResellForm(participant, prefix='resell')
annulform = AnnulForm(participant, prefix='annul')
overdue = participant.attribution_set.filter(
spectacle__date__gte=timezone.now(),
revente__isnull=False,
revente__date__lte=timezone.now()-timedelta(hours=1))
return render(request, "bda-revente.html",
{'tirage': tirage, "form": form})
{'tirage': tirage, 'overdue': overdue,
"annulform": annulform, "resellform": resellform})
@login_required
@ -321,11 +341,10 @@ def list_revente(request, tirage_id):
tirage = get_object_or_404(Tirage, id=tirage_id)
participant, created = Participant.objects.get_or_create(
user=request.user, tirage=tirage)
spectacles = tirage.spectacle_set.all()
spectacles = tirage.spectacle_set.filter(
date__gte=timezone.now())
shotgun = []
for spectacle in spectacles:
spectacle.am_interested = spectacle.revente.filter(
pk=participant.id).exists()
revente_objects = SpectacleRevente.objects.filter(
attribution__spectacle=spectacle)
revente_count = 0
@ -335,9 +354,20 @@ def list_revente(request, tirage_id):
if revente_count:
spectacle.revente_count = revente_count
shotgun.append(spectacle)
if request.method == 'POST':
form = InscriptionReventeForm(tirage, request.POST)
if form.is_valid():
choices = form.cleaned_data['spectacles']
participant.choicesrevente = choices
participant.save()
else:
form = InscriptionReventeForm(
tirage,
initial={'spectacles': participant.choicesrevente.all()})
return render(request, "liste-reventes.html",
{"participant": participant, 'tirage': tirage,
"spectacles": spectacles, 'shotgun': shotgun})
{"form": form, 'shotgun': shotgun})
@login_required