Projet

Général

Profil

0001-agendas-change-annotate_queryset-queries-54332.patch

Valentin Deniaud, 02 juin 2021 16:39

Télécharger (3,52 ko)

Voir les différences:

Subject: [PATCH 1/2] agendas: change annotate_queryset queries (#54332)

 chrono/agendas/models.py | 51 ++++++++++++++--------------------------
 1 file changed, 18 insertions(+), 33 deletions(-)
chrono/agendas/models.py
34 34
from django.core.exceptions import FieldDoesNotExist, ValidationError
35 35
from django.core.validators import MaxValueValidator, MinValueValidator
36 36
from django.db import connection, models, transaction
37
from django.db.models import Case, Count, Max, Q, When
37
from django.db.models import Count, IntegerField, Max, OuterRef, Q, Subquery, Value
38
from django.db.models.functions import Coalesce
38 39
from django.template import Context, Template, TemplateSyntaxError, VariableDoesNotExist, engines
39 40
from django.urls import reverse
40 41
from django.utils import functional
......
1283 1284

  
1284 1285
    @staticmethod
1285 1286
    def annotate_queryset(qs):
1286
        if django.VERSION < (2, 0):
1287
            return qs.annotate(
1288
                booked_places_count=Count(
1289
                    Case(
1290
                        When(
1291
                            booking__cancellation_datetime__isnull=True,
1292
                            booking__in_waiting_list=False,
1293
                            then='booking',
1294
                        )
1295
                    )
1296
                ),
1297
                waiting_list_count=Count(
1298
                    Case(
1299
                        When(
1300
                            booking__cancellation_datetime__isnull=True,
1301
                            booking__in_waiting_list=True,
1302
                            then='booking',
1303
                        )
1304
                    )
1305
                ),
1306
            )
1307
        else:
1308
            return qs.annotate(
1309
                booked_places_count=Count(
1310
                    'booking',
1311
                    filter=Q(booking__cancellation_datetime__isnull=True, booking__in_waiting_list=False),
1312
                ),
1313
                waiting_list_count=Count(
1314
                    'booking',
1315
                    filter=Q(booking__cancellation_datetime__isnull=True, booking__in_waiting_list=True),
1316
                ),
1317
            )
1287
        not_cancelled_bookings = Booking.objects.filter(
1288
            cancellation_datetime__isnull=True, event=OuterRef('pk')
1289
        )
1290

  
1291
        bookings = not_cancelled_bookings.filter(in_waiting_list=False).order_by().values('event')
1292
        count_bookings = bookings.annotate(count=Count('event')).values('count')
1293

  
1294
        waiting_list_bookings = not_cancelled_bookings.filter(in_waiting_list=True).order_by().values('event')
1295
        count_waiting_list = waiting_list_bookings.annotate(count=Count('event')).values('count')
1296

  
1297
        return qs.annotate(
1298
            booked_places_count=Coalesce(Subquery(count_bookings, output_field=IntegerField()), Value(0)),
1299
            waiting_list_count=Coalesce(Subquery(count_waiting_list, output_field=IntegerField()), Value(0)),
1300
        )
1318 1301

  
1319 1302
    @staticmethod
1320 1303
    def annotate_queryset_for_user(qs, excluded_user_external_id):
1321 1304
        if django.VERSION < (2, 0):
1305
            from django.db.models import Case, When
1306

  
1322 1307
            return qs.annotate(
1323 1308
                user_places_count=Count(
1324 1309
                    Case(
1325
-