gestioCOF/bda/views.py

700 lines
28 KiB
Python
Raw Normal View History

2016-07-15 00:02:56 +02:00
# -*- coding: utf-8 -*-
2012-07-11 17:39:20 +02:00
2016-07-25 02:52:49 +02:00
import random
import hashlib
import time
import json
from datetime import timedelta
from more_itertools import unique_everseen
from custommail.shortcuts import (
send_mass_custom_mail, send_custom_mail, render_custom_mail
)
2016-07-25 02:52:49 +02:00
from django.shortcuts import render, get_object_or_404
2012-07-11 17:39:20 +02:00
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.db import models, transaction
from django.core import serializers
from django.db.models import Count, Q, Sum
from django.forms.models import inlineformset_factory
from django.http import HttpResponseBadRequest, HttpResponseRedirect, HttpResponse
2016-09-21 15:39:01 +02:00
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils import timezone, formats
from django.views.generic.list import ListView
from gestioncof.decorators import cof_required, buro_required
from bda.models import Spectacle, Participant, ChoixSpectacle, Attribution,\
2017-03-16 05:50:02 +01:00
Tirage, SpectacleRevente, Salle, Quote
from bda.algorithm import Algorithm
2016-07-27 13:08:00 +02:00
from bda.forms import BaseBdaFormSet, TokenForm, ResellForm, AnnulForm,\
2017-02-16 04:52:44 +01:00
InscriptionReventeForm, SoldForm
2012-07-11 17:39:20 +02:00
2013-10-01 15:27:19 +02:00
@cof_required
def etat_places(request, tirage_id):
"""
Résumé des spectacles d'un tirage avec pour chaque spectacle :
- Le nombre de places en jeu
- Le nombre de demandes
- Le ratio demandes/places
Et le total de toutes les demandes
"""
tirage = get_object_or_404(Tirage, id=tirage_id)
2016-06-06 11:11:33 +02:00
spectacles1 = ChoixSpectacle.objects \
.filter(spectacle__tirage=tirage) \
.filter(double_choice="1") \
.all() \
.values('spectacle', 'spectacle__title') \
2016-06-06 11:11:33 +02:00
.annotate(total=models.Count('spectacle'))
spectacles2 = ChoixSpectacle.objects \
.filter(spectacle__tirage=tirage) \
.exclude(double_choice="1") \
.all() \
.values('spectacle', 'spectacle__title') \
2016-06-06 11:11:33 +02:00
.annotate(total=models.Count('spectacle'))
spectacles = tirage.spectacle_set.all()
spectacles_dict = {}
total = 0
for spectacle in spectacles:
spectacle.total = 0
2015-01-06 11:01:15 +01:00
spectacle.ratio = 0.0
spectacles_dict[spectacle.id] = spectacle
for spectacle in spectacles1:
spectacles_dict[spectacle["spectacle"]].total += spectacle["total"]
spectacles_dict[spectacle["spectacle"]].ratio = \
spectacles_dict[spectacle["spectacle"]].total / \
spectacles_dict[spectacle["spectacle"]].slots
total += spectacle["total"]
for spectacle in spectacles2:
spectacles_dict[spectacle["spectacle"]].total += 2*spectacle["total"]
spectacles_dict[spectacle["spectacle"]].ratio = \
spectacles_dict[spectacle["spectacle"]].total / \
spectacles_dict[spectacle["spectacle"]].slots
total += 2*spectacle["total"]
context = {
"proposed": tirage.spectacle_set.aggregate(Sum('slots'))['slots__sum'],
"spectacles": spectacles,
"total": total,
'tirage': tirage
}
return render(request, "bda/etat-places.html", context)
def _hash_queryset(queryset):
data = serializers.serialize("json", queryset).encode('utf-8')
hasher = hashlib.sha256()
hasher.update(data)
return hasher.hexdigest()
@cof_required
def places(request, tirage_id):
tirage = get_object_or_404(Tirage, id=tirage_id)
participant, created = Participant.objects.get_or_create(
user=request.user, tirage=tirage)
places = participant.attribution_set.order_by(
"spectacle__date", "spectacle").all()
total = sum([place.spectacle.price for place in places])
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:
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,
2017-01-29 22:49:32 +01:00
"total": total})
@cof_required
def inscription(request, tirage_id):
"""
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 le
client demande le formulaire et le moment il soumet son inscription
(autre session par exemple).
"""
tirage = get_object_or_404(Tirage, id=tirage_id)
if timezone.now() < tirage.ouverture:
# Le tirage n'est pas encore ouvert.
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', {})
if timezone.now() > tirage.fermeture:
# Le tirage est fermé.
participant, created = Participant.objects.get_or_create(
user=request.user, tirage=tirage)
choices = participant.choixspectacle_set.order_by("priority").all()
messages.error(request,
" C'est fini : tirage au sort dans la journée !")
return render(request, "bda/resume-inscription-tirage.html",
{"choices": choices})
def formfield_callback(f, **kwargs):
"""
Fonction utilisée par inlineformset_factory ci dessous.
Restreint les spectacles proposés aux spectacles du bo tirage.
"""
if f.name == "spectacle":
kwargs['queryset'] = tirage.spectacle_set
return f.formfield(**kwargs)
BdaFormSet = inlineformset_factory(
Participant,
ChoixSpectacle,
fields=("spectacle", "double_choice", "priority"),
formset=BaseBdaFormSet,
formfield_callback=formfield_callback)
participant, created = Participant.objects.get_or_create(
user=request.user, tirage=tirage)
2012-07-11 17:39:20 +02:00
success = False
stateerror = False
2012-07-11 17:39:20 +02:00
if request.method == "POST":
dbstate = _hash_queryset(participant.choixspectacle_set.all())
if "dbstate" in request.POST and dbstate != request.POST["dbstate"]:
stateerror = True
formset = BdaFormSet(instance=participant)
else:
formset = BdaFormSet(request.POST, instance=participant)
if formset.is_valid():
formset.save()
success = True
formset = BdaFormSet(instance=participant)
2012-07-11 17:39:20 +02:00
else:
formset = BdaFormSet(instance=participant)
dbstate = _hash_queryset(participant.choixspectacle_set.all())
total_price = 0
for choice in participant.choixspectacle_set.all():
total_price += choice.spectacle.price
if choice.double:
total_price += choice.spectacle.price
# Messages
if success:
messages.success(request, "Votre inscription a été mise à jour avec "
"succès !")
if stateerror:
messages.error(request, "Impossible d'enregistrer vos modifications "
": vous avez apporté d'autres modifications "
"entre temps.")
return render(request, "bda/inscription-tirage.html",
{"formset": formset,
"total_price": total_price,
"dbstate": dbstate,
'tirage': tirage})
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
start = time.time()
2017-02-03 17:07:50 +01:00
data = {
'shows': tirage_elt.spectacle_set.select_related().all(),
'token': token,
'members': tirage_elt.participant_set.all(),
'total_slots': 0,
'total_losers': 0,
'total_sold': 0,
'total_deficit': 0,
'opera_deficit': 0,
}
# On lance le tirage
choices = (
ChoixSpectacle.objects
.filter(spectacle__tirage=tirage_elt)
.order_by('participant', 'priority')
.select_related().all()
)
results = Algorithm(data['shows'], data['members'], choices)(token)
# On compte les places attribuées et les déçus
for (_, members, losers) in results:
2017-02-03 17:07:50 +01:00
data['total_slots'] += len(members)
data['total_losers'] += len(losers)
# On calcule le déficit et les bénéfices pour le BdA
# FIXME: le traitement de l'opéra est sale
for (show, members, _) in results:
deficit = (show.slots - len(members)) * show.price
2017-02-03 17:07:50 +01:00
data['total_sold'] += show.slots * show.price
if deficit >= 0:
if "Opéra" in show.location.name:
2017-02-03 17:07:50 +01:00
data['opera_deficit'] += deficit
data['total_deficit'] += deficit
data["total_sold"] -= data['total_deficit']
# 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(
timezone.now().strftime("%y-%m-%d %H:%M:%S"),
token)
tirage_elt.enable_do_tirage = False
tirage_elt.save()
# On enregistre les nouvelles attributions
Attribution.objects.bulk_create([
Attribution(spectacle=show, participant=member)
for show, members, _ in results
for member, _, _, _ in members
])
# On inscrit à BdA-Revente ceux qui n'ont pas eu les places voulues
for (show, _, losers) in results:
for (loser, _, _, _) in losers:
loser.choicesrevente.add(show)
loser.save()
data["duration"] = time.time() - start
2017-02-03 17:07:50 +01:00
data["results"] = results
return data
@buro_required
def tirage(request, tirage_id):
tirage_elt = get_object_or_404(Tirage, id=tirage_id)
if not (tirage_elt.enable_do_tirage
and tirage_elt.fermeture < timezone.now()):
return render(request, "tirage-failed.html", {'tirage': tirage_elt})
if request.POST:
form = TokenForm(request.POST)
if form.is_valid():
2017-02-03 17:07:50 +01:00
results = do_tirage(tirage_elt, form.cleaned_data['token'])
return render(request, "bda-attrib-extra.html", results)
else:
form = TokenForm()
return render(request, "bda-token.html", {"form": form})
@login_required
def 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)
2017-02-16 04:52:44 +01:00
if not participant.paid:
return render(request, "bda-notpaid.html", {})
2017-02-16 04:52:44 +01:00
resellform = ResellForm(participant, prefix='resell')
annulform = AnnulForm(participant, prefix='annul')
soldform = SoldForm(participant, prefix='sold')
2016-07-25 23:03:33 +02:00
if request.method == 'POST':
2016-12-20 22:24:07 +01:00
# On met en vente une place
2016-07-27 13:08:00 +02:00
if 'resell' in request.POST:
resellform = ResellForm(participant, request.POST, prefix='resell')
if resellform.is_valid():
datatuple = []
2016-07-27 13:08:00 +02:00
attributions = resellform.cleaned_data["attributions"]
with transaction.atomic():
for attribution in attributions:
2016-12-20 22:24:07 +01:00
revente, created = \
SpectacleRevente.objects.get_or_create(
attribution=attribution,
defaults={'seller': participant})
if not created:
revente.seller = participant
revente.date = timezone.now()
2016-12-21 03:45:30 +01:00
revente.soldTo = None
2016-11-13 01:45:52 +01:00
revente.notif_sent = False
revente.tirage_done = False
revente.shotgun = False
context = {
'vendeur': participant.user,
'show': attribution.spectacle,
'revente': revente
}
datatuple.append((
'bda-revente-new', context,
settings.MAIL_DATA['revente']['FROM'],
[participant.user.email]
))
revente.save()
send_mass_custom_mail(datatuple)
2016-12-20 22:24:07 +01:00
# On annule une revente
2016-07-27 13:08:00 +02:00
elif 'annul' in request.POST:
annulform = AnnulForm(participant, request.POST, prefix='annul')
if annulform.is_valid():
attributions = annulform.cleaned_data["attributions"]
for attribution in attributions:
attribution.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
elif 'transfer' in request.POST:
2017-02-16 04:52:44 +01:00
soldform = SoldForm(participant, request.POST, prefix='sold')
if soldform.is_valid():
attributions = soldform.cleaned_data['attributions']
for attribution in attributions:
attribution.participant = attribution.revente.soldTo
attribution.save()
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
elif 'reinit' in request.POST:
2017-02-16 04:52:44 +01:00
soldform = SoldForm(participant, request.POST, prefix='sold')
if soldform.is_valid():
attributions = soldform.cleaned_data['attributions']
for attribution in attributions:
if attribution.spectacle.date > timezone.now():
revente = attribution.revente
revente.date = timezone.now() - timedelta(minutes=65)
revente.soldTo = None
revente.notif_sent = False
revente.tirage_done = False
revente.shotgun = False
if revente.answered_mail:
revente.answered_mail.clear()
revente.save()
2016-07-27 13:08:00 +02:00
overdue = participant.attribution_set.filter(
spectacle__date__gte=timezone.now(),
revente__isnull=False,
revente__seller=participant,
2017-02-16 05:28:57 +01:00
revente__notif_sent=True)\
.filter(
Q(revente__soldTo__isnull=True) | Q(revente__soldTo=participant))
2016-07-27 13:08:00 +02:00
2017-02-21 23:49:29 +01:00
return render(request, "bda/reventes.html",
2017-02-16 04:52:44 +01:00
{'tirage': tirage, 'overdue': overdue, "soldform": soldform,
2016-07-27 13:08:00 +02:00
"annulform": annulform, "resellform": resellform})
2016-09-04 11:14:09 +02:00
@login_required
def revente_interested(request, revente_id):
revente = get_object_or_404(SpectacleRevente, id=revente_id)
participant, _ = Participant.objects.get_or_create(
2016-09-04 11:14:09 +02:00
user=request.user, tirage=revente.attribution.spectacle.tirage)
2016-11-13 02:45:14 +01:00
if (timezone.now() < revente.date + timedelta(hours=1)) or revente.shotgun:
return render(request, "bda-wrongtime.html",
{"revente": revente})
2016-09-04 11:14:09 +02:00
2016-09-27 15:44:27 +02:00
revente.answered_mail.add(participant)
2016-09-04 11:14:09 +02:00
return render(request, "bda-interested.html",
{"spectacle": revente.attribution.spectacle,
2016-11-14 15:52:02 +01:00
"date": revente.date_tirage})
2016-09-04 11:14:09 +02:00
2016-07-25 02:52:49 +02:00
@login_required
def list_revente(request, tirage_id):
tirage = get_object_or_404(Tirage, id=tirage_id)
participant, _ = Participant.objects.get_or_create(
user=request.user, tirage=tirage)
deja_revente = False
2016-09-27 17:35:29 +02:00
success = False
inscrit_revente = []
2016-07-27 13:08:00 +02:00
if request.method == 'POST':
form = InscriptionReventeForm(tirage, request.POST)
if form.is_valid():
choices = form.cleaned_data['spectacles']
participant.choicesrevente = choices
participant.save()
for spectacle in choices:
qset = SpectacleRevente.objects.filter(
attribution__spectacle=spectacle)
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)
.annotate(nb_subscribers=Count('answered_mail'))
.order_by('nb_subscribers')
.first()
)
if min_resell is not None:
min_resell.answered_mail.add(participant)
min_resell.save()
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(
tirage,
initial={'spectacles': participant.choicesrevente.all()}
)
# Messages
if success:
messages.success(request, "Ton inscription a bien été prise en compte")
if deja_revente:
messages.info(request, "Des reventes existent déjà pour certains de "
"ces spectacles, vérifie les places "
"disponibles sans tirage !")
if inscrit_revente:
shows = map("<li>{!s}</li>".format, inscrit_revente)
msg = (
"Tu as été inscrit à des reventes en cours pour les spectacles "
"<ul>{:s}</ul>".format('\n'.join(shows))
)
messages.info(request, msg, extra_tags="safe")
return render(request, "bda/liste-reventes.html", {"form": form})
2016-07-25 02:52:49 +02:00
@login_required
def buy_revente(request, spectacle_id):
spectacle = get_object_or_404(Spectacle, id=spectacle_id)
tirage = spectacle.tirage
participant, _ = Participant.objects.get_or_create(
user=request.user, tirage=tirage)
2016-07-25 02:52:49 +02:00
reventes = SpectacleRevente.objects.filter(
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.
2016-09-21 15:39:01 +02:00
if reventes.filter(seller=participant).exists():
revente = reventes.filter(seller=participant)[0]
revente.delete()
2016-10-13 16:02:52 +02:00
return HttpResponseRedirect(reverse("bda-shotgun",
2016-09-21 15:39:01 +02:00
args=[tirage.id]))
2016-09-03 18:47:38 +02:00
2016-11-13 01:45:52 +01:00
reventes_shotgun = list(reventes.filter(shotgun=True).all())
if not reventes_shotgun:
2016-09-03 18:47:38 +02:00
return render(request, "bda-no-revente.html", {})
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()
context = {
'show': spectacle,
'acheteur': request.user,
'vendeur': revente.seller.user
}
send_custom_mail(
'bda-buy-shotgun',
2017-02-11 16:15:17 +01:00
'bda@ens.fr',
[revente.seller.user.email],
context=context,
)
2016-09-19 16:08:12 +02:00
return render(request, "bda-success.html",
2016-09-25 14:39:18 +02:00
{"seller": revente.attribution.participant.user,
2016-09-19 16:08:12 +02:00
"spectacle": spectacle})
2016-07-25 02:52:49 +02:00
return render(request, "revente-confirm.html",
2016-09-03 18:47:38 +02:00
{"spectacle": spectacle,
"user": request.user})
2016-07-25 02:52:49 +02:00
2016-10-06 13:46:18 +02:00
@login_required
def revente_shotgun(request, tirage_id):
tirage = get_object_or_404(Tirage, id=tirage_id)
spectacles = tirage.spectacle_set.filter(
date__gte=timezone.now())
2016-10-06 13:46:18 +02:00
shotgun = []
for spectacle in spectacles:
2016-12-21 03:35:05 +01:00
reventes = SpectacleRevente.objects.filter(
attribution__spectacle=spectacle,
shotgun=True,
soldTo__isnull=True)
2016-12-21 03:35:05 +01:00
if reventes.exists():
2016-10-06 13:46:18 +02:00
shotgun.append(spectacle)
2016-10-06 18:51:19 +02:00
return render(request, "bda-shotgun.html",
2016-10-06 13:46:18 +02:00
{"shotgun": shotgun})
@buro_required
def spectacle(request, tirage_id, spectacle_id):
tirage = get_object_or_404(Tirage, id=tirage_id)
spectacle = get_object_or_404(Spectacle, id=spectacle_id, tirage=tirage)
attributions = spectacle.attribues.all()
participants = {}
for attrib in attributions:
participant = attrib.participant
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:
participants[participant.id]['nb_places'] += 1
participants[participant.id]['given'] += attrib.given
else:
participants[participant.id] = participant_info
participants_info = sorted(participants.values(),
key=lambda part: part['lastname'])
return render(request, "bda-participants.html",
{"spectacle": spectacle, "participants": participants_info})
class SpectacleListView(ListView):
model = Spectacle
template_name = 'spectacle_list.html'
def get_queryset(self):
self.tirage = get_object_or_404(Tirage, id=self.kwargs['tirage_id'])
categories = self.tirage.spectacle_set.all()
return categories
def get_context_data(self, **kwargs):
context = super(SpectacleListView, self).get_context_data(**kwargs)
context['tirage_id'] = self.tirage.id
context['tirage_name'] = self.tirage.title
return context
@buro_required
def unpaid(request, tirage_id):
tirage = get_object_or_404(Tirage, id=tirage_id)
unpaid = tirage.participant_set \
.annotate(nb_attributions=Count('attribution')) \
.filter(paid=False, nb_attributions__gt=0).all()
return render(request, "bda-unpaid.html", {"unpaid": unpaid})
@buro_required
def send_rappel(request, spectacle_id):
show = get_object_or_404(Spectacle, id=spectacle_id)
# Mails d'exemples
exemple_mail_1place = render_custom_mail('bda-rappel', {
'member': request.user,
'show': show,
'nb_attr': 1
})
exemple_mail_2places = render_custom_mail('bda-rappel', {
'member': request.user,
'show': show,
'nb_attr': 2
})
# Contexte
ctxt = {'show': show,
'exemple_mail_1place': exemple_mail_1place,
'exemple_mail_2places': exemple_mail_2places}
# Envoi confirmé
if request.method == 'POST':
members = show.send_rappel()
ctxt['sent'] = True
ctxt['members'] = members
# Demande de confirmation
else:
ctxt['sent'] = False
2016-12-23 10:25:28 +01:00
return render(request, "bda/mails-rappel.html", ctxt)
def descriptions_spectacles(request, tirage_id):
tirage = get_object_or_404(Tirage, id=tirage_id)
shows_qs = tirage.spectacle_set
category_name = request.GET.get('category', '')
location_id = request.GET.get('location', '')
if category_name:
shows_qs = shows_qs.filter(category__name=category_name)
if location_id:
try:
shows_qs = shows_qs.filter(location__id=int(location_id))
except ValueError:
return HttpResponseBadRequest(
"La variable GET 'location' doit contenir un entier")
return render(request, 'descriptions.html', {'shows': shows_qs.all()})
def catalogue(request,request_type):
"""
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":
# Dans ce cas on retourne la liste des tirages et de leur id sous forme de JSON
data_return = [{'id':tirage.id, 'title':tirage.title} for tirage in Tirage.objects.all()]
return HttpResponse(json.dumps(data_return))
if request_type == "details":
# Dans ce cas on retourne une liste des catégories et des salles du tirage
try :
tirage_id = request.GET.get('id', '')
tirage = get_object_or_404(Tirage, id=tirage_id)
except:
return HttpResponseBadRequest()
categories = list(unique_everseen([str(spectacle.category) for spectacle in tirage.spectacle_set.all()]))
categories.remove('None')
locations = list(unique_everseen([str(spectacle.location) for spectacle in tirage.spectacle_set.all()]))
data_return = [{'categories':categories, 'locations':locations}]
return HttpResponse(json.dumps(data_return))
if request_type == "descriptions":
# Ici on retourne les descriptions correspondant à la catégorie et à la salle spécifiées
locations = {}
for salle in Salle.objects.all():
locations[salle.name] = salle.id
try:
tirage_id = request.GET.get('id', '')
category_name = request.GET.get('category', '')
location_name = request.GET.get('location', '')
tirage = get_object_or_404(Tirage, id=tirage_id)
shows_qs = tirage.spectacle_set
if category_name:
shows_qs = shows_qs.filter(category__name=category_name)
if location_name:
shows_qs = shows_qs.filter(location__id=locations[location_name])
except:
return HttpResponseBadRequest("Impossible de trouver des résultats correspondant à ces caractéristiques")
def getImgUrl(obj):
""" Cette fonction permet de gérer les cas où il n'y a pas d'image """
try:
return obj.image.url
except:
return ''
2017-03-16 05:50:02 +01:00
specquotes = lambda spectacle: [{'author': str(quote.author), 'text': str(quote.text)} for quote in Quote.objects.filter(spectacle = spectacle).all()]
# 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)
2017-03-16 05:50:02 +01:00
data_return = [{'title': spectacle.title, 'category': str(spectacle.category), 'date': str(spectacle.date), 'location': str(spectacle.location), 'vips': spectacle.vips, 'description': spectacle.description, 'slots_description': spectacle.slots_description, 'quotes': specquotes(spectacle),'image': getImgUrl(spectacle), 'ext_link': spectacle.ext_link, 'price': spectacle.price, 'slots': spectacle.slots} for spectacle in shows_qs.all()]
return HttpResponse(json.dumps(data_return))
return HttpResponseBadRequest() # Si la requête n'est pas de la forme attendue, on quitte avec une erreur