use forms

This commit is contained in:
Ludovic Stephan 2016-07-25 23:03:33 +02:00
parent 90581af528
commit d12a21d44c
4 changed files with 21 additions and 44 deletions

View file

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

View file

@ -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):

View file

@ -4,30 +4,10 @@
{% block realcontent %}
<h1>Revente de place</h1>
{% if no_resell %}
<h2>Places non revendues</h2>
<form action="" method="post">
{% csrf_token %}
<ol>
{% for attribution in no_resell %}
<li>{{attribution.spectacle}} <input type="checkbox" name="resell" value="{{attribution.id}}"></li>
{% endfor %}
</ol>
{{form}}
<input type="submit" value="Revendre les places sélectionnées">
</form>
{%endif%}
{% if resell %}
<h2>Places en cours de revente</h2>
<form action="" method="post">
{% csrf_token %}
<ol>
{% for attribution in resell %}
<form action="" method="post">
{% csrf_token %}
<input type="hidden" name="annul" value="{{attribution.id}}">
<li>{{attribution.spectacle}} {% if attribution.revente.cancellable %}<input type="submit" value="Supprimer">{%endif%}</li>
{% endfor %}
</ol>
</form>
{% endif %}
{% endblock %}

View file

@ -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))
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 '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)
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