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
|
... | ... | |
1296 |
1297 |
|
1297 |
1298 |
@staticmethod
|
1298 |
1299 |
def annotate_queryset(qs):
|
1299 |
|
if django.VERSION < (2, 0):
|
1300 |
|
return qs.annotate(
|
1301 |
|
booked_places_count=Count(
|
1302 |
|
Case(
|
1303 |
|
When(
|
1304 |
|
booking__cancellation_datetime__isnull=True,
|
1305 |
|
booking__in_waiting_list=False,
|
1306 |
|
then='booking',
|
1307 |
|
)
|
1308 |
|
)
|
1309 |
|
),
|
1310 |
|
waiting_list_count=Count(
|
1311 |
|
Case(
|
1312 |
|
When(
|
1313 |
|
booking__cancellation_datetime__isnull=True,
|
1314 |
|
booking__in_waiting_list=True,
|
1315 |
|
then='booking',
|
1316 |
|
)
|
1317 |
|
)
|
1318 |
|
),
|
1319 |
|
)
|
1320 |
|
else:
|
1321 |
|
return qs.annotate(
|
1322 |
|
booked_places_count=Count(
|
1323 |
|
'booking',
|
1324 |
|
filter=Q(booking__cancellation_datetime__isnull=True, booking__in_waiting_list=False),
|
1325 |
|
),
|
1326 |
|
waiting_list_count=Count(
|
1327 |
|
'booking',
|
1328 |
|
filter=Q(booking__cancellation_datetime__isnull=True, booking__in_waiting_list=True),
|
1329 |
|
),
|
1330 |
|
)
|
|
1300 |
not_cancelled_bookings = Booking.objects.filter(
|
|
1301 |
cancellation_datetime__isnull=True, event=OuterRef('pk')
|
|
1302 |
)
|
|
1303 |
|
|
1304 |
bookings = not_cancelled_bookings.filter(in_waiting_list=False).order_by().values('event')
|
|
1305 |
count_bookings = bookings.annotate(count=Count('event')).values('count')
|
|
1306 |
|
|
1307 |
waiting_list_bookings = not_cancelled_bookings.filter(in_waiting_list=True).order_by().values('event')
|
|
1308 |
count_waiting_list = waiting_list_bookings.annotate(count=Count('event')).values('count')
|
|
1309 |
|
|
1310 |
return qs.annotate(
|
|
1311 |
booked_places_count=Coalesce(Subquery(count_bookings, output_field=IntegerField()), Value(0)),
|
|
1312 |
waiting_list_count=Coalesce(Subquery(count_waiting_list, output_field=IntegerField()), Value(0)),
|
|
1313 |
)
|
1331 |
1314 |
|
1332 |
1315 |
@staticmethod
|
1333 |
1316 |
def annotate_queryset_for_user(qs, excluded_user_external_id):
|
1334 |
1317 |
if django.VERSION < (2, 0):
|
|
1318 |
from django.db.models import Case, When
|
|
1319 |
|
1335 |
1320 |
return qs.annotate(
|
1336 |
1321 |
user_places_count=Count(
|
1337 |
1322 |
Case(
|
1338 |
|
-
|