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 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())

View file

@ -7,7 +7,20 @@
<h2>Places non revendues</h2>
<form action="" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Revendre les places sélectionnées">
{{resellform}}
<input type="submit" name="resell" value="Revendre les places sélectionnées">
</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 %}

View file

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

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"]
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:
form = ResellForm(participant)
resellform = ResellForm(participant, prefix='resell')
annulform = AnnulForm(participant, prefix='annul')
else:
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