mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
add FragmentMixin
and UseFragmentsMixin
classes
This commit is contained in:
@ -56,7 +56,7 @@ from counter.views.home import (
|
||||
CounterMain,
|
||||
)
|
||||
from counter.views.invoice import InvoiceCallView
|
||||
from counter.views.student_card import StudentCardDeleteView, StudentCardFormView
|
||||
from counter.views.student_card import StudentCardDeleteView, StudentCardFormFragment
|
||||
|
||||
urlpatterns = [
|
||||
path("<int:counter_id>/", CounterMain.as_view(), name="details"),
|
||||
@ -83,7 +83,7 @@ urlpatterns = [
|
||||
path("eticket/<int:selling_id>/pdf/", EticketPDFView.as_view(), name="eticket_pdf"),
|
||||
path(
|
||||
"customer/<int:customer_id>/card/add/",
|
||||
StudentCardFormView.as_view(),
|
||||
StudentCardFormFragment.as_view(),
|
||||
name="add_student_card",
|
||||
),
|
||||
path(
|
||||
|
@ -26,7 +26,8 @@ from django.forms import (
|
||||
)
|
||||
from django.http import Http404
|
||||
from django.shortcuts import get_object_or_404, redirect, resolve_url
|
||||
from django.urls import reverse_lazy
|
||||
from django.urls import reverse
|
||||
from django.utils.safestring import SafeString
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.views.generic import FormView
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
@ -34,7 +35,7 @@ from ninja.main import HttpRequest
|
||||
|
||||
from core.auth.mixins import CanViewMixin
|
||||
from core.models import User
|
||||
from core.utils import FormFragmentTemplateData
|
||||
from core.views.mixins import FragmentMixin, UseFragmentsMixin
|
||||
from counter.forms import RefillForm
|
||||
from counter.models import (
|
||||
Counter,
|
||||
@ -45,7 +46,7 @@ from counter.models import (
|
||||
)
|
||||
from counter.utils import is_logged_in_counter
|
||||
from counter.views.mixins import CounterTabsMixin
|
||||
from counter.views.student_card import StudentCardFormView
|
||||
from counter.views.student_card import StudentCardFormFragment
|
||||
|
||||
|
||||
def get_operator(request: HttpRequest, counter: Counter, customer: Customer) -> User:
|
||||
@ -163,7 +164,9 @@ BasketForm = formset_factory(
|
||||
)
|
||||
|
||||
|
||||
class CounterClick(CounterTabsMixin, CanViewMixin, SingleObjectMixin, FormView):
|
||||
class CounterClick(
|
||||
CounterTabsMixin, UseFragmentsMixin, CanViewMixin, SingleObjectMixin, FormView
|
||||
):
|
||||
"""The click view
|
||||
This is a detail view not to have to worry about loading the counter
|
||||
Everything is made by hand in the post method.
|
||||
@ -304,6 +307,18 @@ class CounterClick(CounterTabsMixin, CanViewMixin, SingleObjectMixin, FormView):
|
||||
def get_success_url(self):
|
||||
return resolve_url(self.object)
|
||||
|
||||
def get_fragment_context_data(self) -> dict[str, SafeString]:
|
||||
res = super().get_fragment_context_data()
|
||||
if self.object.type == "BAR":
|
||||
res["student_card_fragment"] = StudentCardFormFragment.as_fragment()(
|
||||
self.request, customer=self.customer
|
||||
)
|
||||
if self.object.can_refill():
|
||||
res["refilling_fragment"] = RefillingCreateView.as_fragment()(
|
||||
self.request, customer=self.customer
|
||||
)
|
||||
return res
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Add customer to the context."""
|
||||
kwargs = super().get_context_data(**kwargs)
|
||||
@ -321,39 +336,15 @@ class CounterClick(CounterTabsMixin, CanViewMixin, SingleObjectMixin, FormView):
|
||||
kwargs["form_errors"] = [
|
||||
list(field_error.values()) for field_error in kwargs["form"].errors
|
||||
]
|
||||
if self.object.type == "BAR":
|
||||
kwargs["student_card_fragment"] = StudentCardFormView.get_template_data(
|
||||
self.customer
|
||||
).render(self.request)
|
||||
|
||||
if self.object.can_refill():
|
||||
kwargs["refilling_fragment"] = RefillingCreateView.get_template_data(
|
||||
self.customer
|
||||
).render(self.request)
|
||||
|
||||
return kwargs
|
||||
|
||||
|
||||
class RefillingCreateView(FormView):
|
||||
class RefillingCreateView(FragmentMixin, FormView):
|
||||
"""This is a fragment only view which integrates with counter_click.jinja"""
|
||||
|
||||
form_class = RefillForm
|
||||
template_name = "counter/fragments/create_refill.jinja"
|
||||
|
||||
@classmethod
|
||||
def get_template_data(
|
||||
cls, customer: Customer, *, form_instance: form_class | None = None
|
||||
) -> FormFragmentTemplateData[form_class]:
|
||||
return FormFragmentTemplateData(
|
||||
form=form_instance if form_instance else cls.form_class(),
|
||||
template=cls.template_name,
|
||||
context={
|
||||
"action": reverse_lazy(
|
||||
"counter:refilling_create", kwargs={"customer_id": customer.pk}
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.customer: Customer = get_object_or_404(Customer, pk=kwargs["customer_id"])
|
||||
if not self.customer.can_buy:
|
||||
@ -373,6 +364,10 @@ class RefillingCreateView(FormView):
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def render_fragment(self, request, **kwargs) -> SafeString:
|
||||
self.customer = kwargs.pop("customer")
|
||||
return super().render_fragment(request, **kwargs)
|
||||
|
||||
def form_valid(self, form):
|
||||
res = super().form_valid(form)
|
||||
form.clean()
|
||||
@ -383,10 +378,11 @@ class RefillingCreateView(FormView):
|
||||
return res
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
data = self.get_template_data(self.customer, form_instance=context["form"])
|
||||
context.update(data.context)
|
||||
return context
|
||||
kwargs = super().get_context_data(**kwargs)
|
||||
kwargs["action"] = reverse(
|
||||
"counter:refilling_create", kwargs={"customer_id": self.customer.pk}
|
||||
)
|
||||
return kwargs
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
return self.request.path
|
||||
|
@ -13,16 +13,16 @@
|
||||
#
|
||||
#
|
||||
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.http import Http404, HttpRequest, HttpResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.utils.safestring import SafeString
|
||||
from django.utils.translation import gettext as _
|
||||
from django.views.generic.edit import DeleteView, FormView
|
||||
|
||||
from core.auth.mixins import can_edit
|
||||
from core.utils import FormFragmentTemplateData
|
||||
from core.views.mixins import FragmentMixin
|
||||
from counter.forms import StudentCardForm
|
||||
from counter.models import Customer, StudentCard
|
||||
from counter.utils import is_logged_in_counter
|
||||
@ -62,28 +62,12 @@ class StudentCardDeleteView(DeleteView):
|
||||
)
|
||||
|
||||
|
||||
class StudentCardFormView(FormView):
|
||||
"""Add a new student card. This is a fragment view !"""
|
||||
class StudentCardFormFragment(FragmentMixin, FormView):
|
||||
"""Add a new student card."""
|
||||
|
||||
form_class = StudentCardForm
|
||||
template_name = "counter/fragments/create_student_card.jinja"
|
||||
|
||||
@classmethod
|
||||
def get_template_data(
|
||||
cls, customer: Customer, *, form_instance: form_class | None = None
|
||||
) -> FormFragmentTemplateData[form_class]:
|
||||
"""Get necessary data to pre-render the fragment"""
|
||||
return FormFragmentTemplateData(
|
||||
form=form_instance if form_instance else cls.form_class(),
|
||||
template=cls.template_name,
|
||||
context={
|
||||
"action": reverse(
|
||||
"counter:add_student_card", kwargs={"customer_id": customer.pk}
|
||||
),
|
||||
"customer": customer,
|
||||
},
|
||||
)
|
||||
|
||||
def dispatch(self, request: HttpRequest, *args, **kwargs):
|
||||
self.customer = get_object_or_404(
|
||||
Customer.objects.select_related("student_card"), pk=kwargs["customer_id"]
|
||||
@ -96,6 +80,10 @@ class StudentCardFormView(FormView):
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def render_fragment(self, request, **kwargs) -> SafeString:
|
||||
self.customer = kwargs.pop("customer")
|
||||
return super().render_fragment(request, **kwargs)
|
||||
|
||||
def form_valid(self, form: StudentCardForm) -> HttpResponse:
|
||||
data = form.clean()
|
||||
StudentCard.objects.update_or_create(
|
||||
@ -104,10 +92,12 @@ class StudentCardFormView(FormView):
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
data = self.get_template_data(self.customer, form_instance=context["form"])
|
||||
context.update(data.context)
|
||||
return context
|
||||
return super().get_context_data(**kwargs) | {
|
||||
"action": reverse(
|
||||
"counter:add_student_card", kwargs={"customer_id": self.customer.pk}
|
||||
),
|
||||
"customer": self.customer,
|
||||
}
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
return self.request.path
|
||||
|
Reference in New Issue
Block a user