diff --git a/bda/forms.py b/bda/forms.py index 60fdb0fd..0c886e0f 100644 --- a/bda/forms.py +++ b/bda/forms.py @@ -52,8 +52,8 @@ class ResellForm(forms.Form): 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) + .filter(spectacle__date__gte=timezone.now())\ + .exclude(revente__seller=participant) class AnnulForm(forms.Form): @@ -67,7 +67,8 @@ class AnnulForm(forms.Form): self.fields['attributions'].queryset = participant.attribution_set\ .filter(spectacle__date__gte=timezone.now(), revente__isnull=False, - revente__date__gte=timezone.now()-timedelta(hours=1)) + revente__date__gte=timezone.now()-timedelta(hours=1), + revente__seller=participant) class InscriptionReventeForm(forms.Form): diff --git a/bda/models.py b/bda/models.py index c3621a45..428dbfb7 100644 --- a/bda/models.py +++ b/bda/models.py @@ -221,6 +221,8 @@ class SpectacleRevente(models.Model): interested = models.ManyToManyField(Participant, related_name="wanted", blank=True) + seller = models.ForeignKey(Participant, + related_name="original_shows") soldTo = models.ForeignKey(Participant, blank=True, null=True) def get_expiration_time(self): @@ -235,7 +237,7 @@ class SpectacleRevente(models.Model): shotgun = property(get_shotgun) def __str__(self): - return "%s -- %s" % (self.attribution.participant.user, + return "%s -- %s" % (self.seller, self.attribution.spectacle.title) def send_notif(self): @@ -261,7 +263,7 @@ class SpectacleRevente(models.Model): def tirage(self): inscrits = self.interested spectacle = self.attribution.spectacle - user = self.attribution.participant.user + seller = self.seller if inscrits.exists(): idx = random.randint(0, inscrits.count() - 1) winner = inscrits.all()[idx] @@ -273,7 +275,7 @@ Tu peux contacter le vendeur à l'adresse %s. Chaleureusement, Le BdA""" % (spectacle.title, spectacle.date_no_seconds(), - spectacle.location, spectacle.price, user.email) + spectacle.location, spectacle.price, seller.email) mail.send_mail("BdA-Revente : %s" % spectacle.title, mail_buyer, "bda@ens.fr", [winner.user.email], @@ -286,5 +288,5 @@ Chaleureusement, Le BdA""" % (spectacle.title, winner.user.get_full_name(), winner.user.email) mail.send_mail("BdA-Revente : %s" % spectacle.title, - mail_seller, "bda@ens.fr", [user.email], + mail_seller, "bda@ens.fr", [seller.email], fail_silently=False) diff --git a/bda/templates/bda-revente.html b/bda/templates/bda-revente.html index 0a5cffb3..a5f64678 100644 --- a/bda/templates/bda-revente.html +++ b/bda/templates/bda-revente.html @@ -24,6 +24,7 @@
+{% if annulform.attributions or overdue %}

Places en cours de revente

{% csrf_token %} @@ -45,6 +46,28 @@ + {% if annulform.attributions %} + {% endif %}
+{% endif %} +
+{% if sold %} +

Places revendues

+ + {% for attrib in sold %} + + + {% csrf_token %} + + + + + + + {% endfor %} +
{{attrib.spectacle}}{{attrib.revente.soldTo.user.get_full_name}}
+{% endif %} {% endblock %} diff --git a/bda/views.py b/bda/views.py index a3fd9168..8c92f4ab 100644 --- a/bda/views.py +++ b/bda/views.py @@ -9,7 +9,7 @@ import random from django.shortcuts import render, get_object_or_404 from django.contrib.auth.decorators import login_required from django.db import models -from django.db.models import Count +from django.db.models import Count, Q from django.core import serializers from django.forms.models import inlineformset_factory from django.http import HttpResponseBadRequest @@ -293,8 +293,14 @@ def revente(request, tirage_id): if resellform.is_valid(): attributions = resellform.cleaned_data["attributions"] for attribution in attributions: - revente = SpectacleRevente(attribution=attribution) + revente, created = SpectacleRevente.objects.get_or_create( + attribution=attribution, + defaults={'seller': participant}) + if not created: + revente.seller = participant + revente.date = timezone.now() revente.save() + elif 'annul' in request.POST: annulform = AnnulForm(participant, request.POST, prefix='annul') resellform = ResellForm(participant, prefix='resell') @@ -302,19 +308,54 @@ def revente(request, tirage_id): attributions = annulform.cleaned_data["attributions"] for attribution in attributions: attribution.revente.delete() + + elif 'transfer' in request.POST: + resellform = ResellForm(participant, prefix='resell') + annulform = AnnulForm(participant, prefix='annul') + + revente_id = request.POST['transfer'][0] + rev = SpectacleRevente.objects.filter(soldTo__isnull=False, + id=revente_id) + if rev.exists(): + revente = rev.get() + attrib = revente.attribution + attrib.participant = revente.soldTo + attrib.save() + + elif 'reinit' in request.POST: + resellform = ResellForm(participant, prefix='resell') + annulform = AnnulForm(participant, prefix='annul') + revente_id = request.POST['transfer'][0] + rev = SpectacleRevente.objects.filter(soldTo__isnull=False, + id=revente_id) + if rev.exists(): + revente = rev.get() + revente.date = timezone.now() - timedelta(hours=1) + revente.soldTo = None + revente.interested = None + # schedule job + else: 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)) + revente__seller=participant, + revente__date__lte=timezone.now()-timedelta(hours=1)).filter( + Q(revente__soldTo__isnull=True) | Q(revente__soldTo=participant)) + sold = participant.attribution_set.filter( + spectacle__date__gte=timezone.now(), + revente__isnull=False, + revente__soldTo__isnull=False).exclude( + revente__soldTo=participant) return render(request, "bda-revente.html", - {'tirage': tirage, 'overdue': overdue, + {'tirage': tirage, 'overdue': overdue, "sold": sold, "annulform": annulform, "resellform": resellform}) @@ -395,10 +436,10 @@ Contacte-moi si tu es toujours intéressé·e ! request.user.get_full_name(), request.user.email) send_mail("BdA-Revente : %s" % spectacle.title, mail, request.user.email, - [revente.attribution.participant.user.email], + [revente.seller.user.email], fail_silently=False) return render(request, "bda-success.html", - {"seller": revente.attribution.participant.user, + {"seller": revente.seller.user, "spectacle": spectacle}) return render(request, "revente-confirm.html",