From 460a135fa591126f9b92c8cfccfc7a3378ef9b58 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Wed, 27 Jul 2016 13:08:00 +0200 Subject: [PATCH] use forms --- bda/forms.py | 40 +++++++++++++++++---- bda/templates/bda-revente.html | 17 +++++++-- bda/templates/liste-reventes.html | 6 +--- bda/views.py | 58 +++++++++++++++++++++++-------- 4 files changed, 94 insertions(+), 27 deletions(-) diff --git a/bda/forms.py b/bda/forms.py index b1acf3d9..60fdb0fd 100644 --- a/bda/forms.py +++ b/bda/forms.py @@ -4,10 +4,12 @@ from __future__ import division from __future__ import print_function from __future__ import unicode_literals +from datetime import timedelta + from django import forms from django.forms.models import BaseInlineFormSet from django.utils import timezone -from bda.models import Attribution +from bda.models import Attribution, Spectacle class BaseBdaFormSet(BaseInlineFormSet): @@ -36,19 +38,45 @@ class TokenForm(forms.Form): token = forms.CharField(widget=forms.widgets.Textarea()) -class SpectacleModelChoiceField(forms.ModelChoiceField): +class AttributionModelMultipleChoiceField(forms.ModelMultipleChoiceField): def label_from_instance(self, obj): - return "%s le %s (%s) à %.02f€" % (obj.title, obj.date_no_seconds(), - obj.location, obj.price) + return "%s" % obj.spectacle class ResellForm(forms.Form): - attributions = forms.ModelMultipleChoiceField( + attributions = AttributionModelMultipleChoiceField( queryset=Attribution.objects.none(), - widget=forms.CheckboxSelectMultiple) + widget=forms.CheckboxSelectMultiple, + required=False) def __init__(self, participant, *args, **kwargs): super(ResellForm, self).__init__(*args, **kwargs) self.fields['attributions'].queryset = participant.attribution_set\ .filter(spectacle__date__gte=timezone.now(), revente__isnull=True) + + +class AnnulForm(forms.Form): + attributions = AttributionModelMultipleChoiceField( + queryset=Attribution.objects.none(), + widget=forms.CheckboxSelectMultiple, + required=False) + + def __init__(self, participant, *args, **kwargs): + super(AnnulForm, self).__init__(*args, **kwargs) + self.fields['attributions'].queryset = participant.attribution_set\ + .filter(spectacle__date__gte=timezone.now(), + revente__isnull=False, + revente__date__gte=timezone.now()-timedelta(hours=1)) + + +class InscriptionReventeForm(forms.Form): + spectacles = forms.ModelMultipleChoiceField( + queryset=Spectacle.objects.none(), + widget=forms.CheckboxSelectMultiple, + required=False) + + def __init__(self, tirage, *args, **kwargs): + super(InscriptionReventeForm, self).__init__(*args, **kwargs) + self.fields['spectacles'].queryset = tirage.spectacle_set.filter( + date__gte=timezone.now()) diff --git a/bda/templates/bda-revente.html b/bda/templates/bda-revente.html index 0b6e14a6..e7fd6e6d 100644 --- a/bda/templates/bda-revente.html +++ b/bda/templates/bda-revente.html @@ -7,7 +7,20 @@

Places non revendues

{% csrf_token %} - {{form}} - + {{resellform}} +
+ +

Places en cours de revente

+
+ {% csrf_token %} + + {% endblock %} diff --git a/bda/templates/liste-reventes.html b/bda/templates/liste-reventes.html index 6ae20388..4b48825f 100644 --- a/bda/templates/liste-reventes.html +++ b/bda/templates/liste-reventes.html @@ -5,11 +5,7 @@

Inscriptions pour BDA-Revente

{% csrf_token %} - + {{form}}
diff --git a/bda/views.py b/bda/views.py index 4c31a635..fe180ada 100644 --- a/bda/views.py +++ b/bda/views.py @@ -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