From ad14f2fe128e175221d6dff1fe5155e7e5ea9d9c Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Sat, 6 Apr 2024 19:42:32 +0200 Subject: [PATCH] Complex form --- project/jinja2env.py | 14 +++- project/main/templates/main/complex_form.html | 12 ++- .../templates/main/complex_form_content.html | 64 +++++++++++++++ .../templates/main/components/inputs.html | 19 +++++ project/main/views/__init__.py | 2 +- project/main/views/complex_form.py | 81 +++++++++++++++++++ project/urls.py | 1 + 7 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 project/main/templates/main/complex_form_content.html create mode 100644 project/main/templates/main/components/inputs.html diff --git a/project/jinja2env.py b/project/jinja2env.py index ba260fb..4def31a 100644 --- a/project/jinja2env.py +++ b/project/jinja2env.py @@ -1,3 +1,4 @@ +import random from typing import Any from jinja2 import Environment @@ -11,10 +12,17 @@ def conditional_cls(conditions: dict[str, Any]) -> str: return " ".join(result) +def random_id() -> str: + return f"_{random.randint(0, 10**8)}" + + def environment(**options): env = Environment(**options) - env.globals.update({ - "conditional_cls": conditional_cls, - }) + env.globals.update( + { + "conditional_cls": conditional_cls, + "random_id": random_id, + } + ) return env diff --git a/project/main/templates/main/complex_form.html b/project/main/templates/main/complex_form.html index 047a9d0..18c060b 100644 --- a/project/main/templates/main/complex_form.html +++ b/project/main/templates/main/complex_form.html @@ -2,7 +2,13 @@ {% block content %} -

- This is some complex form content bellow. -

+
+
+ {% include "main/complex_form_content.html" %} +
+
{% endblock %} diff --git a/project/main/templates/main/complex_form_content.html b/project/main/templates/main/complex_form_content.html new file mode 100644 index 0000000..c6d59b2 --- /dev/null +++ b/project/main/templates/main/complex_form_content.html @@ -0,0 +1,64 @@ +{% from "main/components/inputs.html" import checkbox %} + +{% set indent_1 = "ms-4" %} +{% set indent_2 = "ms-5" %} + +{{ checkbox( + title="Route module", + name="route_module", + state=state.route_module +) }} +{{ checkbox( + title="Smart routing", + name="smart_routing", + state=state.smart_routing, + cls=indent_1 +) }} +{{ checkbox( + title="Geo routing", + name="geo_routing", + state=state.geo_routing, + cls=indent_1 +) }} +{{ checkbox( + title="Use packing model per route", + name="use_packing_model_per_route", + state=state.use_packing_model_per_route, + cls=indent_1 +) }} +{{ checkbox( + title="Prekitting to box", + name="prekitting_to_box", + state=state.prekitting_to_box, + cls=indent_2 +) }} +{{ checkbox( + title="Prekitting to pallet", + name="prekitting_to_pallet", + state=state.prekitting_to_pallet, + cls=indent_2 +) }} +{{ checkbox( + title="Real time stock", + name="real_time_stock", + state=state.real_time_stock, + cls=indent_2 +) }} +{{ checkbox( + title="Use packing model per machine", + name="use_packing_model_per_machine", + state=state.use_packing_model_per_machine, + cls=indent_1 +) }} +{{ checkbox( + title="Warehouse", + name="warehouse", + state=state.warehouse, + cls=indent_1 +) }} +{{ checkbox( + title="Custom forms in routing", + name="custom_forms_in_routing", + state=state.custom_forms_in_routing, + cls=indent_1 +) }} diff --git a/project/main/templates/main/components/inputs.html b/project/main/templates/main/components/inputs.html new file mode 100644 index 0000000..3fa08e2 --- /dev/null +++ b/project/main/templates/main/components/inputs.html @@ -0,0 +1,19 @@ +{% macro checkbox(title, name, checked, state, cls="") %} + {% if state.visible %} + {% set id = random_id() %} + +
+ + +
+ {% endif %} +{% endmacro %} diff --git a/project/main/views/__init__.py b/project/main/views/__init__.py index 10fec0c..921e792 100644 --- a/project/main/views/__init__.py +++ b/project/main/views/__init__.py @@ -1,4 +1,4 @@ -from .complex_form import ComplexFormView +from .complex_form import ComplexFormHandleView, ComplexFormView from .filter_list import FilterListView from .form_validation import FormValidationView from .home import HomeView diff --git a/project/main/views/complex_form.py b/project/main/views/complex_form.py index 84244b5..77e9afe 100644 --- a/project/main/views/complex_form.py +++ b/project/main/views/complex_form.py @@ -1,7 +1,88 @@ +from dataclasses import dataclass +from typing import Any + +from django.core.handlers.wsgi import WSGIRequest +from django.http import HttpResponse +from django.views.generic import TemplateView + from project.main.views.demo_view_base import DemoViewBase +@dataclass +class ComplexFormContext: + ... + + +@dataclass +class ControlState: + checked: bool = False + visible: bool = True + enabled: bool = True + + +@dataclass +class FormState: + route_module: ControlState + smart_routing: ControlState + geo_routing: ControlState + use_packing_model_per_route: ControlState + prekitting_to_box: ControlState + prekitting_to_pallet: ControlState + real_time_stock: ControlState + use_packing_model_per_machine: ControlState + warehouse: ControlState + custom_forms_in_routing: ControlState + + +def form_to_state(values: dict) -> FormState: + return FormState( + route_module=ControlState(checked=values.get("route_module") == "on"), + smart_routing=ControlState(checked=values.get("smart_routing") == "on"), + geo_routing=ControlState(checked=values.get("geo_routing") == "on"), + use_packing_model_per_route=ControlState( + checked=values.get("use_packing_model_per_route") == "on" + ), + prekitting_to_box=ControlState(checked=values.get("prekitting_to_box") == "on"), + prekitting_to_pallet=ControlState( + checked=values.get("prekitting_to_pallet") == "on" + ), + real_time_stock=ControlState(checked=values.get("real_time_stock") == "on"), + use_packing_model_per_machine=ControlState( + checked=values.get("use_packing_model_per_machine") == "on" + ), + warehouse=ControlState(checked=values.get("warehouse") == "on"), + custom_forms_in_routing=ControlState( + checked=values.get("custom_forms_in_routing") == "on" + ), + ) + + class ComplexFormView(DemoViewBase): template_name = "main/complex_form.html" active_section = "complex-form" title = "Complex Form" + + def get_context_data(self, **kwargs) -> dict[str, Any]: + context = super().get_context_data(**kwargs) + context.update( + { + "state": form_to_state(values={}), + } + ) + return context + + def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponse: + ... + + +class ComplexFormHandleView(TemplateView): + template_name = "main/complex_form_content.html" + + def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponse: + state = form_to_state(values=request.POST) + + return self.render_to_response( + context={ + "state": state, + } + ) diff --git a/project/urls.py b/project/urls.py index 61a3921..8567348 100644 --- a/project/urls.py +++ b/project/urls.py @@ -8,5 +8,6 @@ urlpatterns = [ path("filter-list", views.FilterListView.as_view(), name="filter-list"), path("form-validation", views.FormValidationView.as_view(), name="form-validation"), path("complex-form", views.ComplexFormView.as_view(), name="complex-form"), + path("complex-form/handle", views.ComplexFormHandleView.as_view(), name="complex-form-handle"), path("table-inline-edit", views.TableInlineEditView.as_view(), name="table-inline-edit"), ]