DRYing the method up a bit.

This commit is contained in:
Víðir Valberg Guðmundsson 2021-07-29 20:33:43 +02:00
parent 4ed6392554
commit 34dcacea0c

View file

@ -3,11 +3,20 @@ import hashlib
import io import io
import logging import logging
from decimal import Decimal from decimal import Decimal
from typing import Union
import qrcode import qrcode
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.db.models import Count, ExpressionWrapper, F, OuterRef, Subquery, Sum from django.db.models import (
Count,
Expression,
ExpressionWrapper,
F,
OuterRef,
Subquery,
Sum,
)
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -19,40 +28,16 @@ logger = logging.getLogger("bornhack.%s" % __name__)
class TicketTypeQuerySet(models.QuerySet): class TicketTypeQuerySet(models.QuerySet):
def with_price_stats(self): def with_price_stats(self):
shopticket_count = Subquery( def _make_subquery(annotation: Union[Expression, F]) -> Subquery:
TicketType.objects.annotate(shopticket_count=Count("shopticket")) return Subquery(
.filter(pk=OuterRef("pk")) TicketType.objects.annotate(annotation_value=annotation)
.values("shopticket_count") .filter(pk=OuterRef("pk"))
) .values("annotation_value")[:1]
)
quantity = F("product__orderproductrelation__quantity") quantity = F("product__orderproductrelation__quantity")
total_units_sold = Subquery(
TicketType.objects.annotate(total_units_sold=quantity)
.filter(pk=OuterRef("pk"))
.values("total_units_sold")[:1]
)
cost = quantity * F("product__cost") cost = quantity * F("product__cost")
total_cost = Subquery(
TicketType.objects.annotate(total_cost=Sum(cost))
.filter(pk=OuterRef("pk"))
.values("total_cost")[:1]
)
income = quantity * F("product__price") income = quantity * F("product__price")
total_income = Subquery(
TicketType.objects.annotate(total_income=Sum(income))
.filter(pk=OuterRef("pk"))
.values("total_income")[:1]
)
profit = income - cost
total_profit = Subquery(
TicketType.objects.annotate(total_profit=Sum(profit))
.filter(pk=OuterRef("pk"))
.values("total_profit")[:1]
)
avg_ticket_price = Subquery( avg_ticket_price = Subquery(
TicketType.objects.annotate(units=Sum(quantity)) TicketType.objects.annotate(units=Sum(quantity))
@ -69,11 +54,11 @@ class TicketTypeQuerySet(models.QuerySet):
) )
return self.annotate( return self.annotate(
shopticket_count=shopticket_count, shopticket_count=_make_subquery(Count("shopticket")),
total_units_sold=total_units_sold, total_units_sold=_make_subquery(quantity),
total_income=total_income, total_income=_make_subquery(cost),
total_cost=total_cost, total_cost=_make_subquery(cost),
total_profit=total_profit, total_profit=_make_subquery(income - cost),
avg_ticket_price=avg_ticket_price, avg_ticket_price=avg_ticket_price,
).distinct() ).distinct()