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-07 16:28:34 +00:00
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2024-12-08 15:07:25 +00:00
|
|
|
from django.http import Http404, HttpRequest, HttpResponse
|
2024-12-06 23:06:33 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2024-12-08 15:07:25 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.translation import gettext as _
|
2024-12-06 23:06:33 +00:00
|
|
|
from django.views.generic.edit import DeleteView, FormView
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2024-12-08 10:45:16 +00:00
|
|
|
from core.utils import FormFragmentTemplateData
|
2024-12-10 22:48:46 +00:00
|
|
|
from core.views import can_edit
|
2024-12-06 23:06:33 +00:00
|
|
|
from counter.forms import StudentCardForm
|
2024-12-07 16:28:34 +00:00
|
|
|
from counter.models import Customer, StudentCard
|
|
|
|
from counter.utils import is_logged_in_counter
|
2024-10-06 11:22:09 +00:00
|
|
|
|
2017-06-12 07:47:24 +00:00
|
|
|
|
2024-12-10 22:48:46 +00:00
|
|
|
class StudentCardDeleteView(DeleteView):
|
|
|
|
"""View used to delete a card from a user. This is a fragment view !"""
|
2018-10-19 17:25:55 +00:00
|
|
|
|
|
|
|
model = StudentCard
|
2024-12-10 22:48:46 +00:00
|
|
|
template_name = "counter/fragments/delete_student_card.jinja"
|
2018-10-19 17:25:55 +00:00
|
|
|
|
2024-12-10 22:48:46 +00:00
|
|
|
def dispatch(self, request: HttpRequest, *args, **kwargs):
|
2018-10-19 17:25:55 +00:00
|
|
|
self.customer = get_object_or_404(Customer, pk=kwargs["customer_id"])
|
2024-12-10 22:48:46 +00:00
|
|
|
if not is_logged_in_counter(request) and not can_edit(
|
|
|
|
self.get_object(), 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
|
|
|
|
2024-12-10 22:48:46 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["action"] = self.request.path
|
|
|
|
context["action_cancel"] = self.get_success_url()
|
|
|
|
return context
|
|
|
|
|
2024-12-08 15:07:25 +00:00
|
|
|
def get_object(self, queryset=None):
|
|
|
|
if not hasattr(self.customer, "student_card"):
|
|
|
|
raise Http404(
|
|
|
|
_("%(name)s has no registered student card")
|
|
|
|
% {"name": self.customer.user.get_full_name()}
|
|
|
|
)
|
|
|
|
return self.customer.student_card
|
|
|
|
|
2018-10-19 17:25:55 +00:00
|
|
|
def get_success_url(self, **kwargs):
|
2024-12-10 22:48:46 +00:00
|
|
|
return reverse(
|
|
|
|
"counter:add_student_card", kwargs={"customer_id": self.customer.pk}
|
|
|
|
)
|
2018-10-19 17:25:55 +00:00
|
|
|
|
|
|
|
|
2024-12-07 16:28:34 +00:00
|
|
|
class StudentCardFormView(FormView):
|
|
|
|
"""Add a new student card. This is a fragment view !"""
|
2024-11-14 15:17:10 +00:00
|
|
|
|
|
|
|
form_class = StudentCardForm
|
2024-12-07 16:28:34 +00:00
|
|
|
template_name = "counter/fragments/create_student_card.jinja"
|
2024-11-14 15:17:10 +00:00
|
|
|
|
2024-12-07 23:32:28 +00:00
|
|
|
@classmethod
|
|
|
|
def get_template_data(
|
2024-12-08 10:45:16 +00:00
|
|
|
cls, customer: Customer
|
2024-12-08 15:07:25 +00:00
|
|
|
) -> FormFragmentTemplateData[StudentCardForm]:
|
2024-12-07 23:32:28 +00:00
|
|
|
"""Get necessary data to pre-render the fragment"""
|
2024-12-08 15:07:25 +00:00
|
|
|
return FormFragmentTemplateData(
|
2024-12-07 23:32:28 +00:00
|
|
|
form=cls.form_class(),
|
|
|
|
template=cls.template_name,
|
|
|
|
context={
|
2024-12-08 15:07:25 +00:00
|
|
|
"action": reverse(
|
2024-12-07 23:32:28 +00:00
|
|
|
"counter:add_student_card", kwargs={"customer_id": customer.pk}
|
|
|
|
),
|
|
|
|
"customer": customer,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-12-07 16:28:34 +00:00
|
|
|
def dispatch(self, request: HttpRequest, *args, **kwargs):
|
2024-11-14 15:17:10 +00:00
|
|
|
self.customer = get_object_or_404(
|
2024-12-08 15:07:25 +00:00
|
|
|
Customer.objects.select_related("student_card"), pk=kwargs["customer_id"]
|
2024-11-14 15:17:10 +00:00
|
|
|
)
|
2024-12-07 16:28:34 +00:00
|
|
|
|
|
|
|
if not is_logged_in_counter(request) and not StudentCard.can_create(
|
|
|
|
self.customer, request.user
|
2024-11-14 15:17:10 +00:00
|
|
|
):
|
|
|
|
raise PermissionDenied
|
2024-12-07 16:28:34 +00:00
|
|
|
|
2024-11-14 15:17:10 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
2024-12-08 15:07:25 +00:00
|
|
|
def form_valid(self, form: StudentCardForm) -> HttpResponse:
|
2024-11-14 15:17:10 +00:00
|
|
|
data = form.clean()
|
2024-12-08 15:07:25 +00:00
|
|
|
StudentCard.objects.update_or_create(
|
|
|
|
customer=self.customer, defaults={"uid": data["uid"]}
|
|
|
|
)
|
|
|
|
return super().form_valid(form)
|
2024-11-14 15:17:10 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2024-12-08 10:45:16 +00:00
|
|
|
data = self.get_template_data(self.customer)
|
2024-12-07 23:32:28 +00:00
|
|
|
context.update(data.context)
|
2024-11-14 15:17:10 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2024-12-07 16:28:34 +00:00
|
|
|
return self.request.path
|