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
+
+Places en cours de revente
+
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