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.db import transaction
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.http import (
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,
# on supprime la revente en question.
if reventes.filter(seller=participant).exists():
revente = reventes.filter(seller=participant)[0]
revente.delete()
own_reventes = reventes.filter(seller=participant)
if len(own_reventes) > 0:
own_reventes[0].delete()
return HttpResponseRedirect(reverse("bda-shotgun",
args=[tirage.id]))
reventes_shotgun = list(reventes.filter(shotgun=True).all())
reventes_shotgun = reventes.filter(shotgun=True)
if not reventes_shotgun:
return render(request, "bda-no-revente.html", {})
@ -598,16 +598,21 @@ def buy_revente(request, spectacle_id):
@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())
shotgun = []
for spectacle in spectacles:
reventes = SpectacleRevente.objects.filter(
attribution__spectacle=spectacle,
shotgun=True,
soldTo__isnull=True)
if reventes.exists():
shotgun.append(spectacle)
spectacles = (
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',
))
)
shotgun = [sp for sp in spectacles if len(sp.shotguns) > 0]
return render(request, "bda-shotgun.html",
{"shotgun": shotgun})