Fewer requests with shotgunable reventes

bda.views.revente_shotgun:
- 2 requests instead of ~#spectacles requests
This commit is contained in:
Aurélien Delobelle 2017-04-08 12:53:37 +02:00
parent fdc1128bd5
commit 93a3a9af2c

View file

@ -15,7 +15,7 @@ from django.contrib.auth.decorators import login_required
from django.contrib import messages from django.contrib import messages
from django.db import transaction from django.db import transaction
from django.core import serializers from django.core import serializers
from django.db.models import Count, Q from django.db.models import Count, Q, Prefetch
from django.forms.models import inlineformset_factory from django.forms.models import inlineformset_factory
from django.http import ( from django.http import (
HttpResponseBadRequest, HttpResponseRedirect, JsonResponse HttpResponseBadRequest, HttpResponseRedirect, JsonResponse
@ -560,13 +560,13 @@ def buy_revente(request, spectacle_id):
# Si l'utilisateur veut racheter une place qu'il est en train de revendre, # Si l'utilisateur veut racheter une place qu'il est en train de revendre,
# on supprime la revente en question. # on supprime la revente en question.
if reventes.filter(seller=participant).exists(): own_reventes = reventes.filter(seller=participant)
revente = reventes.filter(seller=participant)[0] if len(own_reventes) > 0:
revente.delete() own_reventes[0].delete()
return HttpResponseRedirect(reverse("bda-shotgun", return HttpResponseRedirect(reverse("bda-shotgun",
args=[tirage.id])) args=[tirage.id]))
reventes_shotgun = list(reventes.filter(shotgun=True).all()) reventes_shotgun = reventes.filter(shotgun=True)
if not reventes_shotgun: if not reventes_shotgun:
return render(request, "bda-no-revente.html", {}) return render(request, "bda-no-revente.html", {})
@ -598,16 +598,21 @@ def buy_revente(request, spectacle_id):
@login_required @login_required
def revente_shotgun(request, tirage_id): def revente_shotgun(request, tirage_id):
tirage = get_object_or_404(Tirage, id=tirage_id) tirage = get_object_or_404(Tirage, id=tirage_id)
spectacles = tirage.spectacle_set.filter( spectacles = (
date__gte=timezone.now()) tirage.spectacle_set
shotgun = [] .filter(date__gte=timezone.now())
for spectacle in spectacles: .select_related('location')
reventes = SpectacleRevente.objects.filter( .prefetch_related(Prefetch(
attribution__spectacle=spectacle, 'attribues',
shotgun=True, queryset=(
soldTo__isnull=True) Attribution.objects
if reventes.exists(): .filter(revente__shotgun=True,
shotgun.append(spectacle) revente__soldTo__isnull=True)
),
to_attr='shotguns',
))
)
shotgun = [sp for sp in spectacles if len(sp.shotguns) > 0]
return render(request, "bda-shotgun.html", return render(request, "bda-shotgun.html",
{"shotgun": shotgun}) {"shotgun": shotgun})