Skip to content

Commit

Permalink
Improve age management for getting products and make get_product a pa…
Browse files Browse the repository at this point in the history
…rt of counter model
  • Loading branch information
klmp200 committed Dec 22, 2024
1 parent 372470b commit eed434a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
8 changes: 0 additions & 8 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,6 @@ def get_display_name(self) -> str:
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,
Expand Down
2 changes: 1 addition & 1 deletion core/templates/core/macros.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
{% endif %}
{% if user.date_of_birth %}
<div class="user_mini_profile_dob">
{{ user.date_of_birth|date("d/m/Y") }} ({{ user.get_age() }})
{{ user.date_of_birth|date("d/m/Y") }} ({{ user.age }})
</div>
{% endif %}
</div>
Expand Down
28 changes: 28 additions & 0 deletions counter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,34 @@ def customer_is_barman(self, customer: Customer | User) -> bool:
# 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:
Expand Down
39 changes: 4 additions & 35 deletions counter/views/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.db.models import F
from django.forms import (
BaseFormSet,
Form,
Expand Down Expand Up @@ -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

Expand All @@ -204,6 +171,8 @@ def dispatch(self, request, *args, **kwargs):
):
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):
Expand Down Expand Up @@ -260,7 +229,7 @@ def get_success_url(self):
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:
Expand Down

0 comments on commit eed434a

Please sign in to comment.