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-11-27 18:02:48 +00:00
|
|
|
from datetime import timedelta
|
2017-04-24 15:51:12 +00:00
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import PermissionDenied
|
2023-01-10 21:26:46 +00:00
|
|
|
from django.db.models import F
|
2024-11-27 18:02:48 +00:00
|
|
|
from django.http import HttpRequest, HttpResponseRedirect
|
2024-07-21 08:44:43 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
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 _
|
2022-11-28 16:03:46 +00:00
|
|
|
from django.views.decorators.http import require_POST
|
2024-11-27 18:02:48 +00:00
|
|
|
from django.views.generic import DetailView
|
|
|
|
from django.views.generic.edit import DeleteView, FormMixin, FormView, ProcessFormView
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2024-11-27 18:02:48 +00:00
|
|
|
from core.views import CanEditMixin, CanViewMixin
|
2024-06-24 11:07:36 +00:00
|
|
|
from core.views.forms import LoginForm
|
2024-11-27 18:02:48 +00:00
|
|
|
from counter.forms import GetUserForm, StudentCardForm
|
|
|
|
from counter.models import Counter, Customer, Permanency, StudentCard
|
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-11-11 21:56:51 +00:00
|
|
|
class StudentCardDeleteView(DeleteView, CanEditMixin):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""View used to delete a card from a user."""
|
2018-10-19 17:25:55 +00:00
|
|
|
|
|
|
|
model = StudentCard
|
|
|
|
template_name = "core/delete_confirm.jinja"
|
|
|
|
pk_url_kwarg = "card_id"
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.customer = get_object_or_404(Customer, pk=kwargs["customer_id"])
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2018-10-19 17:25:55 +00:00
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return reverse_lazy(
|
|
|
|
"core:user_prefs", kwargs={"user_id": self.customer.user.pk}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-09-26 09:17:00 +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 (
|
2024-10-15 09:36:26 +00:00
|
|
|
"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-09-26 09:17:00 +00:00
|
|
|
|
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":
|
2016-04-18 02:37:37 +00:00
|
|
|
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"))
|
2016-09-08 23:54:26 +00:00
|
|
|
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
|
2016-09-26 09:17:00 +00:00
|
|
|
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
|
2019-10-05 23:34:21 +00:00
|
|
|
elif self.request.user.is_authenticated:
|
2018-10-04 19:29:19 +00:00
|
|
|
kwargs["barmen"] = [self.request.user]
|
2024-10-15 09:36:26 +00:00
|
|
|
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
|
|
|
|
|
2016-04-18 02:37:37 +00:00
|
|
|
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)
|
2016-04-18 02:37:37 +00:00
|
|
|
|
|
|
|
def get_success_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse_lazy("counter:click", args=self.args, kwargs=self.kwargs)
|
2016-04-18 02:37:37 +00:00
|
|
|
|
2017-06-12 07:47:24 +00:00
|
|
|
|
2024-07-21 08:44:43 +00:00
|
|
|
@require_POST
|
|
|
|
def counter_login(request: HttpRequest, counter_id: int) -> HttpResponseRedirect:
|
|
|
|
"""Log a user in a counter.
|
2016-04-12 08:00:47 +00:00
|
|
|
|
2024-07-21 08:44:43 +00:00
|
|
|
A successful login will result in the beginning of a counter duty
|
|
|
|
for the user.
|
2016-04-12 08:00:47 +00:00
|
|
|
"""
|
2024-07-21 08:44:43 +00:00
|
|
|
counter = get_object_or_404(Counter, pk=counter_id)
|
|
|
|
form = LoginForm(request, data=request.POST)
|
|
|
|
if not form.is_valid():
|
|
|
|
return redirect(counter.get_absolute_url() + "?credentials")
|
|
|
|
user = form.get_user()
|
|
|
|
if not counter.sellers.contains(user) or user in counter.barmen_list:
|
|
|
|
return redirect(counter.get_absolute_url() + "?sellers")
|
|
|
|
if len(counter.barmen_list) == 0:
|
|
|
|
counter.gen_token()
|
|
|
|
request.session["counter_token"] = counter.token
|
|
|
|
counter.permanencies.create(user=user, start=timezone.now())
|
|
|
|
return redirect(counter)
|
|
|
|
|
|
|
|
|
|
|
|
@require_POST
|
|
|
|
def counter_logout(request: HttpRequest, counter_id: int) -> HttpResponseRedirect:
|
|
|
|
"""End the permanency of a user in this counter."""
|
|
|
|
Permanency.objects.filter(counter=counter_id, user=request.POST["user_id"]).update(
|
|
|
|
end=F("activity")
|
|
|
|
)
|
|
|
|
return redirect("counter:details", counter_id=counter_id)
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2016-03-31 08:36:00 +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"
|
2016-09-12 15:34:33 +00:00
|
|
|
|
2017-06-12 07:47:24 +00:00
|
|
|
|
2018-10-19 17:25:55 +00:00
|
|
|
class StudentCardFormView(FormView):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Add a new student card."""
|
2018-10-19 17:25:55 +00:00
|
|
|
|
|
|
|
form_class = StudentCardForm
|
|
|
|
template_name = "core/create.jinja"
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.customer = get_object_or_404(Customer, pk=kwargs["customer_id"])
|
2018-11-11 21:56:51 +00:00
|
|
|
if not StudentCard.can_create(self.customer, request.user):
|
|
|
|
raise PermissionDenied
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2018-10-19 17:25:55 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
data = form.clean()
|
|
|
|
res = super(FormView, self).form_valid(form)
|
2018-11-11 21:56:51 +00:00
|
|
|
StudentCard(customer=self.customer, uid=data["uid"]).save()
|
2018-10-19 17:25:55 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return reverse_lazy(
|
|
|
|
"core:user_prefs", kwargs={"user_id": self.customer.user.pk}
|
|
|
|
)
|