mirror of
https://github.com/ae-utbm/sith.git
synced 2024-12-22 15:51:19 +00:00
Improve age management for getting products and make get_product a part of counter model
This commit is contained in:
parent
372470b44b
commit
eed434aeb2
@ -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,
|
||||
|
@ -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>
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user