Optimize user account pages

This commit is contained in:
imperosol
2024-10-04 13:41:15 +02:00
parent f6be360eab
commit 58d3a7ee2c
5 changed files with 208 additions and 137 deletions

View File

@ -20,7 +20,7 @@ import random
import string
from datetime import date, datetime, timedelta
from datetime import timezone as tz
from typing import Tuple
from typing import Self, Tuple
from dict2xml import dict2xml
from django.conf import settings
@ -585,6 +585,23 @@ class Counter(models.Model):
)["total"]
class RefillingQuerySet(models.QuerySet):
def annotate_total(self) -> Self:
"""Annotate the Queryset with the total amount.
The total is just the sum of the amounts for each row.
If no grouping is involved (like in most queries),
this is just the same as doing nothing and fetching the
`amount` attribute.
However, it may be useful when there is a `group by` clause
in the query, or when other models are queried and having
a common interface is helpful (e.g. `Selling.objects.annotate_total()`
and `Refilling.objects.annotate_total()` will both have the `total` field).
"""
return self.annotate(total=Sum("amount"))
class Refilling(models.Model):
"""Handle the refilling."""
@ -613,6 +630,8 @@ class Refilling(models.Model):
)
is_validated = models.BooleanField(_("is validated"), default=False)
objects = RefillingQuerySet.as_manager()
class Meta:
verbose_name = _("refilling")
@ -657,6 +676,15 @@ class Refilling(models.Model):
super().delete(*args, **kwargs)
class SellingQuerySet(models.QuerySet):
def annotate_total(self) -> Self:
"""Annotate the Queryset with the total amount of the sales.
The total is considered as the sum of (unit_price * quantity).
"""
return self.annotate(total=Sum(F("unit_price") * F("quantity")))
class Selling(models.Model):
"""Handle the sellings."""
@ -703,6 +731,8 @@ class Selling(models.Model):
)
is_validated = models.BooleanField(_("is validated"), default=False)
objects = SellingQuerySet.as_manager()
class Meta:
verbose_name = _("selling")