revente de revente + confirmation de transfert de places

This commit is contained in:
Ludovic Stephan 2016-09-05 02:29:49 +02:00
parent 0b40ebb6f7
commit 3bc9880db1
4 changed files with 80 additions and 13 deletions

View file

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

View file

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

View file

@ -24,6 +24,7 @@
</div>
</form>
<br>
{% if annulform.attributions or overdue %}
<h3>Places en cours de revente</h3>
<form action="" method="post">
{% csrf_token %}
@ -45,6 +46,28 @@
</ul>
</div>
</div>
{% if annulform.attributions %}
<input type="submit" class="btn btn-primary" name="annul" value="Annuler les reventes sélectionnées">
{% endif %}
</form>
{% endif %}
<br>
{% if sold %}
<h3>Places revendues</h3>
<table class="table">
{% for attrib in sold %}
<tr>
<form action="" method="post">
{% csrf_token %}
<td>{{attrib.spectacle}}</td>
<td>{{attrib.revente.soldTo.user.get_full_name}}</td>
<td><button type="submit" class="btn btn-primary" name="transfer"
value={{attrib.revente.id}}>Transférer</button></td>
<td><button type="submit" class="btn btn-primary" name="reinit"
value={{attrib.revente.id}}>Réinitialiser</button></td>
</form>
</tr>
{% endfor %}
</table>
{% endif %}
{% endblock %}

View file

@ -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",