mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-11 04:19:25 +00:00
remove-useless-queries-counter-stats (#519)
This commit is contained in:
@ -22,13 +22,12 @@
|
||||
#
|
||||
#
|
||||
from __future__ import annotations
|
||||
from django.db.models import Sum, F
|
||||
|
||||
from typing import Tuple
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import OuterRef, Exists
|
||||
from django.db.models.functions import Length
|
||||
from django.db.models import F, Value, Sum, QuerySet, OuterRef, Exists
|
||||
from django.db.models.functions import Concat, Length
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils import timezone
|
||||
from django.conf import settings
|
||||
@ -37,14 +36,14 @@ from django.core.validators import MinLengthValidator
|
||||
from django.forms import ValidationError
|
||||
from django.utils.functional import cached_property
|
||||
|
||||
from datetime import timedelta, date
|
||||
from datetime import timedelta, date, datetime
|
||||
import random
|
||||
import string
|
||||
import os
|
||||
import base64
|
||||
import datetime
|
||||
from dict2xml import dict2xml
|
||||
|
||||
from core.utils import get_start_of_semester
|
||||
from sith.settings import SITH_COUNTER_OFFICES, SITH_MAIN_CLUB
|
||||
from club.models import Club, Membership
|
||||
from accounting.models import CurrencyField
|
||||
@ -92,8 +91,9 @@ class Customer(models.Model):
|
||||
don't mix them) and a Product.
|
||||
"""
|
||||
subscription = self.user.subscriptions.order_by("subscription_end").last()
|
||||
time_diff = date.today() - subscription.subscription_end
|
||||
return subscription is not None and time_diff < timedelta(days=90)
|
||||
if subscription is None:
|
||||
return False
|
||||
return (date.today() - subscription.subscription_end) < timedelta(days=90)
|
||||
|
||||
@classmethod
|
||||
def get_or_create(cls, user: User) -> Tuple[Customer, bool]:
|
||||
@ -491,7 +491,7 @@ class Counter(models.Model):
|
||||
"""
|
||||
return self.is_open() and (
|
||||
(timezone.now() - self.permanencies.order_by("-activity").first().activity)
|
||||
> datetime.timedelta(minutes=settings.SITH_COUNTER_MINUTE_INACTIVE)
|
||||
> timedelta(minutes=settings.SITH_COUNTER_MINUTE_INACTIVE)
|
||||
)
|
||||
|
||||
def barman_list(self):
|
||||
@ -517,6 +517,77 @@ class Counter(models.Model):
|
||||
is_ae_member = True
|
||||
return is_ae_member
|
||||
|
||||
def get_top_barmen(self) -> QuerySet:
|
||||
"""
|
||||
Return a QuerySet querying the office hours stats of all the barmen of all time
|
||||
of this counter, ordered by descending number of hours.
|
||||
|
||||
Each element of the QuerySet corresponds to a barman and has the following data :
|
||||
- the full name (first name + last name) of the barman
|
||||
- the nickname of the barman
|
||||
- the promo of the barman
|
||||
- the total number of office hours the barman did attend
|
||||
"""
|
||||
return (
|
||||
self.permanencies.exclude(end=None)
|
||||
.annotate(
|
||||
name=Concat(F("user__first_name"), Value(" "), F("user__last_name"))
|
||||
)
|
||||
.annotate(nickname=F("user__nick_name"))
|
||||
.annotate(promo=F("user__promo"))
|
||||
.values("user", "name", "nickname", "promo")
|
||||
.annotate(perm_sum=Sum(F("end") - F("start")))
|
||||
.exclude(perm_sum=None)
|
||||
.order_by("-perm_sum")
|
||||
)
|
||||
|
||||
def get_top_customers(self, since=get_start_of_semester()) -> QuerySet:
|
||||
"""
|
||||
Return a QuerySet querying the money spent by customers of this counter
|
||||
since the specified date, ordered by descending amount of money spent.
|
||||
|
||||
Each element of the QuerySet corresponds to a customer and has the following data :
|
||||
- the full name (first name + last name) of the customer
|
||||
- the nickname of the customer
|
||||
- the amount of money spent by the customer
|
||||
"""
|
||||
return (
|
||||
self.sellings.filter(date__gte=since)
|
||||
.annotate(
|
||||
name=Concat(
|
||||
F("customer__user__first_name"),
|
||||
Value(" "),
|
||||
F("customer__user__last_name"),
|
||||
)
|
||||
)
|
||||
.annotate(nickname=F("customer__user__nick_name"))
|
||||
.annotate(promo=F("customer__user__promo"))
|
||||
.values("customer__user", "name", "nickname")
|
||||
.annotate(
|
||||
selling_sum=Sum(
|
||||
F("unit_price") * F("quantity"), output_field=CurrencyField()
|
||||
)
|
||||
)
|
||||
.filter(selling_sum__gt=0)
|
||||
.order_by("-selling_sum")
|
||||
)
|
||||
|
||||
def get_total_sales(self, since=get_start_of_semester()) -> CurrencyField:
|
||||
"""
|
||||
Compute and return the total turnover of this counter
|
||||
since the date specified in parameter (by default, since the start of the current
|
||||
semester)
|
||||
:param since: timestamp from which to perform the calculation
|
||||
:type since: datetime | date
|
||||
:return: Total revenue earned at this counter
|
||||
"""
|
||||
if isinstance(since, date):
|
||||
since = datetime.combine(since, datetime.min.time())
|
||||
total = self.sellings.filter(date__gte=since).aggregate(
|
||||
total=Sum(F("quantity") * F("unit_price"), output_field=CurrencyField())
|
||||
)["total"]
|
||||
return total if total is not None else CurrencyField(0)
|
||||
|
||||
|
||||
class Refilling(models.Model):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user