2017-01-06 18:19:15 +01:00
|
|
|
import hashlib
|
2017-03-31 04:33:13 +02:00
|
|
|
import json
|
2018-10-06 12:35:49 +02:00
|
|
|
import random
|
|
|
|
import time
|
|
|
|
from collections import defaultdict
|
|
|
|
|
2017-06-02 20:33:23 +02:00
|
|
|
from custommail.models import CustomMail
|
2018-10-06 12:35:49 +02:00
|
|
|
from custommail.shortcuts import send_custom_mail, send_mass_custom_mail
|
|
|
|
from django.conf import settings
|
2017-01-29 17:13:01 +01:00
|
|
|
from django.contrib import messages
|
2016-12-22 02:00:10 +01:00
|
|
|
from django.core import serializers
|
2018-11-12 22:44:09 +01:00
|
|
|
from django.core.exceptions import NON_FIELD_ERRORS
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.db import transaction
|
2018-10-07 00:34:36 +02:00
|
|
|
from django.db.models import Count, Prefetch
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.forms.models import inlineformset_factory
|
|
|
|
from django.http import HttpResponseBadRequest, HttpResponseRedirect, JsonResponse
|
|
|
|
from django.shortcuts import get_object_or_404, render
|
|
|
|
from django.template.defaultfilters import pluralize
|
2019-03-19 10:18:56 +01:00
|
|
|
from django.urls import reverse
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.utils import formats, timezone
|
2016-06-06 18:43:56 +02:00
|
|
|
from django.views.generic.list import ListView
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
from bda.algorithm import Algorithm
|
2017-03-31 18:54:31 +02:00
|
|
|
from bda.forms import (
|
2018-10-06 12:35:49 +02:00
|
|
|
AnnulForm,
|
|
|
|
InscriptionInlineFormSet,
|
|
|
|
InscriptionReventeForm,
|
|
|
|
ResellForm,
|
|
|
|
ReventeTirageAnnulForm,
|
|
|
|
ReventeTirageForm,
|
|
|
|
SoldForm,
|
|
|
|
TokenForm,
|
2017-03-31 18:54:31 +02:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
from bda.models import (
|
|
|
|
Attribution,
|
|
|
|
CategorieSpectacle,
|
|
|
|
ChoixSpectacle,
|
|
|
|
Participant,
|
|
|
|
Salle,
|
|
|
|
Spectacle,
|
|
|
|
SpectacleRevente,
|
|
|
|
Tirage,
|
|
|
|
)
|
|
|
|
from gestioncof.decorators import buro_required, cof_required
|
2017-11-19 18:41:39 +01:00
|
|
|
from utils.views.autocomplete import Select2QuerySetView
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-01 15:27:19 +02:00
|
|
|
@cof_required
|
2016-06-05 02:18:12 +02:00
|
|
|
def etat_places(request, tirage_id):
|
2017-01-06 18:19:15 +01:00
|
|
|
"""
|
|
|
|
Résumé des spectacles d'un tirage avec pour chaque spectacle :
|
2017-01-07 13:13:12 +01:00
|
|
|
- Le nombre de places en jeu
|
2017-01-06 18:19:15 +01:00
|
|
|
- Le nombre de demandes
|
|
|
|
- Le ratio demandes/places
|
|
|
|
Et le total de toutes les demandes
|
|
|
|
"""
|
2016-06-05 02:18:12 +02:00
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
2017-04-07 13:25:50 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
spectacles = tirage.spectacle_set.select_related("location")
|
2017-04-07 13:25:50 +02:00
|
|
|
spectacles_dict = {} # index of spectacle by id
|
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
for spectacle in spectacles:
|
2017-04-07 13:25:50 +02:00
|
|
|
spectacle.total = 0 # init total requests
|
2013-09-05 22:20:52 +02:00
|
|
|
spectacles_dict[spectacle.id] = spectacle
|
2017-04-07 13:25:50 +02:00
|
|
|
|
|
|
|
choices = (
|
2018-10-06 12:35:49 +02:00
|
|
|
ChoixSpectacle.objects.filter(spectacle__in=spectacles)
|
|
|
|
.values("spectacle")
|
|
|
|
.annotate(total=Count("spectacle"))
|
2017-04-07 13:25:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# choices *by spectacles* whose only 1 place is requested
|
|
|
|
choices1 = choices.filter(double_choice="1")
|
|
|
|
# choices *by spectacles* whose 2 places is requested
|
|
|
|
choices2 = choices.exclude(double_choice="1")
|
|
|
|
|
|
|
|
for spectacle in choices1:
|
2018-10-06 12:35:49 +02:00
|
|
|
pk = spectacle["spectacle"]
|
|
|
|
spectacles_dict[pk].total += spectacle["total"]
|
2017-04-07 13:25:50 +02:00
|
|
|
for spectacle in choices2:
|
2018-10-06 12:35:49 +02:00
|
|
|
pk = spectacle["spectacle"]
|
|
|
|
spectacles_dict[pk].total += 2 * spectacle["total"]
|
2017-04-07 13:25:50 +02:00
|
|
|
|
|
|
|
# here, each spectacle.total contains the number of requests
|
|
|
|
|
|
|
|
slots = 0 # proposed slots
|
|
|
|
total = 0 # requests
|
|
|
|
for spectacle in spectacles:
|
|
|
|
slots += spectacle.slots
|
|
|
|
total += spectacle.total
|
|
|
|
spectacle.ratio = spectacle.total / spectacle.slots
|
|
|
|
|
2017-01-07 13:13:12 +01:00
|
|
|
context = {
|
2017-04-07 13:25:50 +02:00
|
|
|
"proposed": slots,
|
2017-01-07 13:13:12 +01:00
|
|
|
"spectacles": spectacles,
|
|
|
|
"total": total,
|
2018-10-06 12:35:49 +02:00
|
|
|
"tirage": tirage,
|
2017-01-07 13:13:12 +01:00
|
|
|
}
|
|
|
|
return render(request, "bda/etat-places.html", context)
|
2013-09-05 22:20:52 +02:00
|
|
|
|
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
def _hash_queryset(queryset):
|
2018-10-06 12:35:49 +02:00
|
|
|
data = serializers.serialize("json", queryset).encode("utf-8")
|
2014-08-19 12:54:22 +02:00
|
|
|
hasher = hashlib.sha256()
|
|
|
|
hasher.update(data)
|
|
|
|
return hasher.hexdigest()
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
@cof_required
|
2016-06-05 02:18:12 +02:00
|
|
|
def places(request, tirage_id):
|
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
2018-10-06 12:35:49 +02:00
|
|
|
participant, _ = Participant.objects.get_or_create(user=request.user, tirage=tirage)
|
|
|
|
places = participant.attribution_set.order_by(
|
|
|
|
"spectacle__date", "spectacle"
|
|
|
|
).select_related("spectacle", "spectacle__location")
|
2017-04-07 13:25:50 +02:00
|
|
|
total = sum(place.spectacle.price for place in places)
|
2014-08-19 12:54:22 +02:00
|
|
|
filtered_places = []
|
|
|
|
places_dict = {}
|
|
|
|
spectacles = []
|
|
|
|
dates = []
|
|
|
|
warning = False
|
|
|
|
for place in places:
|
|
|
|
if place.spectacle in spectacles:
|
|
|
|
places_dict[place.spectacle].double = True
|
|
|
|
else:
|
|
|
|
place.double = False
|
|
|
|
places_dict[place.spectacle] = place
|
|
|
|
spectacles.append(place.spectacle)
|
|
|
|
filtered_places.append(place)
|
|
|
|
date = place.spectacle.date.date()
|
|
|
|
if date in dates:
|
|
|
|
warning = True
|
|
|
|
else:
|
|
|
|
dates.append(date)
|
2017-01-29 22:49:32 +01:00
|
|
|
# On prévient l'utilisateur s'il a deux places à la même date
|
|
|
|
if warning:
|
2018-10-06 12:35:49 +02:00
|
|
|
messages.warning(
|
|
|
|
request,
|
|
|
|
"Attention, vous avez reçu des places pour "
|
|
|
|
"des spectacles différents à la même date.",
|
|
|
|
)
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"bda/resume_places.html",
|
|
|
|
{
|
|
|
|
"participant": participant,
|
|
|
|
"places": filtered_places,
|
|
|
|
"tirage": tirage,
|
|
|
|
"total": total,
|
|
|
|
},
|
|
|
|
)
|
2014-08-19 12:54:22 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
@cof_required
|
2016-06-05 02:18:12 +02:00
|
|
|
def inscription(request, tirage_id):
|
2017-01-29 17:13:01 +01:00
|
|
|
"""
|
|
|
|
Vue d'inscription à un tirage BdA.
|
|
|
|
- On vérifie qu'on se situe bien entre la date d'ouverture et la date de
|
|
|
|
fermeture des inscriptions.
|
|
|
|
- On vérifie que l'inscription n'a pas été modifiée entre le moment où le
|
|
|
|
client demande le formulaire et le moment où il soumet son inscription
|
|
|
|
(autre session par exemple).
|
|
|
|
"""
|
2016-06-05 02:18:12 +02:00
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
2016-06-06 19:22:01 +02:00
|
|
|
if timezone.now() < tirage.ouverture:
|
2017-01-29 17:13:01 +01:00
|
|
|
# Le tirage n'est pas encore ouvert.
|
2018-10-06 12:35:49 +02:00
|
|
|
opening = formats.localize(timezone.template_localtime(tirage.ouverture))
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
"Le tirage n'est pas encore ouvert : " "ouverture le {:s}".format(opening),
|
|
|
|
)
|
|
|
|
return render(request, "bda/resume-inscription-tirage.html", {})
|
|
|
|
|
|
|
|
participant, _ = Participant.objects.select_related("tirage").get_or_create(
|
|
|
|
user=request.user, tirage=tirage
|
2017-04-21 18:22:53 +02:00
|
|
|
)
|
|
|
|
|
2016-06-05 02:18:12 +02:00
|
|
|
if timezone.now() > tirage.fermeture:
|
2017-01-29 17:13:01 +01:00
|
|
|
# Le tirage est fermé.
|
2017-04-07 13:25:50 +02:00
|
|
|
choices = participant.choixspectacle_set.order_by("priority")
|
2018-10-06 12:35:49 +02:00
|
|
|
messages.error(request, " C'est fini : tirage au sort dans la journée !")
|
|
|
|
return render(
|
|
|
|
request, "bda/resume-inscription-tirage.html", {"choices": choices}
|
|
|
|
)
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-06-06 11:19:27 +02:00
|
|
|
BdaFormSet = inlineformset_factory(
|
2016-07-09 22:31:56 +02:00
|
|
|
Participant,
|
|
|
|
ChoixSpectacle,
|
|
|
|
fields=("spectacle", "double_choice", "priority"),
|
2017-04-21 18:22:53 +02:00
|
|
|
formset=InscriptionInlineFormSet,
|
2018-11-12 22:44:09 +01:00
|
|
|
error_messages={
|
|
|
|
NON_FIELD_ERRORS: {
|
2018-11-12 22:52:20 +01:00
|
|
|
"unique_together": "Vous avez déjà demandé ce voeu plus haut !"
|
|
|
|
}
|
|
|
|
},
|
2017-04-07 13:25:50 +02:00
|
|
|
)
|
2017-04-21 18:22:53 +02:00
|
|
|
|
2012-07-11 17:39:20 +02:00
|
|
|
if request.method == "POST":
|
2017-04-07 13:25:50 +02:00
|
|
|
# use *this* queryset
|
2014-08-19 12:54:22 +02:00
|
|
|
dbstate = _hash_queryset(participant.choixspectacle_set.all())
|
|
|
|
if "dbstate" in request.POST and dbstate != request.POST["dbstate"]:
|
2016-06-04 13:25:35 +02:00
|
|
|
formset = BdaFormSet(instance=participant)
|
2018-11-12 22:22:49 +01:00
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
"Impossible d'enregistrer vos modifications "
|
|
|
|
": vous avez apporté d'autres modifications "
|
|
|
|
"entre temps.",
|
|
|
|
)
|
2014-08-19 12:54:22 +02:00
|
|
|
else:
|
2016-06-04 13:25:35 +02:00
|
|
|
formset = BdaFormSet(request.POST, instance=participant)
|
2014-08-19 12:54:22 +02:00
|
|
|
if formset.is_valid():
|
2016-06-05 02:18:12 +02:00
|
|
|
formset.save()
|
2016-06-04 13:25:35 +02:00
|
|
|
formset = BdaFormSet(instance=participant)
|
2018-11-12 22:22:49 +01:00
|
|
|
messages.success(
|
|
|
|
request, "Votre inscription a été mise à jour avec succès !"
|
|
|
|
)
|
2018-11-12 22:16:43 +01:00
|
|
|
else:
|
2018-11-12 22:22:49 +01:00
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
"Une erreur s'est produite lors de l'enregistrement de vos vœux. "
|
|
|
|
"Avez-vous demandé plusieurs fois le même spectacle ?",
|
|
|
|
)
|
2012-07-11 17:39:20 +02:00
|
|
|
else:
|
2016-06-04 13:25:35 +02:00
|
|
|
formset = BdaFormSet(instance=participant)
|
2017-04-07 13:25:50 +02:00
|
|
|
# use *this* queryset
|
2014-08-19 12:54:22 +02:00
|
|
|
dbstate = _hash_queryset(participant.choixspectacle_set.all())
|
2013-09-05 22:20:52 +02:00
|
|
|
total_price = 0
|
2018-10-06 12:35:49 +02:00
|
|
|
choices = participant.choixspectacle_set.select_related("spectacle")
|
2017-04-07 13:25:50 +02:00
|
|
|
for choice in choices:
|
2013-09-05 22:20:52 +02:00
|
|
|
total_price += choice.spectacle.price
|
2016-07-09 21:19:37 +02:00
|
|
|
if choice.double:
|
|
|
|
total_price += choice.spectacle.price
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"bda/inscription-tirage.html",
|
|
|
|
{
|
|
|
|
"formset": formset,
|
|
|
|
"total_price": total_price,
|
|
|
|
"dbstate": dbstate,
|
|
|
|
"tirage": tirage,
|
|
|
|
},
|
|
|
|
)
|
2013-09-05 22:20:52 +02:00
|
|
|
|
|
|
|
|
2017-02-03 17:07:50 +01:00
|
|
|
def do_tirage(tirage_elt, token):
|
|
|
|
"""
|
|
|
|
Fonction auxiliaire à la vue ``tirage`` qui lance effectivement le tirage
|
|
|
|
après qu'on a vérifié que c'est légitime et que le token donné en argument
|
|
|
|
est correct.
|
|
|
|
Rend les résultats
|
|
|
|
"""
|
|
|
|
# Initialisation du dictionnaire data qui va contenir les résultats
|
2014-08-19 12:54:22 +02:00
|
|
|
start = time.time()
|
2017-02-03 17:07:50 +01:00
|
|
|
data = {
|
2018-10-06 12:35:49 +02:00
|
|
|
"shows": tirage_elt.spectacle_set.select_related("location"),
|
|
|
|
"token": token,
|
|
|
|
"members": tirage_elt.participant_set.select_related("user"),
|
|
|
|
"total_slots": 0,
|
|
|
|
"total_losers": 0,
|
|
|
|
"total_sold": 0,
|
|
|
|
"total_deficit": 0,
|
|
|
|
"opera_deficit": 0,
|
2017-02-03 17:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# On lance le tirage
|
|
|
|
choices = (
|
2018-10-06 12:35:49 +02:00
|
|
|
ChoixSpectacle.objects.filter(spectacle__tirage=tirage_elt)
|
|
|
|
.order_by("participant", "priority")
|
|
|
|
.select_related("participant", "participant__user", "spectacle")
|
2017-02-03 17:07:50 +01:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
results = Algorithm(data["shows"], data["members"], choices)(token)
|
2017-02-03 17:07:50 +01:00
|
|
|
|
|
|
|
# On compte les places attribuées et les déçus
|
2013-09-05 22:20:52 +02:00
|
|
|
for (_, members, losers) in results:
|
2018-10-06 12:35:49 +02:00
|
|
|
data["total_slots"] += len(members)
|
|
|
|
data["total_losers"] += len(losers)
|
2017-02-03 17:07:50 +01:00
|
|
|
|
|
|
|
# On calcule le déficit et les bénéfices pour le BdA
|
|
|
|
# FIXME: le traitement de l'opéra est sale
|
2014-08-19 12:54:22 +02:00
|
|
|
for (show, members, _) in results:
|
|
|
|
deficit = (show.slots - len(members)) * show.price
|
2018-10-06 12:35:49 +02:00
|
|
|
data["total_sold"] += show.slots * show.price
|
2013-09-05 22:20:52 +02:00
|
|
|
if deficit >= 0:
|
2016-05-26 22:44:10 +02:00
|
|
|
if "Opéra" in show.location.name:
|
2018-10-06 12:35:49 +02:00
|
|
|
data["opera_deficit"] += deficit
|
|
|
|
data["total_deficit"] += deficit
|
|
|
|
data["total_sold"] -= data["total_deficit"]
|
2017-02-03 17:07:50 +01:00
|
|
|
|
|
|
|
# Participant objects are not shared accross spectacle results,
|
|
|
|
# so assign a single object for each Participant id
|
|
|
|
members_uniq = {}
|
|
|
|
members2 = {}
|
|
|
|
for (show, members, _) in results:
|
|
|
|
for (member, _, _, _) in members:
|
|
|
|
if member.id not in members_uniq:
|
|
|
|
members_uniq[member.id] = member
|
|
|
|
members2[member] = []
|
|
|
|
member.total = 0
|
|
|
|
member = members_uniq[member.id]
|
|
|
|
members2[member].append(show)
|
|
|
|
member.total += show.price
|
|
|
|
members2 = members2.items()
|
|
|
|
data["members2"] = sorted(members2, key=lambda m: m[0].user.last_name)
|
|
|
|
|
|
|
|
# ---
|
|
|
|
# À partir d'ici, le tirage devient effectif
|
|
|
|
# ---
|
|
|
|
|
|
|
|
# On suppression les vieilles attributions, on sauvegarde le token et on
|
|
|
|
# désactive le tirage
|
|
|
|
Attribution.objects.filter(spectacle__tirage=tirage_elt).delete()
|
|
|
|
tirage_elt.tokens += '{:s}\n"""{:s}"""\n'.format(
|
2018-10-06 12:35:49 +02:00
|
|
|
timezone.now().strftime("%y-%m-%d %H:%M:%S"), token
|
|
|
|
)
|
2017-02-03 17:07:50 +01:00
|
|
|
tirage_elt.enable_do_tirage = False
|
|
|
|
tirage_elt.save()
|
|
|
|
|
|
|
|
# On enregistre les nouvelles attributions
|
2018-10-06 12:35:49 +02:00
|
|
|
Attribution.objects.bulk_create(
|
|
|
|
[
|
|
|
|
Attribution(spectacle=show, participant=member)
|
|
|
|
for show, members, _ in results
|
|
|
|
for member, _, _, _ in members
|
|
|
|
]
|
|
|
|
)
|
2017-02-03 17:07:50 +01:00
|
|
|
|
|
|
|
# On inscrit à BdA-Revente ceux qui n'ont pas eu les places voulues
|
2017-04-07 16:22:10 +02:00
|
|
|
ChoixRevente = Participant.choicesrevente.through
|
|
|
|
|
2017-06-02 18:32:23 +02:00
|
|
|
# Suppression des reventes demandées/enregistrées
|
|
|
|
# (si le tirage est relancé)
|
2018-10-06 12:35:49 +02:00
|
|
|
(ChoixRevente.objects.filter(spectacle__tirage=tirage_elt).delete())
|
2017-04-07 17:04:06 +02:00
|
|
|
(
|
2018-10-06 12:35:49 +02:00
|
|
|
SpectacleRevente.objects.filter(
|
|
|
|
attribution__spectacle__tirage=tirage_elt
|
|
|
|
).delete()
|
2017-04-07 17:04:06 +02:00
|
|
|
)
|
|
|
|
|
2017-04-07 16:22:10 +02:00
|
|
|
lost_by = defaultdict(set)
|
|
|
|
for show, _, losers in results:
|
|
|
|
for loser, _, _, _ in losers:
|
|
|
|
lost_by[loser].add(show)
|
|
|
|
|
|
|
|
ChoixRevente.objects.bulk_create(
|
|
|
|
ChoixRevente(participant=member, spectacle=show)
|
|
|
|
for member, shows in lost_by.items()
|
|
|
|
for show in shows
|
|
|
|
)
|
2017-02-03 17:07:50 +01:00
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
data["duration"] = time.time() - start
|
2017-02-03 17:07:50 +01:00
|
|
|
data["results"] = results
|
|
|
|
return data
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-06-05 13:59:24 +02:00
|
|
|
@buro_required
|
2016-06-05 02:18:12 +02:00
|
|
|
def tirage(request, tirage_id):
|
2016-07-08 20:33:26 +02:00
|
|
|
tirage_elt = get_object_or_404(Tirage, id=tirage_id)
|
2018-10-06 12:35:49 +02:00
|
|
|
if not (tirage_elt.enable_do_tirage and tirage_elt.fermeture < timezone.now()):
|
|
|
|
return render(request, "tirage-failed.html", {"tirage": tirage_elt})
|
2013-09-05 22:20:52 +02:00
|
|
|
if request.POST:
|
|
|
|
form = TokenForm(request.POST)
|
|
|
|
if form.is_valid():
|
2018-10-06 12:35:49 +02:00
|
|
|
results = do_tirage(tirage_elt, form.cleaned_data["token"])
|
2017-02-03 17:07:50 +01:00
|
|
|
return render(request, "bda-attrib-extra.html", results)
|
2013-09-05 22:20:52 +02:00
|
|
|
else:
|
|
|
|
form = TokenForm()
|
|
|
|
return render(request, "bda-token.html", {"form": form})
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2019-01-10 10:31:58 +01:00
|
|
|
@cof_required
|
2017-10-23 18:39:45 +02:00
|
|
|
def revente_manage(request, tirage_id):
|
2017-11-01 17:26:40 +01:00
|
|
|
"""
|
|
|
|
Gestion de ses propres reventes :
|
|
|
|
- Création d'une revente
|
|
|
|
- Annulation d'une revente
|
|
|
|
- Confirmation d'une revente = transfert de la place à la personne qui
|
|
|
|
rachète
|
|
|
|
- Annulation d'une revente après que le tirage a eu lieu
|
|
|
|
"""
|
2016-06-05 02:18:12 +02:00
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
|
|
|
participant, created = Participant.objects.get_or_create(
|
2018-10-06 12:35:49 +02:00
|
|
|
user=request.user, tirage=tirage
|
|
|
|
)
|
2017-02-16 04:52:44 +01:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
if not participant.paid:
|
2017-10-23 18:39:45 +02:00
|
|
|
return render(request, "bda/revente/notpaid.html", {})
|
2017-02-16 04:52:44 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
resellform = ResellForm(participant, prefix="resell")
|
|
|
|
annulform = AnnulForm(participant, prefix="annul")
|
|
|
|
soldform = SoldForm(participant, prefix="sold")
|
2017-02-16 04:52:44 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
if request.method == "POST":
|
2016-12-20 22:24:07 +01:00
|
|
|
# On met en vente une place
|
2018-10-06 12:35:49 +02:00
|
|
|
if "resell" in request.POST:
|
|
|
|
resellform = ResellForm(participant, request.POST, prefix="resell")
|
2016-07-27 13:08:00 +02:00
|
|
|
if resellform.is_valid():
|
2017-02-16 12:55:19 +01:00
|
|
|
datatuple = []
|
2016-07-27 13:08:00 +02:00
|
|
|
attributions = resellform.cleaned_data["attributions"]
|
2016-11-05 22:35:46 +01:00
|
|
|
with transaction.atomic():
|
|
|
|
for attribution in attributions:
|
2018-10-06 12:35:49 +02:00
|
|
|
revente, created = SpectacleRevente.objects.get_or_create(
|
|
|
|
attribution=attribution, defaults={"seller": participant}
|
|
|
|
)
|
2016-11-05 22:35:46 +01:00
|
|
|
if not created:
|
2017-10-23 18:59:30 +02:00
|
|
|
revente.reset()
|
2017-10-26 12:40:11 +02:00
|
|
|
|
2017-02-16 12:55:19 +01:00
|
|
|
context = {
|
2018-10-06 12:35:49 +02:00
|
|
|
"vendeur": participant.user,
|
|
|
|
"show": attribution.spectacle,
|
|
|
|
"revente": revente,
|
2017-02-16 12:55:19 +01:00
|
|
|
}
|
2018-10-06 12:35:49 +02:00
|
|
|
datatuple.append(
|
|
|
|
(
|
|
|
|
"bda-revente-new",
|
|
|
|
context,
|
|
|
|
settings.MAIL_DATA["revente"]["FROM"],
|
|
|
|
[participant.user.email],
|
|
|
|
)
|
|
|
|
)
|
2017-02-16 12:55:19 +01:00
|
|
|
revente.save()
|
|
|
|
send_mass_custom_mail(datatuple)
|
2016-12-20 22:24:07 +01:00
|
|
|
# On annule une revente
|
2018-10-06 12:35:49 +02:00
|
|
|
elif "annul" in request.POST:
|
|
|
|
annulform = AnnulForm(participant, request.POST, prefix="annul")
|
2016-07-27 13:08:00 +02:00
|
|
|
if annulform.is_valid():
|
2017-12-19 12:41:50 +01:00
|
|
|
reventes = annulform.cleaned_data["reventes"]
|
|
|
|
for revente in reventes:
|
|
|
|
revente.delete()
|
2016-12-20 22:24:07 +01:00
|
|
|
# On confirme une vente en transférant la place à la personne qui a
|
|
|
|
# gagné le tirage
|
2018-10-06 12:35:49 +02:00
|
|
|
elif "transfer" in request.POST:
|
|
|
|
soldform = SoldForm(participant, request.POST, prefix="sold")
|
2017-02-16 04:52:44 +01:00
|
|
|
if soldform.is_valid():
|
2018-10-06 12:35:49 +02:00
|
|
|
reventes = soldform.cleaned_data["reventes"]
|
2018-05-30 10:13:37 +02:00
|
|
|
for revente in reventes:
|
2017-12-19 12:41:50 +01:00
|
|
|
revente.attribution.participant = revente.soldTo
|
|
|
|
revente.attribution.save()
|
2017-02-16 04:52:44 +01:00
|
|
|
|
2016-12-20 22:24:07 +01:00
|
|
|
# On annule la revente après le tirage au sort (par exemple si
|
|
|
|
# la personne qui a gagné le tirage ne se manifeste pas). La place est
|
|
|
|
# alors remise en vente
|
2018-10-06 12:35:49 +02:00
|
|
|
elif "reinit" in request.POST:
|
|
|
|
soldform = SoldForm(participant, request.POST, prefix="sold")
|
2017-02-16 04:52:44 +01:00
|
|
|
if soldform.is_valid():
|
2018-10-06 12:35:49 +02:00
|
|
|
reventes = soldform.cleaned_data["reventes"]
|
2017-12-19 12:41:50 +01:00
|
|
|
for revente in reventes:
|
|
|
|
if revente.attribution.spectacle.date > timezone.now():
|
2017-10-26 12:40:11 +02:00
|
|
|
# On antidate pour envoyer le mail plus vite
|
2018-10-06 12:35:49 +02:00
|
|
|
new_date = timezone.now() - SpectacleRevente.remorse_time
|
2017-10-26 12:40:11 +02:00
|
|
|
revente.reset(new_date=new_date)
|
2016-09-05 02:29:49 +02:00
|
|
|
|
2019-01-07 14:43:32 +01:00
|
|
|
sold_exists = soldform.fields["reventes"].queryset.exists()
|
|
|
|
annul_exists = annulform.fields["reventes"].queryset.exists()
|
|
|
|
resell_exists = resellform.fields["attributions"].queryset.exists()
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"bda/revente/manage.html",
|
|
|
|
{
|
|
|
|
"tirage": tirage,
|
|
|
|
"soldform": soldform,
|
|
|
|
"annulform": annulform,
|
|
|
|
"resellform": resellform,
|
2019-01-07 14:43:32 +01:00
|
|
|
"sold_exists": sold_exists,
|
|
|
|
"annul_exists": annul_exists,
|
|
|
|
"resell_exists": resell_exists,
|
2018-10-06 12:35:49 +02:00
|
|
|
},
|
|
|
|
)
|
2013-09-05 22:20:52 +02:00
|
|
|
|
|
|
|
|
2019-01-10 10:31:58 +01:00
|
|
|
@cof_required
|
2017-10-23 17:25:58 +02:00
|
|
|
def revente_tirages(request, tirage_id):
|
2017-11-01 17:26:40 +01:00
|
|
|
"""
|
|
|
|
Affiche à un participant la liste de toutes les reventes en cours (pour un
|
|
|
|
tirage donné) et lui permet de s'inscrire et se désinscrire à ces reventes.
|
|
|
|
"""
|
2017-10-23 17:25:58 +02:00
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
2018-10-06 12:35:49 +02:00
|
|
|
participant, _ = Participant.objects.get_or_create(user=request.user, tirage=tirage)
|
2017-10-23 17:25:58 +02:00
|
|
|
subform = ReventeTirageForm(participant, prefix="subscribe")
|
|
|
|
annulform = ReventeTirageAnnulForm(participant, prefix="annul")
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
if request.method == "POST":
|
2017-10-23 17:25:58 +02:00
|
|
|
if "subscribe" in request.POST:
|
2018-10-06 12:35:49 +02:00
|
|
|
subform = ReventeTirageForm(participant, request.POST, prefix="subscribe")
|
2017-10-23 17:25:58 +02:00
|
|
|
if subform.is_valid():
|
2018-10-06 12:35:49 +02:00
|
|
|
reventes = subform.cleaned_data["reventes"]
|
2017-10-26 12:40:11 +02:00
|
|
|
count = reventes.count()
|
2017-10-23 17:25:58 +02:00
|
|
|
for revente in reventes:
|
2017-10-23 20:52:25 +02:00
|
|
|
revente.confirmed_entry.add(participant)
|
2017-10-26 12:40:11 +02:00
|
|
|
if count > 0:
|
2017-10-23 17:25:58 +02:00
|
|
|
messages.success(
|
|
|
|
request,
|
2018-10-06 12:35:49 +02:00
|
|
|
"Tu as bien été inscrit à {} revente{}".format(
|
|
|
|
count, pluralize(count)
|
|
|
|
),
|
2017-10-23 17:25:58 +02:00
|
|
|
)
|
|
|
|
elif "annul" in request.POST:
|
2018-10-06 12:35:49 +02:00
|
|
|
annulform = ReventeTirageAnnulForm(
|
|
|
|
participant, request.POST, prefix="annul"
|
|
|
|
)
|
2017-10-23 17:25:58 +02:00
|
|
|
if annulform.is_valid():
|
2018-10-06 12:35:49 +02:00
|
|
|
reventes = annulform.cleaned_data["reventes"]
|
2017-10-26 12:40:11 +02:00
|
|
|
count = reventes.count()
|
2017-10-23 17:25:58 +02:00
|
|
|
for revente in reventes:
|
2017-10-23 20:52:25 +02:00
|
|
|
revente.confirmed_entry.remove(participant)
|
2017-10-26 12:40:11 +02:00
|
|
|
if count > 0:
|
2017-10-23 17:25:58 +02:00
|
|
|
messages.success(
|
|
|
|
request,
|
2018-10-06 12:35:49 +02:00
|
|
|
"Tu as bien été désinscrit de {} revente{}".format(
|
|
|
|
count, pluralize(count)
|
|
|
|
),
|
2017-10-23 17:25:58 +02:00
|
|
|
)
|
|
|
|
|
2018-12-07 17:35:40 +01:00
|
|
|
annul_exists = annulform.fields["reventes"].queryset.exists()
|
|
|
|
sub_exists = subform.fields["reventes"].queryset.exists()
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"bda/revente/tirages.html",
|
2018-12-08 10:41:46 +01:00
|
|
|
{
|
|
|
|
"annulform": annulform,
|
|
|
|
"subform": subform,
|
|
|
|
"annul_exists": annul_exists,
|
|
|
|
"sub_exists": sub_exists,
|
|
|
|
},
|
2018-10-06 12:35:49 +02:00
|
|
|
)
|
2017-10-23 17:25:58 +02:00
|
|
|
|
|
|
|
|
2019-01-10 10:31:58 +01:00
|
|
|
@cof_required
|
2017-10-23 18:39:45 +02:00
|
|
|
def revente_confirm(request, revente_id):
|
2016-09-04 11:14:09 +02:00
|
|
|
revente = get_object_or_404(SpectacleRevente, id=revente_id)
|
2016-12-22 12:28:03 +01:00
|
|
|
participant, _ = Participant.objects.get_or_create(
|
2018-10-06 12:35:49 +02:00
|
|
|
user=request.user, tirage=revente.attribution.spectacle.tirage
|
|
|
|
)
|
2017-10-23 18:59:30 +02:00
|
|
|
if not revente.notif_sent or revente.shotgun:
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(request, "bda/revente/wrongtime.html", {"revente": revente})
|
2016-09-04 11:14:09 +02:00
|
|
|
|
2017-10-23 20:52:25 +02:00
|
|
|
revente.confirmed_entry.add(participant)
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"bda/revente/confirmed.html",
|
|
|
|
{"spectacle": revente.attribution.spectacle, "date": revente.date_tirage},
|
|
|
|
)
|
2016-09-04 11:14:09 +02:00
|
|
|
|
|
|
|
|
2019-01-10 10:31:58 +01:00
|
|
|
@cof_required
|
2017-10-23 18:39:45 +02:00
|
|
|
def revente_subscribe(request, tirage_id):
|
2017-11-01 17:26:40 +01:00
|
|
|
"""
|
|
|
|
Permet à un participant de sélectionner ses préférences pour les reventes.
|
|
|
|
Il recevra des notifications pour les spectacles qui l'intéressent et il
|
|
|
|
est automatiquement inscrit aux reventes en cours au moment où il ajoute un
|
|
|
|
spectacle à la liste des spectacles qui l'intéressent.
|
|
|
|
"""
|
2016-07-25 02:52:49 +02:00
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
2018-10-06 12:35:49 +02:00
|
|
|
participant, _ = Participant.objects.get_or_create(user=request.user, tirage=tirage)
|
2016-09-05 03:32:29 +02:00
|
|
|
deja_revente = False
|
2016-09-27 17:35:29 +02:00
|
|
|
success = False
|
2017-01-29 23:05:59 +01:00
|
|
|
inscrit_revente = []
|
2018-10-06 12:35:49 +02:00
|
|
|
if request.method == "POST":
|
2016-07-27 13:08:00 +02:00
|
|
|
form = InscriptionReventeForm(tirage, request.POST)
|
|
|
|
if form.is_valid():
|
2018-10-06 12:35:49 +02:00
|
|
|
choices = form.cleaned_data["spectacles"]
|
2016-07-27 13:08:00 +02:00
|
|
|
participant.choicesrevente = choices
|
|
|
|
participant.save()
|
2016-09-05 03:32:29 +02:00
|
|
|
for spectacle in choices:
|
2018-10-06 12:35:49 +02:00
|
|
|
qset = SpectacleRevente.objects.filter(attribution__spectacle=spectacle)
|
2016-12-21 02:15:50 +01:00
|
|
|
if qset.filter(shotgun=True, soldTo__isnull=True).exists():
|
|
|
|
# Une place est disponible au shotgun, on suggère à
|
|
|
|
# l'utilisateur d'aller la récupérer
|
|
|
|
deja_revente = True
|
|
|
|
else:
|
|
|
|
# La place n'est pas disponible au shotgun, si des reventes
|
|
|
|
# pour ce spectacle existent déjà, on inscrit la personne à
|
|
|
|
# la revente ayant le moins d'inscrits
|
|
|
|
min_resell = (
|
|
|
|
qset.filter(shotgun=False)
|
2018-10-06 12:35:49 +02:00
|
|
|
.annotate(nb_subscribers=Count("confirmed_entry"))
|
|
|
|
.order_by("nb_subscribers")
|
2016-12-21 02:15:50 +01:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if min_resell is not None:
|
2017-10-23 20:52:25 +02:00
|
|
|
min_resell.confirmed_entry.add(participant)
|
2017-01-29 23:05:59 +01:00
|
|
|
inscrit_revente.append(spectacle)
|
2016-09-27 17:35:29 +02:00
|
|
|
success = True
|
2016-07-27 13:08:00 +02:00
|
|
|
else:
|
|
|
|
form = InscriptionReventeForm(
|
2018-10-06 12:35:49 +02:00
|
|
|
tirage, initial={"spectacles": participant.choicesrevente.all()}
|
2017-02-11 20:55:17 +01:00
|
|
|
)
|
2017-01-29 23:05:59 +01:00
|
|
|
# Messages
|
|
|
|
if success:
|
2018-11-21 16:14:52 +01:00
|
|
|
messages.success(request, "Votre inscription a bien été prise en compte")
|
2017-01-29 23:05:59 +01:00
|
|
|
if deja_revente:
|
2018-10-06 12:35:49 +02:00
|
|
|
messages.info(
|
|
|
|
request,
|
|
|
|
"Des reventes existent déjà pour certains de "
|
2018-11-21 16:14:52 +01:00
|
|
|
"ces spectacles, vérifiez les places "
|
2018-10-06 12:35:49 +02:00
|
|
|
"disponibles sans tirage !",
|
|
|
|
)
|
2017-02-04 00:01:15 +01:00
|
|
|
if inscrit_revente:
|
|
|
|
shows = map("<li>{!s}</li>".format, inscrit_revente)
|
|
|
|
msg = (
|
2018-11-21 16:14:52 +01:00
|
|
|
"Vous avez été inscrit·e à des reventes en cours pour les spectacles "
|
2018-10-06 12:35:49 +02:00
|
|
|
"<ul>{:s}</ul>".format("\n".join(shows))
|
2017-02-04 00:01:15 +01:00
|
|
|
)
|
|
|
|
messages.info(request, msg, extra_tags="safe")
|
2017-01-29 23:05:59 +01:00
|
|
|
|
2017-10-23 18:39:45 +02:00
|
|
|
return render(request, "bda/revente/subscribe.html", {"form": form})
|
2016-07-25 02:52:49 +02:00
|
|
|
|
|
|
|
|
2019-01-10 10:31:58 +01:00
|
|
|
@cof_required
|
2017-10-23 18:39:45 +02:00
|
|
|
def revente_buy(request, spectacle_id):
|
2016-07-25 02:52:49 +02:00
|
|
|
spectacle = get_object_or_404(Spectacle, id=spectacle_id)
|
|
|
|
tirage = spectacle.tirage
|
2018-10-06 12:35:49 +02:00
|
|
|
participant, _ = Participant.objects.get_or_create(user=request.user, tirage=tirage)
|
2016-07-25 02:52:49 +02:00
|
|
|
reventes = SpectacleRevente.objects.filter(
|
2018-10-06 12:35:49 +02:00
|
|
|
attribution__spectacle=spectacle, soldTo__isnull=True
|
|
|
|
)
|
2016-11-13 01:45:52 +01:00
|
|
|
|
|
|
|
# Si l'utilisateur veut racheter une place qu'il est en train de revendre,
|
|
|
|
# on supprime la revente en question.
|
2017-04-08 12:53:37 +02:00
|
|
|
own_reventes = reventes.filter(seller=participant)
|
|
|
|
if len(own_reventes) > 0:
|
|
|
|
own_reventes[0].delete()
|
2018-10-06 12:35:49 +02:00
|
|
|
return HttpResponseRedirect(reverse("bda-revente-shotgun", args=[tirage.id]))
|
2016-09-03 18:47:38 +02:00
|
|
|
|
2017-04-08 12:53:37 +02:00
|
|
|
reventes_shotgun = reventes.filter(shotgun=True)
|
2016-11-13 01:45:52 +01:00
|
|
|
|
|
|
|
if not reventes_shotgun:
|
2017-10-23 18:39:45 +02:00
|
|
|
return render(request, "bda/revente/none.html", {})
|
2016-09-03 18:47:38 +02:00
|
|
|
|
2016-07-25 02:52:49 +02:00
|
|
|
if request.POST:
|
2016-11-13 01:45:52 +01:00
|
|
|
revente = random.choice(reventes_shotgun)
|
2016-09-19 16:08:12 +02:00
|
|
|
revente.soldTo = participant
|
|
|
|
revente.save()
|
2016-12-22 02:00:10 +01:00
|
|
|
context = {
|
2018-10-06 12:35:49 +02:00
|
|
|
"show": spectacle,
|
|
|
|
"acheteur": request.user,
|
|
|
|
"vendeur": revente.seller.user,
|
2016-12-22 02:00:10 +01:00
|
|
|
}
|
|
|
|
send_custom_mail(
|
2018-10-06 12:35:49 +02:00
|
|
|
"bda-buy-shotgun",
|
|
|
|
"bda@ens.fr",
|
2017-02-11 16:15:17 +01:00
|
|
|
[revente.seller.user.email],
|
2016-12-22 02:00:10 +01:00
|
|
|
context=context,
|
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"bda/revente/mail-success.html",
|
|
|
|
{"seller": revente.attribution.participant.user, "spectacle": spectacle},
|
|
|
|
)
|
2016-07-25 02:52:49 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"bda/revente/confirm-shotgun.html",
|
|
|
|
{"spectacle": spectacle, "user": request.user},
|
|
|
|
)
|
2016-07-25 02:52:49 +02:00
|
|
|
|
|
|
|
|
2019-01-10 10:31:58 +01:00
|
|
|
@cof_required
|
2016-10-06 13:46:18 +02:00
|
|
|
def revente_shotgun(request, tirage_id):
|
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
2017-04-08 12:53:37 +02:00
|
|
|
spectacles = (
|
2018-10-06 12:35:49 +02:00
|
|
|
tirage.spectacle_set.filter(date__gte=timezone.now())
|
|
|
|
.select_related("location")
|
|
|
|
.prefetch_related(
|
|
|
|
Prefetch(
|
|
|
|
"attribues",
|
|
|
|
queryset=(
|
|
|
|
Attribution.objects.filter(
|
|
|
|
revente__shotgun=True, revente__soldTo__isnull=True
|
|
|
|
)
|
|
|
|
),
|
|
|
|
to_attr="shotguns",
|
|
|
|
)
|
|
|
|
)
|
2017-04-08 12:53:37 +02:00
|
|
|
)
|
|
|
|
shotgun = [sp for sp in spectacles if len(sp.shotguns) > 0]
|
2016-10-06 13:46:18 +02:00
|
|
|
|
2018-12-07 17:35:53 +01:00
|
|
|
return render(request, "bda/revente/shotgun.html", {"spectacles": shotgun})
|
2016-10-06 13:46:18 +02:00
|
|
|
|
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
@buro_required
|
2016-06-05 02:18:12 +02:00
|
|
|
def spectacle(request, tirage_id, spectacle_id):
|
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
2016-06-15 21:19:39 +02:00
|
|
|
spectacle = get_object_or_404(Spectacle, id=spectacle_id, tirage=tirage)
|
2018-10-06 12:35:49 +02:00
|
|
|
attributions = spectacle.attribues.select_related(
|
|
|
|
"participant", "participant__user"
|
2017-04-08 13:01:05 +02:00
|
|
|
)
|
2016-06-12 18:36:21 +02:00
|
|
|
participants = {}
|
|
|
|
for attrib in attributions:
|
|
|
|
participant = attrib.participant
|
2018-10-06 12:35:49 +02:00
|
|
|
participant_info = {
|
|
|
|
"lastname": participant.user.last_name,
|
|
|
|
"name": participant.user.get_full_name,
|
|
|
|
"username": participant.user.username,
|
|
|
|
"email": participant.user.email,
|
|
|
|
"given": int(attrib.given),
|
|
|
|
"paid": participant.paid,
|
|
|
|
"nb_places": 1,
|
|
|
|
}
|
2016-06-12 19:29:50 +02:00
|
|
|
if participant.id in participants:
|
2018-10-06 12:35:49 +02:00
|
|
|
participants[participant.id]["nb_places"] += 1
|
|
|
|
participants[participant.id]["given"] += attrib.given
|
2016-06-12 18:36:21 +02:00
|
|
|
else:
|
2016-06-15 19:34:10 +02:00
|
|
|
participants[participant.id] = participant_info
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
participants_info = sorted(participants.values(), key=lambda part: part["lastname"])
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"bda/participants.html",
|
|
|
|
{"spectacle": spectacle, "participants": participants_info},
|
|
|
|
)
|
2016-06-06 18:43:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SpectacleListView(ListView):
|
|
|
|
model = Spectacle
|
2018-10-06 12:35:49 +02:00
|
|
|
template_name = "spectacle_list.html"
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-06-06 18:43:56 +02:00
|
|
|
def get_queryset(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
self.tirage = get_object_or_404(Tirage, id=self.kwargs["tirage_id"])
|
|
|
|
categories = self.tirage.spectacle_set.select_related("location")
|
2016-06-06 18:43:56 +02:00
|
|
|
return categories
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-06-06 18:43:56 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
2018-01-16 16:22:52 +01:00
|
|
|
context = super().get_context_data(**kwargs)
|
2018-10-06 12:35:49 +02:00
|
|
|
context["tirage_id"] = self.tirage.id
|
|
|
|
context["tirage_name"] = self.tirage.title
|
2016-06-06 18:43:56 +02:00
|
|
|
return context
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
@buro_required
|
2016-06-05 02:18:12 +02:00
|
|
|
def unpaid(request, tirage_id):
|
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
2017-04-08 13:08:53 +02:00
|
|
|
unpaid = (
|
2018-10-06 12:35:49 +02:00
|
|
|
tirage.participant_set.annotate(nb_attributions=Count("attribution"))
|
2017-04-08 13:08:53 +02:00
|
|
|
.filter(paid=False, nb_attributions__gt=0)
|
2018-10-06 12:35:49 +02:00
|
|
|
.select_related("user")
|
2017-04-08 13:08:53 +02:00
|
|
|
)
|
2016-06-05 02:18:12 +02:00
|
|
|
return render(request, "bda-unpaid.html", {"unpaid": unpaid})
|
2016-05-21 23:57:36 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-06-10 23:53:29 +02:00
|
|
|
@buro_required
|
|
|
|
def send_rappel(request, spectacle_id):
|
|
|
|
show = get_object_or_404(Spectacle, id=spectacle_id)
|
|
|
|
# Mails d'exemples
|
2017-06-18 18:03:50 +02:00
|
|
|
custommail = CustomMail.objects.get(shortname="bda-rappel")
|
2018-10-06 12:35:49 +02:00
|
|
|
exemple_mail_1place = custommail.render(
|
|
|
|
{"member": request.user, "show": show, "nb_attr": 1}
|
|
|
|
)
|
|
|
|
exemple_mail_2places = custommail.render(
|
|
|
|
{"member": request.user, "show": show, "nb_attr": 2}
|
|
|
|
)
|
2016-07-10 14:19:19 +02:00
|
|
|
# Contexte
|
2017-06-02 20:33:23 +02:00
|
|
|
ctxt = {
|
2018-10-06 12:35:49 +02:00
|
|
|
"show": show,
|
|
|
|
"exemple_mail_1place": exemple_mail_1place,
|
|
|
|
"exemple_mail_2places": exemple_mail_2places,
|
|
|
|
"custommail": custommail,
|
2017-06-02 20:33:23 +02:00
|
|
|
}
|
2016-07-10 14:19:19 +02:00
|
|
|
# Envoi confirmé
|
2018-10-06 12:35:49 +02:00
|
|
|
if request.method == "POST":
|
2016-07-10 14:19:19 +02:00
|
|
|
members = show.send_rappel()
|
2018-10-06 12:35:49 +02:00
|
|
|
ctxt["sent"] = True
|
|
|
|
ctxt["members"] = members
|
2016-07-10 14:19:19 +02:00
|
|
|
# Demande de confirmation
|
|
|
|
else:
|
2018-10-06 12:35:49 +02:00
|
|
|
ctxt["sent"] = False
|
2017-06-02 19:01:27 +02:00
|
|
|
if show.rappel_sent:
|
|
|
|
messages.warning(
|
|
|
|
request,
|
|
|
|
"Attention, un mail de rappel pour ce spectale a déjà été "
|
2018-10-06 12:35:49 +02:00
|
|
|
"envoyé le {}".format(
|
|
|
|
formats.localize(timezone.template_localtime(show.rappel_sent))
|
|
|
|
),
|
2017-06-02 19:01:27 +02:00
|
|
|
)
|
2016-12-23 10:25:28 +01:00
|
|
|
return render(request, "bda/mails-rappel.html", ctxt)
|
2016-08-23 16:22:06 +02:00
|
|
|
|
|
|
|
|
2017-03-31 02:51:58 +02:00
|
|
|
def catalogue(request, request_type):
|
2017-03-15 07:37:24 +01:00
|
|
|
"""
|
|
|
|
Vue destinée à communiquer avec un client AJAX, fournissant soit :
|
|
|
|
- la liste des tirages
|
|
|
|
- les catégories et salles d'un tirage
|
|
|
|
- les descriptions d'un tirage (filtrées selon la catégorie et la salle)
|
|
|
|
"""
|
|
|
|
if request_type == "list":
|
2017-03-31 02:51:58 +02:00
|
|
|
# Dans ce cas on retourne la liste des tirages et de leur id en JSON
|
2017-03-31 03:35:09 +02:00
|
|
|
data_return = list(
|
2018-10-06 12:35:49 +02:00
|
|
|
Tirage.objects.filter(appear_catalogue=True).values("id", "title")
|
2017-04-05 20:33:18 +02:00
|
|
|
)
|
2017-03-31 03:15:40 +02:00
|
|
|
return JsonResponse(data_return, safe=False)
|
2017-03-15 07:37:24 +01:00
|
|
|
if request_type == "details":
|
2017-03-31 02:51:58 +02:00
|
|
|
# Dans ce cas on retourne une liste des catégories et des salles
|
2018-10-06 12:35:49 +02:00
|
|
|
tirage_id = request.GET.get("id", None)
|
2017-04-05 20:33:18 +02:00
|
|
|
if tirage_id is None:
|
2018-10-06 12:35:49 +02:00
|
|
|
return HttpResponseBadRequest("Missing GET parameter: id <int>")
|
2017-04-05 20:33:18 +02:00
|
|
|
try:
|
|
|
|
tirage = get_object_or_404(Tirage, id=int(tirage_id))
|
2017-03-31 03:47:32 +02:00
|
|
|
except ValueError:
|
2018-10-06 12:35:49 +02:00
|
|
|
return HttpResponseBadRequest("Bad format: int expected for `id`")
|
2017-04-05 21:48:18 +02:00
|
|
|
shows = tirage.spectacle_set.values_list("id", flat=True)
|
2017-03-31 03:35:09 +02:00
|
|
|
categories = list(
|
2018-10-06 12:35:49 +02:00
|
|
|
CategorieSpectacle.objects.filter(spectacle__in=shows)
|
2017-04-05 20:33:18 +02:00
|
|
|
.distinct()
|
2018-10-06 12:35:49 +02:00
|
|
|
.values("id", "name")
|
2017-04-05 20:33:18 +02:00
|
|
|
)
|
2017-03-31 03:35:09 +02:00
|
|
|
locations = list(
|
2018-10-06 12:35:49 +02:00
|
|
|
Salle.objects.filter(spectacle__in=shows).distinct().values("id", "name")
|
2017-04-05 20:33:18 +02:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
data_return = {"categories": categories, "locations": locations}
|
2017-03-31 03:15:40 +02:00
|
|
|
return JsonResponse(data_return, safe=False)
|
2017-03-15 07:37:24 +01:00
|
|
|
if request_type == "descriptions":
|
2017-03-31 02:51:58 +02:00
|
|
|
# Ici on retourne les descriptions correspondant à la catégorie et
|
|
|
|
# à la salle spécifiées
|
2017-03-31 04:33:13 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
tirage_id = request.GET.get("id", "")
|
|
|
|
categories = request.GET.get("category", "[]")
|
|
|
|
locations = request.GET.get("location", "[]")
|
2017-03-15 07:37:24 +01:00
|
|
|
try:
|
2017-04-05 20:33:18 +02:00
|
|
|
tirage_id = int(tirage_id)
|
|
|
|
categories_id = json.loads(categories)
|
|
|
|
locations_id = json.loads(locations)
|
|
|
|
# Integers expected
|
|
|
|
if not all(isinstance(id, int) for id in categories_id):
|
|
|
|
raise ValueError
|
|
|
|
if not all(isinstance(id, int) for id in locations_id):
|
|
|
|
raise ValueError
|
2017-04-01 16:34:17 +02:00
|
|
|
except ValueError: # Contient JSONDecodeError
|
|
|
|
return HttpResponseBadRequest(
|
2017-04-05 20:33:18 +02:00
|
|
|
"Parse error, please ensure the GET parameters have the "
|
|
|
|
"following types:\n"
|
|
|
|
"id: int, category: [int], location: [int]\n"
|
|
|
|
"Data received:\n"
|
2018-10-06 12:35:49 +02:00
|
|
|
"id = {}, category = {}, locations = {}".format(
|
|
|
|
request.GET.get("id", ""),
|
|
|
|
request.GET.get("category", "[]"),
|
|
|
|
request.GET.get("location", "[]"),
|
|
|
|
)
|
2017-04-05 00:51:22 +02:00
|
|
|
)
|
2017-04-05 20:33:18 +02:00
|
|
|
tirage = get_object_or_404(Tirage, id=tirage_id)
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
shows_qs = tirage.spectacle_set.select_related("location").prefetch_related(
|
|
|
|
"quote_set"
|
2017-04-08 13:44:21 +02:00
|
|
|
)
|
2017-09-20 18:21:59 +02:00
|
|
|
if categories_id and 0 not in categories_id:
|
2017-04-05 20:33:18 +02:00
|
|
|
shows_qs = shows_qs.filter(category__id__in=categories_id)
|
2017-09-20 18:21:59 +02:00
|
|
|
if locations_id and 0 not in locations_id:
|
2017-04-05 20:33:18 +02:00
|
|
|
shows_qs = shows_qs.filter(location__id__in=locations_id)
|
2017-03-15 07:37:24 +01:00
|
|
|
|
2017-03-31 02:51:58 +02:00
|
|
|
# On convertit les descriptions à envoyer en une liste facilement
|
|
|
|
# JSONifiable (il devrait y avoir un moyen plus efficace en
|
|
|
|
# redéfinissant le serializer de JSON)
|
2018-10-06 12:35:49 +02:00
|
|
|
data_return = [
|
|
|
|
{
|
|
|
|
"title": spectacle.title,
|
|
|
|
"category": str(spectacle.category),
|
|
|
|
"date": str(
|
|
|
|
formats.date_format(
|
|
|
|
timezone.localtime(spectacle.date), "SHORT_DATETIME_FORMAT"
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"location": str(spectacle.location),
|
|
|
|
"vips": spectacle.vips,
|
|
|
|
"description": spectacle.description,
|
|
|
|
"slots_description": spectacle.slots_description,
|
|
|
|
"quotes": [
|
|
|
|
dict(author=quote.author, text=quote.text)
|
|
|
|
for quote in spectacle.quote_set.all()
|
|
|
|
],
|
|
|
|
"image": spectacle.getImgUrl(),
|
|
|
|
"ext_link": spectacle.ext_link,
|
|
|
|
"price": spectacle.price,
|
|
|
|
"slots": spectacle.slots,
|
2017-03-31 02:51:58 +02:00
|
|
|
}
|
2017-04-08 13:44:21 +02:00
|
|
|
for spectacle in shows_qs
|
2017-03-31 02:51:58 +02:00
|
|
|
]
|
2017-03-31 03:15:40 +02:00
|
|
|
return JsonResponse(data_return, safe=False)
|
2017-03-31 02:51:58 +02:00
|
|
|
# Si la requête n'est pas de la forme attendue, on quitte avec une erreur
|
|
|
|
return HttpResponseBadRequest()
|
2017-11-19 18:41:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Autocomplete views
|
|
|
|
#
|
|
|
|
# https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#create-an-autocomplete-view
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
class ParticipantAutocomplete(Select2QuerySetView):
|
|
|
|
model = Participant
|
2018-10-06 12:35:49 +02:00
|
|
|
search_fields = ("user__username", "user__first_name", "user__last_name")
|
2017-11-19 18:41:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
participant_autocomplete = buro_required(ParticipantAutocomplete.as_view())
|
|
|
|
|
|
|
|
|
|
|
|
class SpectacleAutocomplete(Select2QuerySetView):
|
|
|
|
model = Spectacle
|
2018-10-06 12:35:49 +02:00
|
|
|
search_fields = ("title",)
|
2017-11-19 18:41:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
spectacle_autocomplete = buro_required(SpectacleAutocomplete.as_view())
|