From d12a21d44c3d9155db7100cceb5f5f5ac20247cb Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Mon, 25 Jul 2016 23:03:33 +0200 Subject: [PATCH] use forms --- bda/forms.py | 13 ++++++++----- bda/models.py | 2 +- bda/templates/bda-revente.html | 22 +--------------------- bda/views.py | 28 +++++++++++----------------- 4 files changed, 21 insertions(+), 44 deletions(-) diff --git a/bda/forms.py b/bda/forms.py index 9f5e3890..b1acf3d9 100644 --- a/bda/forms.py +++ b/bda/forms.py @@ -6,7 +6,8 @@ from __future__ import unicode_literals from django import forms from django.forms.models import BaseInlineFormSet -from bda.models import Spectacle +from django.utils import timezone +from bda.models import Attribution class BaseBdaFormSet(BaseInlineFormSet): @@ -42,10 +43,12 @@ class SpectacleModelChoiceField(forms.ModelChoiceField): class ResellForm(forms.Form): - count = forms.ChoiceField(choices=(("1", "1"), ("2", "2"),)) - spectacle = SpectacleModelChoiceField(queryset=Spectacle.objects.none()) + attributions = forms.ModelMultipleChoiceField( + queryset=Attribution.objects.none(), + widget=forms.CheckboxSelectMultiple) def __init__(self, participant, *args, **kwargs): super(ResellForm, self).__init__(*args, **kwargs) - self.fields['spectacle'].queryset = participant.attributions.all() \ - .distinct() + self.fields['attributions'].queryset = participant.attribution_set\ + .filter(spectacle__date__gte=timezone.now(), + revente__isnull=True) diff --git a/bda/models.py b/bda/models.py index 77b7d00c..91af3e99 100644 --- a/bda/models.py +++ b/bda/models.py @@ -193,7 +193,7 @@ class SpectacleRevente(models.Model): default=timezone.now) interested = models.ManyToManyField(Participant, related_name="wanted", - blank=True, null=True) + blank=True) soldTo = models.ForeignKey(Participant, blank=True, null=True) def get_expiration_time(self): diff --git a/bda/templates/bda-revente.html b/bda/templates/bda-revente.html index 16fbc0e2..0b6e14a6 100644 --- a/bda/templates/bda-revente.html +++ b/bda/templates/bda-revente.html @@ -4,30 +4,10 @@ {% block realcontent %}

Revente de place

-{% if no_resell %}

Places non revendues

{% csrf_token %} -
    - {% for attribution in no_resell %} -
  1. {{attribution.spectacle}}
  2. - {% endfor %} -
+ {{form}}
-{%endif%} -{% if resell %} -

Places en cours de revente

-
- {% csrf_token %} -
    - {% for attribution in resell %} - - {% csrf_token %} - -
  1. {{attribution.spectacle}} {% if attribution.revente.cancellable %}{%endif%}
  2. - {% endfor %} -
-
-{% endif %} {% endblock %} diff --git a/bda/views.py b/bda/views.py index 3c42f94d..4c31a635 100644 --- a/bda/views.py +++ b/bda/views.py @@ -26,7 +26,7 @@ from bda.models import Spectacle, Participant, ChoixSpectacle, Attribution,\ Tirage, render_template, SpectacleRevente from bda.algorithm import Algorithm -from bda.forms import BaseBdaFormSet, TokenForm +from bda.forms import BaseBdaFormSet, TokenForm, ResellForm @cof_required @@ -303,23 +303,17 @@ def revente(request, tirage_id): user=request.user, tirage=tirage) if not participant.paid: return render(request, "bda-notpaid.html", {}) - if request.POST: - for attr_id in request.POST.getlist('resell'): - attribution = Attribution.objects.get(id=int(attr_id)) - revente = SpectacleRevente(attribution=attribution) - revente.save() - if 'annul' in request.POST: - revente = SpectacleRevente.objects\ - .get(attribution__pk=request.POST['annul']) - revente.delete() - - attributions = participant.attribution_set.filter( - spectacle__date__gte=timezone.now) - resell = attributions.filter(revente__isnull=False) - no_resell = attributions.filter(revente__isnull=True) + 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() + else: + form = ResellForm(participant) return render(request, "bda-revente.html", - {"participant": participant, 'tirage': tirage, - "resell": resell, "no_resell": no_resell}) + {'tirage': tirage, "form": form}) @login_required