mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Remove student card creation from CounterClick view and use fragment instead
Intercept htmx on submit requests, this allows auto submit from nfc fields Fix super call with parameters Add loading wheel on student card form for counter_click.jinja
This commit is contained in:
@ -27,8 +27,8 @@ from django.utils.translation import gettext_lazy as _
|
||||
from django.views.generic import DetailView
|
||||
|
||||
from core.views import CanViewMixin
|
||||
from counter.forms import NFCCardForm, RefillForm
|
||||
from counter.models import Counter, Customer, Product, Selling, StudentCard
|
||||
from counter.forms import RefillForm
|
||||
from counter.models import Counter, Customer, Product, Selling
|
||||
from counter.views.mixins import CounterTabsMixin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -134,7 +134,6 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
|
||||
request.session["too_young"] = False
|
||||
request.session["not_allowed"] = False
|
||||
request.session["no_age"] = False
|
||||
request.session["not_valid_student_card_uid"] = False
|
||||
if self.object.type != "BAR":
|
||||
self.operator = request.user
|
||||
elif self.customer_is_barman():
|
||||
@ -146,8 +145,6 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
|
||||
action = parse_qs(request.body.decode()).get("action", [""])[0]
|
||||
if action == "add_product":
|
||||
self.add_product(request)
|
||||
elif action == "add_student_card":
|
||||
self.add_student_card(request)
|
||||
elif action == "del_product":
|
||||
self.del_product(request)
|
||||
elif action == "refill":
|
||||
@ -284,23 +281,6 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
|
||||
request.session.modified = True
|
||||
return True
|
||||
|
||||
def add_student_card(self, request):
|
||||
"""Add a new student card on the customer account."""
|
||||
uid = str(request.POST["student_card_uid"])
|
||||
if not StudentCard.is_valid(uid):
|
||||
request.session["not_valid_student_card_uid"] = True
|
||||
return False
|
||||
|
||||
if not (
|
||||
self.object.type == "BAR"
|
||||
and "counter_token" in request.session
|
||||
and request.session["counter_token"] == self.object.token
|
||||
and self.object.is_open
|
||||
):
|
||||
raise PermissionDenied
|
||||
StudentCard(customer=self.customer, uid=uid).save()
|
||||
return True
|
||||
|
||||
def del_product(self, request):
|
||||
"""Delete a product from the basket."""
|
||||
pid = parse_qs(request.body.decode())["product_id"][0]
|
||||
@ -431,10 +411,7 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
|
||||
product
|
||||
)
|
||||
kwargs["customer"] = self.customer
|
||||
kwargs["student_cards"] = self.customer.student_cards.all()
|
||||
kwargs["student_card_input"] = NFCCardForm()
|
||||
kwargs["basket_total"] = self.sum_basket(self.request)
|
||||
kwargs["refill_form"] = self.refill_form or RefillForm()
|
||||
kwargs["student_card_max_uid_size"] = StudentCard.UID_SIZE
|
||||
kwargs["barmens_can_refill"] = self.object.can_refill()
|
||||
return kwargs
|
||||
|
@ -18,9 +18,9 @@ from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic.edit import DeleteView, FormView
|
||||
|
||||
from core.views import CanEditMixin
|
||||
from core.views import AllowFragment, CanEditMixin
|
||||
from counter.forms import StudentCardForm
|
||||
from counter.models import Customer, StudentCard
|
||||
from counter.models import Counter, Customer, StudentCard
|
||||
|
||||
|
||||
class StudentCardDeleteView(DeleteView, CanEditMixin):
|
||||
@ -40,7 +40,7 @@ class StudentCardDeleteView(DeleteView, CanEditMixin):
|
||||
)
|
||||
|
||||
|
||||
class StudentCardFormView(FormView):
|
||||
class StudentCardFormView(AllowFragment, FormView):
|
||||
"""Add a new student card."""
|
||||
|
||||
form_class = StudentCardForm
|
||||
@ -62,3 +62,52 @@ class StudentCardFormView(FormView):
|
||||
return reverse_lazy(
|
||||
"core:user_prefs", kwargs={"user_id": self.customer.user.pk}
|
||||
)
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
|
Reference in New Issue
Block a user