2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# Copyright 2023 © AE UTBM
|
|
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
|
|
# https://ae.utbm.fr.
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2024-09-22 23:37:25 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
2024-09-23 08:25:27 +00:00
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
|
2023-04-04 16:39:45 +00:00
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
#
|
2024-12-20 16:32:37 +00:00
|
|
|
import math
|
2017-04-24 15:51:12 +00:00
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2024-12-20 16:32:37 +00:00
|
|
|
from django.db import transaction
|
|
|
|
from django.forms import (
|
|
|
|
BaseFormSet,
|
2024-12-20 17:34:27 +00:00
|
|
|
Form,
|
2024-12-20 16:32:37 +00:00
|
|
|
IntegerField,
|
|
|
|
ValidationError,
|
|
|
|
formset_factory,
|
|
|
|
)
|
|
|
|
from django.http import Http404
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, resolve_url
|
2024-11-27 17:41:12 +00:00
|
|
|
from django.urls import reverse_lazy
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2024-12-20 16:32:37 +00:00
|
|
|
from django.views.generic import FormView
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
from core.models import User
|
2024-12-15 20:33:19 +00:00
|
|
|
from core.utils import FormFragmentTemplateData
|
2024-11-27 17:41:12 +00:00
|
|
|
from core.views import CanViewMixin
|
2024-11-14 15:17:10 +00:00
|
|
|
from counter.forms import RefillForm
|
|
|
|
from counter.models import Counter, Customer, Product, Selling
|
2024-12-15 20:33:19 +00:00
|
|
|
from counter.utils import is_logged_in_counter
|
2024-11-27 18:02:48 +00:00
|
|
|
from counter.views.mixins import CounterTabsMixin
|
2024-12-07 23:32:28 +00:00
|
|
|
from counter.views.student_card import StudentCardFormView
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2024-10-06 11:22:09 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
def get_operator(counter: Counter, customer: Customer) -> User:
|
|
|
|
if counter.customer_is_barman(customer):
|
|
|
|
return customer.user
|
|
|
|
return counter.get_random_barman()
|
2017-06-12 07:47:24 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
|
2024-12-20 17:34:27 +00:00
|
|
|
class ProductForm(Form):
|
2024-12-20 16:32:37 +00:00
|
|
|
quantity = IntegerField(min_value=1)
|
2024-12-20 17:34:27 +00:00
|
|
|
id = IntegerField(min_value=0)
|
2024-12-20 16:32:37 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2024-12-22 00:56:57 +00:00
|
|
|
customer: Customer,
|
|
|
|
counter: Counter,
|
2024-12-22 01:14:14 +00:00
|
|
|
allowed_products: dict[int, Product],
|
2024-12-20 16:32:37 +00:00
|
|
|
*args,
|
|
|
|
**kwargs,
|
|
|
|
):
|
2024-12-22 00:56:57 +00:00
|
|
|
self.customer = customer # Used by formset
|
|
|
|
self.counter = counter # Used by formset
|
|
|
|
self.allowed_products = allowed_products
|
2024-12-20 16:32:37 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2024-12-22 01:14:14 +00:00
|
|
|
def clean_id(self):
|
|
|
|
data = self.cleaned_data["id"]
|
2024-12-20 18:10:46 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
# We store self.product so we can use it later on the formset validation
|
2024-12-22 01:14:14 +00:00
|
|
|
# And also in the global clean
|
|
|
|
self.product = self.allowed_products.get(data, None)
|
2024-12-20 16:32:37 +00:00
|
|
|
if self.product is None:
|
|
|
|
raise ValidationError(
|
2024-12-22 00:56:57 +00:00
|
|
|
_("The selected product isn't available for this user")
|
2024-12-20 16:32:37 +00:00
|
|
|
)
|
|
|
|
|
2024-12-22 01:14:14 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
if len(self.errors) > 0:
|
|
|
|
return
|
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
# Compute prices
|
|
|
|
cleaned_data["bonus_quantity"] = 0
|
|
|
|
if self.product.tray:
|
|
|
|
cleaned_data["bonus_quantity"] = math.floor(
|
|
|
|
cleaned_data["quantity"] / Product.QUANTITY_FOR_TRAY_PRICE
|
|
|
|
)
|
2024-12-22 00:56:57 +00:00
|
|
|
cleaned_data["total_price"] = self.product.price * (
|
2024-12-20 16:32:37 +00:00
|
|
|
cleaned_data["quantity"] - cleaned_data["bonus_quantity"]
|
|
|
|
)
|
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
|
|
|
|
class BaseBasketForm(BaseFormSet):
|
|
|
|
def clean(self):
|
|
|
|
super().clean()
|
|
|
|
if len(self) == 0:
|
|
|
|
return
|
2024-12-20 18:10:46 +00:00
|
|
|
|
2024-12-20 18:41:38 +00:00
|
|
|
self._check_forms_have_errors()
|
2024-12-20 16:32:37 +00:00
|
|
|
self._check_recorded_products(self[0].customer)
|
|
|
|
self._check_enough_money(self[0].counter, self[0].customer)
|
|
|
|
|
2024-12-20 18:41:38 +00:00
|
|
|
def _check_forms_have_errors(self):
|
2024-12-22 11:00:42 +00:00
|
|
|
if any(len(form.errors) > 0 for form in self):
|
|
|
|
raise ValidationError(_("Submmited basket is invalid"))
|
2024-12-20 18:41:38 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
def _check_enough_money(self, counter: Counter, customer: Customer):
|
|
|
|
self.total_price = sum([data["total_price"] for data in self.cleaned_data])
|
|
|
|
if self.total_price > customer.amount:
|
|
|
|
raise ValidationError(_("Not enough money"))
|
|
|
|
|
|
|
|
def _check_recorded_products(self, customer: Customer):
|
|
|
|
"""Check for, among other things, ecocups and pitchers"""
|
|
|
|
self.total_recordings = 0
|
|
|
|
for form in self:
|
|
|
|
# form.product is stored by the clean step of each formset form
|
|
|
|
if form.product.is_record_product:
|
|
|
|
self.total_recordings -= form.cleaned_data["quantity"]
|
|
|
|
if form.product.is_unrecord_product:
|
|
|
|
self.total_recordings += form.cleaned_data["quantity"]
|
|
|
|
|
|
|
|
if not customer.can_record_more(self.total_recordings):
|
|
|
|
raise ValidationError(_("This user have reached his recording limit"))
|
|
|
|
|
|
|
|
|
|
|
|
BasketForm = formset_factory(
|
|
|
|
ProductForm, formset=BaseBasketForm, absolute_max=None, min_num=1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CounterClick(CounterTabsMixin, CanViewMixin, SingleObjectMixin, FormView):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""The click view
|
2016-04-18 23:54:51 +00:00
|
|
|
This is a detail view not to have to worry about loading the counter
|
2024-07-12 07:34:16 +00:00
|
|
|
Everything is made by hand in the post method.
|
2016-04-15 09:50:31 +00:00
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2016-04-18 15:34:21 +00:00
|
|
|
model = Counter
|
2024-10-09 22:06:22 +00:00
|
|
|
queryset = Counter.objects.annotate_is_open()
|
2024-12-20 16:32:37 +00:00
|
|
|
form_class = BasketForm
|
2018-10-04 19:29:19 +00:00
|
|
|
template_name = "counter/counter_click.jinja"
|
2016-04-15 09:50:31 +00:00
|
|
|
pk_url_kwarg = "counter_id"
|
2016-09-28 09:07:32 +00:00
|
|
|
current_tab = "counter"
|
2016-04-18 15:34:21 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
kwargs["form_kwargs"] = {
|
|
|
|
"customer": self.customer,
|
|
|
|
"counter": self.object,
|
2024-12-22 11:27:58 +00:00
|
|
|
"allowed_products": {product.id: product for product in self.products},
|
2024-12-20 16:32:37 +00:00
|
|
|
}
|
|
|
|
return kwargs
|
2021-09-25 18:48:07 +00:00
|
|
|
|
2017-03-27 21:24:25 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2018-10-04 19:29:19 +00:00
|
|
|
self.customer = get_object_or_404(Customer, user__id=self.kwargs["user_id"])
|
2024-07-21 08:44:43 +00:00
|
|
|
obj: Counter = self.get_object()
|
2024-12-22 00:56:57 +00:00
|
|
|
|
|
|
|
if not self.customer.can_buy or self.customer.user.is_banned_counter:
|
|
|
|
return redirect(obj) # Redirect to counter
|
|
|
|
|
2024-07-21 08:44:43 +00:00
|
|
|
if obj.type != "BAR" and not request.user.is_authenticated:
|
|
|
|
raise PermissionDenied
|
2024-12-22 00:56:57 +00:00
|
|
|
|
2024-08-01 13:47:41 +00:00
|
|
|
if obj.type == "BAR" and (
|
2024-07-21 08:44:43 +00:00
|
|
|
"counter_token" not in request.session
|
|
|
|
or request.session["counter_token"] != obj.token
|
|
|
|
or len(obj.barmen_list) == 0
|
|
|
|
):
|
2024-12-20 16:32:37 +00:00
|
|
|
return redirect(obj) # Redirect to counter
|
2024-12-22 00:56:57 +00:00
|
|
|
|
2024-12-22 11:27:58 +00:00
|
|
|
self.products = obj.get_products_for(self.customer)
|
|
|
|
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2017-03-27 21:24:25 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
def form_valid(self, formset):
|
|
|
|
ret = super().form_valid(formset)
|
2016-08-20 00:55:48 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
if len(formset) == 0:
|
|
|
|
return ret
|
2017-07-21 19:39:49 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
operator = get_operator(self.object, self.customer)
|
2016-07-21 23:19:04 +00:00
|
|
|
with transaction.atomic():
|
2024-12-20 16:32:37 +00:00
|
|
|
self.request.session["last_basket"] = []
|
|
|
|
|
|
|
|
for form in formset:
|
|
|
|
self.request.session["last_basket"].append(
|
|
|
|
f"{form.cleaned_data['quantity']} x {form.product.name}"
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2024-12-20 16:32:37 +00:00
|
|
|
|
|
|
|
Selling(
|
|
|
|
label=form.product.name,
|
|
|
|
product=form.product,
|
|
|
|
club=form.product.club,
|
2018-10-04 19:29:19 +00:00
|
|
|
counter=self.object,
|
2024-12-22 00:56:57 +00:00
|
|
|
unit_price=form.product.price,
|
2024-12-20 16:32:37 +00:00
|
|
|
quantity=form.cleaned_data["quantity"]
|
|
|
|
- form.cleaned_data["bonus_quantity"],
|
|
|
|
seller=operator,
|
2018-10-04 19:29:19 +00:00
|
|
|
customer=self.customer,
|
2024-12-20 16:32:37 +00:00
|
|
|
).save()
|
|
|
|
if form.cleaned_data["bonus_quantity"] > 0:
|
|
|
|
Selling(
|
|
|
|
label=f"{form.product.name} (Plateau)",
|
|
|
|
product=form.product,
|
|
|
|
club=form.product.club,
|
2018-10-04 19:29:19 +00:00
|
|
|
counter=self.object,
|
|
|
|
unit_price=0,
|
2024-12-20 16:32:37 +00:00
|
|
|
quantity=form.cleaned_data["bonus_quantity"],
|
|
|
|
seller=operator,
|
2018-10-04 19:29:19 +00:00
|
|
|
customer=self.customer,
|
2024-12-20 16:32:37 +00:00
|
|
|
).save()
|
2016-04-18 23:54:51 +00:00
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
self.customer.recorded_products -= formset.total_recordings
|
|
|
|
self.customer.save()
|
|
|
|
|
|
|
|
# Add some info for the main counter view to display
|
|
|
|
self.request.session["last_customer"] = self.customer.user.get_display_name()
|
|
|
|
self.request.session["last_total"] = f"{formset.total_price:0.2f}"
|
|
|
|
self.request.session["new_customer_amount"] = str(self.customer.amount)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return resolve_url(self.object)
|
|
|
|
|
2016-04-18 15:34:21 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Add customer to the context."""
|
2024-06-27 12:46:43 +00:00
|
|
|
kwargs = super().get_context_data(**kwargs)
|
2024-12-22 11:27:58 +00:00
|
|
|
kwargs["products"] = self.products
|
2019-09-10 11:41:43 +00:00
|
|
|
kwargs["categories"] = {}
|
|
|
|
for product in kwargs["products"]:
|
|
|
|
if product.product_type:
|
|
|
|
kwargs["categories"].setdefault(product.product_type, []).append(
|
|
|
|
product
|
|
|
|
)
|
2018-10-04 19:29:19 +00:00
|
|
|
kwargs["customer"] = self.customer
|
2024-12-20 19:21:36 +00:00
|
|
|
kwargs["cancel_url"] = self.get_success_url()
|
2024-12-15 23:15:21 +00:00
|
|
|
|
2024-12-20 19:18:02 +00:00
|
|
|
# To get all forms errors to the javascript, we create a list of error list
|
2024-12-22 11:00:42 +00:00
|
|
|
kwargs["form_errors"] = [
|
|
|
|
list(field_error.values()) for field_error in kwargs["form"].errors
|
|
|
|
]
|
2024-12-15 23:15:21 +00:00
|
|
|
if self.object.type == "BAR":
|
|
|
|
kwargs["student_card_fragment"] = StudentCardFormView.get_template_data(
|
|
|
|
self.customer
|
|
|
|
).render(self.request)
|
2024-12-15 20:33:19 +00:00
|
|
|
|
|
|
|
if self.object.can_refill():
|
|
|
|
kwargs["refilling_fragment"] = RefillingCreateView.get_template_data(
|
|
|
|
self.customer
|
|
|
|
).render(self.request)
|
2024-12-20 16:32:37 +00:00
|
|
|
|
2016-04-18 15:34:21 +00:00
|
|
|
return kwargs
|
2024-12-15 20:33:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RefillingCreateView(FormView):
|
|
|
|
"""This is a fragment only view which integrates with counter_click.jinja"""
|
|
|
|
|
|
|
|
form_class = RefillForm
|
|
|
|
template_name = "counter/fragments/create_refill.jinja"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_template_data(
|
|
|
|
cls, customer: Customer, *, form_instance: form_class | None = None
|
|
|
|
) -> FormFragmentTemplateData[form_class]:
|
2024-12-17 00:41:45 +00:00
|
|
|
return FormFragmentTemplateData(
|
2024-12-15 20:33:19 +00:00
|
|
|
form=form_instance if form_instance else cls.form_class(),
|
|
|
|
template=cls.template_name,
|
|
|
|
context={
|
|
|
|
"action": reverse_lazy(
|
|
|
|
"counter:refilling_create", kwargs={"customer_id": customer.pk}
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.customer: Customer = get_object_or_404(Customer, pk=kwargs["customer_id"])
|
|
|
|
if not self.customer.can_buy:
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
if not is_logged_in_counter(request):
|
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
self.counter: Counter = get_object_or_404(
|
|
|
|
Counter, token=request.session["counter_token"]
|
|
|
|
)
|
|
|
|
|
|
|
|
if not self.counter.can_refill():
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2024-12-20 16:32:37 +00:00
|
|
|
self.operator = get_operator(self.counter, self.customer)
|
2024-12-15 20:33:19 +00:00
|
|
|
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
res = super().form_valid(form)
|
|
|
|
form.clean()
|
|
|
|
form.instance.counter = self.counter
|
|
|
|
form.instance.operator = self.operator
|
|
|
|
form.instance.customer = self.customer
|
|
|
|
form.instance.save()
|
|
|
|
return res
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
data = self.get_template_data(self.customer, form_instance=context["form"])
|
|
|
|
context.update(data.context)
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return self.request.path
|