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-06-24 11:07:36 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2024-12-06 23:06:33 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.views.generic.edit import DeleteView, FormView
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2024-11-14 15:17:10 +00:00
|
|
|
from core.views import AllowFragment, CanEditMixin
|
2024-12-06 23:06:33 +00:00
|
|
|
from counter.forms import StudentCardForm
|
2024-11-14 15:17:10 +00:00
|
|
|
from counter.models import Counter, Customer, StudentCard
|
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}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-11-14 15:17:10 +00:00
|
|
|
class StudentCardFormView(AllowFragment, 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}
|
|
|
|
)
|
2024-11-14 15:17:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StudentCardFormFragmentView(FormView):
|
|
|
|
"""
|
|
|
|
Add a new student card from a counter
|
|
|
|
This is a fragment only view which integrates with counter_click.jinja
|
|
|
|
"""
|
|
|
|
|
|
|
|
form_class = StudentCardForm
|
|
|
|
template_name = "counter/add_student_card_fragment.jinja"
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.counter = get_object_or_404(
|
|
|
|
Counter.objects.annotate_is_open(), pk=kwargs["counter_id"]
|
|
|
|
)
|
|
|
|
self.customer = get_object_or_404(
|
|
|
|
Customer.objects.prefetch_related("student_cards"), pk=kwargs["customer_id"]
|
|
|
|
)
|
|
|
|
if not (
|
|
|
|
self.counter.type == "BAR"
|
|
|
|
and "counter_token" in request.session
|
|
|
|
and request.session["counter_token"] == self.counter.token
|
|
|
|
and self.counter.is_open
|
|
|
|
):
|
|
|
|
raise PermissionDenied
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
data = form.clean()
|
|
|
|
res = super().form_valid(form)
|
|
|
|
StudentCard(customer=self.customer, uid=data["uid"]).save()
|
|
|
|
return res
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["counter"] = self.counter
|
|
|
|
context["customer"] = self.customer
|
|
|
|
context["action"] = self.request.path
|
|
|
|
context["student_cards"] = self.customer.student_cards.all()
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return reverse_lazy(
|
|
|
|
"counter:add_student_card_fragment",
|
|
|
|
kwargs={
|
|
|
|
"customer_id": self.customer.pk,
|
|
|
|
"counter_id": self.counter.id,
|
|
|
|
},
|
|
|
|
)
|