Sith/counter/views/home.py

148 lines
5.5 KiB
Python
Raw Permalink Normal View History

#
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith
#
# 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
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
2024-11-27 18:02:48 +00:00
from datetime import timedelta
2024-06-24 11:07:36 +00:00
from django.conf import settings
2024-12-06 23:10:46 +00:00
from django.http import HttpResponseRedirect
2024-06-24 11:07:36 +00:00
from django.urls import reverse, reverse_lazy
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
2024-11-27 18:02:48 +00:00
from django.views.generic import DetailView
2024-12-06 23:10:46 +00:00
from django.views.generic.edit import FormMixin, ProcessFormView
2016-03-28 12:54:35 +00:00
2024-12-06 23:10:46 +00:00
from core.views import CanViewMixin
2024-06-24 11:07:36 +00:00
from core.views.forms import LoginForm
2024-12-06 23:10:46 +00:00
from counter.forms import GetUserForm
from counter.models import Counter
2024-08-05 08:46:15 +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-10-06 11:22:09 +00:00
2017-06-12 07:47:24 +00:00
2018-10-04 19:29:19 +00:00
class CounterMain(
CounterTabsMixin, CanViewMixin, DetailView, ProcessFormView, FormMixin
):
2024-07-12 07:34:16 +00:00
"""The public (barman) view."""
2018-10-04 19:29:19 +00:00
2016-03-28 12:54:35 +00:00
model = Counter
2018-10-04 19:29:19 +00:00
template_name = "counter/counter_main.jinja"
2016-03-28 12:54:35 +00:00
pk_url_kwarg = "counter_id"
2018-10-04 19:29:19 +00:00
form_class = (
2019-11-04 12:46:09 +00:00
GetUserForm # Form to enter a client code and get the corresponding user id
)
2016-09-28 09:07:32 +00:00
current_tab = "counter"
2016-03-29 08:30:24 +00:00
def post(self, request, *args, **kwargs):
self.object = self.get_object()
2018-10-04 19:29:19 +00:00
if self.object.type == "BAR" and not (
"counter_token" in self.request.session
2018-10-04 19:29:19 +00:00
and self.request.session["counter_token"] == self.object.token
): # Check the token to avoid the bar to be stolen
return HttpResponseRedirect(
reverse_lazy(
"counter:details",
args=self.args,
kwargs={"counter_id": self.object.id},
)
+ "?bad_location"
)
2024-06-27 12:46:43 +00:00
return super().post(request, *args, **kwargs)
2016-04-12 08:00:47 +00:00
def get_context_data(self, **kwargs):
2024-07-12 07:34:16 +00:00
"""We handle here the login form for the barman."""
2018-10-04 19:29:19 +00:00
if self.request.method == "POST":
self.object = self.get_object()
2016-09-12 15:34:33 +00:00
self.object.update_activity()
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["login_form"] = LoginForm()
kwargs["login_form"].fields["username"].widget.attrs["autofocus"] = True
kwargs[
"login_form"
].cleaned_data = {} # add_error fails if there are no cleaned_data
2016-08-20 14:09:46 +00:00
if "credentials" in self.request.GET:
2018-10-04 19:29:19 +00:00
kwargs["login_form"].add_error(None, _("Bad credentials"))
if "sellers" in self.request.GET:
2018-10-04 19:29:19 +00:00
kwargs["login_form"].add_error(None, _("User is not barman"))
kwargs["form"] = self.get_form()
kwargs["form"].cleaned_data = {} # same as above
if "bad_location" in self.request.GET:
2018-10-04 19:29:19 +00:00
kwargs["form"].add_error(
None, _("Bad location, someone is already logged in somewhere else")
)
if self.object.type == "BAR":
2024-07-21 08:44:43 +00:00
kwargs["barmen"] = self.object.barmen_list
elif self.request.user.is_authenticated:
2018-10-04 19:29:19 +00:00
kwargs["barmen"] = [self.request.user]
if "last_basket" in self.request.session:
2018-10-04 19:29:19 +00:00
kwargs["last_basket"] = self.request.session.pop("last_basket")
kwargs["last_customer"] = self.request.session.pop("last_customer")
kwargs["last_total"] = self.request.session.pop("last_total")
kwargs["new_customer_amount"] = self.request.session.pop(
"new_customer_amount"
)
2016-04-15 09:50:31 +00:00
return kwargs
def form_valid(self, form):
2024-07-12 07:34:16 +00:00
"""We handle here the redirection, passing the user id of the asked customer."""
2018-10-04 19:29:19 +00:00
self.kwargs["user_id"] = form.cleaned_data["user_id"]
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse_lazy("counter:click", args=self.args, kwargs=self.kwargs)
2017-06-12 07:47:24 +00:00
2016-09-28 09:07:32 +00:00
class CounterLastOperationsView(CounterTabsMixin, CanViewMixin, DetailView):
2024-07-12 07:34:16 +00:00
"""Provide the last operations to allow barmen to delete them."""
2018-10-04 19:29:19 +00:00
2016-09-28 09:07:32 +00:00
model = Counter
pk_url_kwarg = "counter_id"
2018-10-04 19:29:19 +00:00
template_name = "counter/last_ops.jinja"
2016-09-28 09:07:32 +00:00
current_tab = "last_ops"
def dispatch(self, request, *args, **kwargs):
2024-07-12 07:34:16 +00:00
"""We have here again a very particular right handling."""
2016-09-28 09:07:32 +00:00
self.object = self.get_object()
2024-08-05 08:46:15 +00:00
if is_logged_in_counter(request) and self.object.barmen_list:
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2018-10-04 19:29:19 +00:00
return HttpResponseRedirect(
reverse("counter:details", kwargs={"counter_id": self.object.id})
+ "?bad_location"
)
2016-09-28 09:07:32 +00:00
def get_context_data(self, **kwargs):
2024-07-12 07:34:16 +00:00
"""Add form to the context."""
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
threshold = timezone.now() - timedelta(
minutes=settings.SITH_LAST_OPERATIONS_LIMIT
)
2024-09-30 13:57:21 +00:00
kwargs["last_refillings"] = (
self.object.refillings.filter(date__gte=threshold)
.select_related("operator", "customer__user")
.order_by("-id")[:20]
)
kwargs["last_sellings"] = (
self.object.sellings.filter(date__gte=threshold)
.select_related("seller", "customer__user")
.order_by("-id")[:20]
)
2016-09-28 09:07:32 +00:00
return kwargs
2017-06-12 07:47:24 +00:00
2016-09-12 15:34:33 +00:00
class CounterActivityView(DetailView):
2024-07-12 07:34:16 +00:00
"""Show the bar activity."""
2018-10-04 19:29:19 +00:00
2016-09-12 15:34:33 +00:00
model = Counter
pk_url_kwarg = "counter_id"
2018-10-04 19:29:19 +00:00
template_name = "counter/activity.jinja"