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

@ -4,10 +4,12 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
from datetime import timedelta
from django import forms from django import forms
from django.forms.models import BaseInlineFormSet from django.forms.models import BaseInlineFormSet
from django.utils import timezone from django.utils import timezone
from bda.models import Attribution from bda.models import Attribution, Spectacle
class BaseBdaFormSet(BaseInlineFormSet): class BaseBdaFormSet(BaseInlineFormSet):
@ -36,19 +38,45 @@ class TokenForm(forms.Form):
token = forms.CharField(widget=forms.widgets.Textarea()) token = forms.CharField(widget=forms.widgets.Textarea())
class SpectacleModelChoiceField(forms.ModelChoiceField): class AttributionModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj): def label_from_instance(self, obj):
return "%s le %s (%s) à %.02f" % (obj.title, obj.date_no_seconds(), return "%s" % obj.spectacle
obj.location, obj.price)
class ResellForm(forms.Form): class ResellForm(forms.Form):
attributions = forms.ModelMultipleChoiceField( attributions = AttributionModelMultipleChoiceField(
queryset=Attribution.objects.none(), queryset=Attribution.objects.none(),
widget=forms.CheckboxSelectMultiple) widget=forms.CheckboxSelectMultiple,
required=False)
def __init__(self, participant, *args, **kwargs): def __init__(self, participant, *args, **kwargs):
super(ResellForm, self).__init__(*args, **kwargs) super(ResellForm, self).__init__(*args, **kwargs)
self.fields['attributions'].queryset = participant.attribution_set\ self.fields['attributions'].queryset = participant.attribution_set\
.filter(spectacle__date__gte=timezone.now(), .filter(spectacle__date__gte=timezone.now(),
revente__isnull=True) 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())

View file

@ -7,7 +7,20 @@
<h2>Places non revendues</h2> <h2>Places non revendues</h2>
<form action="" method="post"> <form action="" method="post">
{% csrf_token %} {% csrf_token %}
{{form}} {{resellform}}
<input type="submit" value="Revendre les places sélectionnées"> <input type="submit" name="resell" value="Revendre les places sélectionnées">
</form> </form>
<h2>Places en cours de revente</h2>
<form action="" method="post">
{% csrf_token %}
<ul>
{% for box in annulform.attributions %}
<li>{{box.choice_label}}{{box.tag}}</li>
{% endfor %}
{% for attrib in overdue %}
<li>{{attrib.spectacle}}</li>
{%endfor%}
</ul>
<input type="submit" name="annul" value="Annuler les reventes sélectionnées">
{% endblock %} {% endblock %}

View file

@ -5,11 +5,7 @@
<h2>Inscriptions pour BDA-Revente</h2> <h2>Inscriptions pour BDA-Revente</h2>
<form action="" method="post"> <form action="" method="post">
{% csrf_token %} {% csrf_token %}
<ul> {{form}}
{% for spectacle in spectacles %}
<li>{{spectacle}} <input type="checkbox" name="spect_id" value="{{spectacle.id}}" {% if spectacle.am_interested %}checked{%endif%}></li>
{%endfor%}
</ul>
<input type="submit" value="S'inscrire pour les places sélectionnées"> <input type="submit" value="S'inscrire pour les places sélectionnées">
</form> </form>

View file

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