Possibilité de filtrer sur plusieurs salles/catégories

This commit is contained in:
Hugo Roussille 2017-03-31 04:33:13 +02:00
parent 73c21d83ee
commit 8cf14d3f6b

View file

@ -3,6 +3,7 @@
import random import random
import hashlib import hashlib
import time import time
import json
from datetime import timedelta from datetime import timedelta
from custommail.shortcuts import ( from custommail.shortcuts import (
send_mass_custom_mail, send_custom_mail, render_custom_mail send_mass_custom_mail, send_custom_mail, render_custom_mail
@ -663,31 +664,30 @@ def catalogue(request, request_type):
categories = list( categories = list(
CategorieSpectacle.objects.filter( CategorieSpectacle.objects.filter(
spectacle__in=tirage.spectacle_set.all()) spectacle__in=tirage.spectacle_set.all())
.distinct()) .distinct().values('id', 'name'))
locations = list( locations = list(
Salle.objects.filter( Salle.objects.filter(
spectacle__in=tirage.spectacle_set.all()) spectacle__in=tirage.spectacle_set.all())
.distinct().values_list('name', flat=True)) .distinct().values('id', 'name'))
data_return = [{'categories': categories, 'locations': locations}] data_return = {'categories': categories, 'locations': locations}
return JsonResponse(data_return, safe=False) return JsonResponse(data_return, safe=False)
if request_type == "descriptions": if request_type == "descriptions":
# Ici on retourne les descriptions correspondant à la catégorie et # Ici on retourne les descriptions correspondant à la catégorie et
# à la salle spécifiées # à la salle spécifiées
locations = {}
for salle in Salle.objects.all():
locations[salle.name] = salle.id
try: try:
tirage_id = request.GET.get('id', '') tirage_id = request.GET.get('id', '')
category_name = request.GET.get('category', '') category_id = json.loads(request.GET.get('category', '[0]'))
location_name = request.GET.get('location', '') location_id = json.loads(request.GET.get('location', '[0]'))
tirage = get_object_or_404(Tirage, id=tirage_id) tirage = get_object_or_404(Tirage, id=tirage_id)
shows_qs = tirage.spectacle_set shows_qs = tirage.spectacle_set
if category_name: if not(0 in category_id):
shows_qs = shows_qs.filter(category__name=category_name)
if location_name:
shows_qs = shows_qs.filter( shows_qs = shows_qs.filter(
location__id=locations[location_name]) category__id__in=category_id)
if not(0 in location_id):
shows_qs = shows_qs.filter(
location__id__in=location_id)
except ValueError: except ValueError:
return HttpResponseBadRequest( return HttpResponseBadRequest(
"Impossible de trouver des résultats correspondant \ "Impossible de trouver des résultats correspondant \