API: Use algorithm to count number of checked in orders consistently with backend

This commit is contained in:
Raphael Michel
2020-10-19 12:58:13 +02:00
parent fcd0c65567
commit 4ef95346a7

View File

@@ -1,6 +1,6 @@
import django_filters import django_filters
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db.models import Count, F, Max, OuterRef, Prefetch, Q, Subquery from django.db.models import Count, F, Max, OuterRef, Prefetch, Q, Subquery, Exists
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
from django.http import Http404 from django.http import Http404
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@@ -90,20 +90,12 @@ class CheckinListViewSet(viewsets.ModelViewSet):
def status(self, *args, **kwargs): def status(self, *args, **kwargs):
with language(self.request.event.settings.locale): with language(self.request.event.settings.locale):
clist = self.get_object() clist = self.get_object()
cqs = Checkin.objects.filter( cqs = clist.positions.annotate(
position__order__event=clist.event, checkedin=Exists(Checkin.objects.filter(list_id=clist.pk, position=OuterRef('pk'), type=Checkin.TYPE_ENTRY))
position__order__status__in=[Order.STATUS_PAID] + ([Order.STATUS_PENDING] if clist.include_pending else []), ).filter(
list=clist checkedin=True,
) )
pqs = OrderPosition.objects.filter( pqs = clist.positions
order__event=clist.event,
order__status__in=[Order.STATUS_PAID] + ([Order.STATUS_PENDING] if clist.include_pending else []),
)
if clist.subevent:
pqs = pqs.filter(subevent=clist.subevent)
if not clist.all_products:
pqs = pqs.filter(item__in=clist.limit_products.values_list('id', flat=True))
cqs = cqs.filter(position__item__in=clist.limit_products.values_list('id', flat=True))
ev = clist.subevent or clist.event ev = clist.subevent or clist.event
response = { response = {
@@ -124,12 +116,12 @@ class CheckinListViewSet(viewsets.ModelViewSet):
for p in pqs.order_by().values('variation').annotate(cnt=Count('id')) for p in pqs.order_by().values('variation').annotate(cnt=Count('id'))
} }
c_by_item = { c_by_item = {
p['position__item']: p['cnt'] p['item']: p['cnt']
for p in cqs.order_by().values('position__item').annotate(cnt=Count('id')) for p in cqs.order_by().values('item').annotate(cnt=Count('id'))
} }
c_by_variation = { c_by_variation = {
p['position__variation']: p['cnt'] p['variation']: p['cnt']
for p in cqs.order_by().values('position__variation').annotate(cnt=Count('id')) for p in cqs.order_by().values('variation').annotate(cnt=Count('id'))
} }
if not clist.all_products: if not clist.all_products: