mirror of
https://github.com/ae-utbm/sith.git
synced 2025-08-25 02:05:44 +00:00
Get customer last purchases in one request
This commit is contained in:
@@ -34,6 +34,7 @@ from django.contrib.auth.mixins import (
|
|||||||
from django.contrib.messages.views import SuccessMessageMixin
|
from django.contrib.messages.views import SuccessMessageMixin
|
||||||
from django.core.exceptions import SuspiciousOperation, ValidationError
|
from django.core.exceptions import SuspiciousOperation, ValidationError
|
||||||
from django.db import DatabaseError, transaction
|
from django.db import DatabaseError, transaction
|
||||||
|
from django.db.models import Subquery
|
||||||
from django.db.models.fields import forms
|
from django.db.models.fields import forms
|
||||||
from django.db.utils import cached_property
|
from django.db.utils import cached_property
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
@@ -127,39 +128,38 @@ class EbouticMainView(LoginRequiredMixin, FormView):
|
|||||||
def customer(self) -> Customer:
|
def customer(self) -> Customer:
|
||||||
return Customer.get_or_create(self.request.user)[0]
|
return Customer.get_or_create(self.request.user)[0]
|
||||||
|
|
||||||
def get_purchase_timestamp(
|
|
||||||
self, purchase: Selling | Refilling | None
|
|
||||||
) -> int | None:
|
|
||||||
if purchase is None:
|
|
||||||
return None
|
|
||||||
return int(purchase.date.timestamp() * 1000)
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context["products"] = self.products
|
context["products"] = self.products
|
||||||
context["customer_amount"] = self.request.user.account_balance
|
context["customer_amount"] = self.request.user.account_balance
|
||||||
|
|
||||||
last_buying: Selling | None = (
|
|
||||||
self.customer.buyings.filter(counter__type="EBOUTIC")
|
|
||||||
.order_by("-date")
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
last_refilling: Refilling | None = (
|
|
||||||
self.customer.refillings.filter(counter__type="EBOUTIC")
|
|
||||||
.order_by("-date")
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
purchase_times = [
|
purchase_times = [
|
||||||
timestamp
|
int(purchase.timestamp() * 1000)
|
||||||
for timestamp in [
|
for purchase in (
|
||||||
self.get_purchase_timestamp(last_buying),
|
Customer.objects.filter(pk=self.customer.pk)
|
||||||
self.get_purchase_timestamp(last_refilling),
|
.annotate(
|
||||||
]
|
last_refill=Subquery(
|
||||||
if timestamp is not None
|
Refilling.objects.filter(
|
||||||
|
counter__type="EBOUTIC", customer_id=self.customer.pk
|
||||||
|
)
|
||||||
|
.order_by("-date")
|
||||||
|
.values("date")[:1]
|
||||||
|
),
|
||||||
|
last_purchase=Subquery(
|
||||||
|
Selling.objects.filter(
|
||||||
|
counter__type="EBOUTIC", customer_id=self.customer.pk
|
||||||
|
)
|
||||||
|
.order_by("-date")
|
||||||
|
.values("date")[:1]
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.values("last_refill", "last_purchase")
|
||||||
|
)[0].values()
|
||||||
|
if purchase is not None
|
||||||
]
|
]
|
||||||
|
|
||||||
context["last_purchase_time"] = (
|
context["last_purchase_time"] = (
|
||||||
max(*purchase_times) if len(purchase_times) > 0 else "null"
|
max(purchase_times) if len(purchase_times) > 0 else "null"
|
||||||
)
|
)
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user