From eed434aeb2381b4541bdbe2eae00ebb9333bd661 Mon Sep 17 00:00:00 2001 From: Sli Date: Sun, 22 Dec 2024 12:27:58 +0100 Subject: [PATCH] Improve age management for getting products and make get_product a part of counter model --- core/models.py | 8 ------- core/templates/core/macros.jinja | 2 +- counter/models.py | 28 +++++++++++++++++++++++ counter/views/click.py | 39 ++++---------------------------- 4 files changed, 33 insertions(+), 44 deletions(-) diff --git a/core/models.py b/core/models.py index 347e9bd4..c8375727 100644 --- a/core/models.py +++ b/core/models.py @@ -578,14 +578,6 @@ class User(AbstractUser): return "%s (%s)" % (self.get_full_name(), self.nick_name) return self.get_full_name() - def get_age(self): - """Returns the age.""" - today = timezone.now() - born = self.date_of_birth - return ( - today.year - born.year - ((today.month, today.day) < (born.month, born.day)) - ) - def get_family( self, godfathers_depth: NonNegativeInt = 4, diff --git a/core/templates/core/macros.jinja b/core/templates/core/macros.jinja index e624e87a..84c5b05a 100644 --- a/core/templates/core/macros.jinja +++ b/core/templates/core/macros.jinja @@ -60,7 +60,7 @@ {% endif %} {% if user.date_of_birth %}
- {{ user.date_of_birth|date("d/m/Y") }} ({{ user.get_age() }}) + {{ user.date_of_birth|date("d/m/Y") }} ({{ user.age }})
{% endif %} diff --git a/counter/models.py b/counter/models.py index d5f9c9c7..0e8753b0 100644 --- a/counter/models.py +++ b/counter/models.py @@ -659,6 +659,34 @@ class Counter(models.Model): # but they share the same primary key return self.type == "BAR" and any(b.pk == customer.pk for b in self.barmen_list) + def get_products_for(self, customer: Customer) -> list[Product]: + """ + Get all allowed products for the provided customer on this counter + Prices will be annotated + """ + + products = self.products.select_related("product_type").prefetch_related( + "buying_groups" + ) + + # Only include age appropriate products + age = customer.user.age + if customer.user.is_banned_alcohol: + age = min(age, 17) + products = products.filter(limit_age__lte=age) + + # Compute special price for customer if he is a barmen on that bar + if self.customer_is_barman(customer): + products = products.annotate(price=F("special_selling_price")) + else: + products = products.annotate(price=F("selling_price")) + + return [ + product + for product in products.all() + if product.can_be_sold_to(customer.user) + ] + class RefillingQuerySet(models.QuerySet): def annotate_total(self) -> Self: diff --git a/counter/views/click.py b/counter/views/click.py index 7036f06f..5795cd3b 100644 --- a/counter/views/click.py +++ b/counter/views/click.py @@ -16,7 +16,6 @@ import math from django.core.exceptions import PermissionDenied from django.db import transaction -from django.db.models import F from django.forms import ( BaseFormSet, Form, @@ -146,44 +145,12 @@ class CounterClick(CounterTabsMixin, CanViewMixin, SingleObjectMixin, FormView): pk_url_kwarg = "counter_id" current_tab = "counter" - def get_products(self) -> list[Product]: - """Get all allowed products for the current customer on the current counter""" - - if hasattr(self, "_products"): - return self._products - - products = self.object.products.select_related("product_type").prefetch_related( - "buying_groups" - ) - - # Only include allowed products - if not self.customer.user.date_of_birth or self.customer.user.is_banned_alcohol: - products = products.filter(limit_age__lt=18) - else: - products = products.filter(limit_age__lte=self.customer.user.get_age()) - - # Compute special price for customer if he is a barmen on that bar - if self.object.customer_is_barman(self.customer): - products = products.annotate(price=F("special_selling_price")) - else: - products = products.annotate(price=F("selling_price")) - - self._products = [ - product - for product in products.all() - if product.can_be_sold_to(self.customer.user) - ] - - return self._products - def get_form_kwargs(self): kwargs = super().get_form_kwargs() kwargs["form_kwargs"] = { "customer": self.customer, "counter": self.object, - "allowed_products": { - product.id: product for product in self.get_products() - }, + "allowed_products": {product.id: product for product in self.products}, } return kwargs @@ -204,6 +171,8 @@ class CounterClick(CounterTabsMixin, CanViewMixin, SingleObjectMixin, FormView): ): return redirect(obj) # Redirect to counter + self.products = obj.get_products_for(self.customer) + return super().dispatch(request, *args, **kwargs) def form_valid(self, formset): @@ -260,7 +229,7 @@ class CounterClick(CounterTabsMixin, CanViewMixin, SingleObjectMixin, FormView): def get_context_data(self, **kwargs): """Add customer to the context.""" kwargs = super().get_context_data(**kwargs) - kwargs["products"] = self.get_products() + kwargs["products"] = self.products kwargs["categories"] = {} for product in kwargs["products"]: if product.product_type: