Sith/counter/views/main.py

223 lines
8.2 KiB
Python
Raw 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
from django.core.exceptions import PermissionDenied
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 _
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
class StudentCardDeleteView(DeleteView, CanEditMixin):
2024-07-12 07:34:16 +00:00
"""View used to delete a card from a user."""
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)
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
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
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
class StudentCardFormView(FormView):
2024-07-12 07:34:16 +00:00
"""Add a new student card."""
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"])
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)
def form_valid(self, form):
data = form.clean()
res = super(FormView, self).form_valid(form)
StudentCard(customer=self.customer, uid=data["uid"]).save()
return res
def get_success_url(self, **kwargs):
return reverse_lazy(
"core:user_prefs", kwargs={"user_id": self.customer.user.pk}
)