From 5e1989a60f27b4a016ca34d8bff5e156125349e9 Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Sat, 6 Apr 2024 22:04:57 +0200 Subject: [PATCH] Complex form --- .../container.html} | 3 +- .../route_module.html} | 46 +++++++--- .../templates/main/components/inputs.html | 30 ++++++- project/main/views/__init__.py | 2 +- project/main/views/complex_form.py | 88 ------------------- project/main/views/complex_form/__init__.py | 0 .../main/views/complex_form/state_models.py | 67 ++++++++++++++ project/main/views/complex_form/states.py | 0 project/main/views/complex_form/views.py | 82 +++++++++++++++++ 9 files changed, 211 insertions(+), 107 deletions(-) rename project/main/templates/main/{complex_form.html => complex_form/container.html} (81%) rename project/main/templates/main/{complex_form_content.html => complex_form/route_module.html} (58%) delete mode 100644 project/main/views/complex_form.py create mode 100644 project/main/views/complex_form/__init__.py create mode 100644 project/main/views/complex_form/state_models.py create mode 100644 project/main/views/complex_form/states.py create mode 100644 project/main/views/complex_form/views.py diff --git a/project/main/templates/main/complex_form.html b/project/main/templates/main/complex_form/container.html similarity index 81% rename from project/main/templates/main/complex_form.html rename to project/main/templates/main/complex_form/container.html index 18c060b..2d553b9 100644 --- a/project/main/templates/main/complex_form.html +++ b/project/main/templates/main/complex_form/container.html @@ -1,6 +1,5 @@ {% extends "main/base/layout.html" %} - {% block content %}
- {% include "main/complex_form_content.html" %} + {% include "main/complex_form/route_module.html" %}
{% endblock %} diff --git a/project/main/templates/main/complex_form_content.html b/project/main/templates/main/complex_form/route_module.html similarity index 58% rename from project/main/templates/main/complex_form_content.html rename to project/main/templates/main/complex_form/route_module.html index c6d59b2..d7cedfa 100644 --- a/project/main/templates/main/complex_form_content.html +++ b/project/main/templates/main/complex_form/route_module.html @@ -1,12 +1,14 @@ -{% from "main/components/inputs.html" import checkbox %} +{% from "main/components/inputs.html" import checkbox, select %} {% set indent_1 = "ms-4" %} {% set indent_2 = "ms-5" %} +{% set state = form_state.route_module %} + {{ checkbox( title="Route module", name="route_module", - state=state.route_module + state=state.enabled ) }} {{ checkbox( title="Smart routing", @@ -14,18 +16,38 @@ state=state.smart_routing, cls=indent_1 ) }} +{{ checkbox( + title="Predictive pickup", + name="predictive_pickup", + state=state.predictive_pickup, + cls=indent_2 +) }} +{{ checkbox( + title="Automatic planning", + name="automatic_planning", + state=state.automatic_planning, + cls=indent_2 +) }} {{ 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, + +{{ select( + title="Packing model", + name="packing_model", + options={ + "": "No packing model", + "per_route": "Packing model per route", + "per_machine": "Packing model per machine", + }, + state=state.packing_model, cls=indent_1 ) }} + + {{ checkbox( title="Prekitting to box", name="prekitting_to_box", @@ -44,18 +66,18 @@ 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="End warehouse tracking", + name="end_warehouse_tracking", + state=state.end_warehouse_tracking, + cls=indent_2 +) }} {{ checkbox( title="Custom forms in routing", name="custom_forms_in_routing", diff --git a/project/main/templates/main/components/inputs.html b/project/main/templates/main/components/inputs.html index 3fa08e2..50af88f 100644 --- a/project/main/templates/main/components/inputs.html +++ b/project/main/templates/main/components/inputs.html @@ -1,15 +1,15 @@ -{% macro checkbox(title, name, checked, state, cls="") %} +{% macro checkbox(title, name, state, cls="") %} {% if state.visible %} {% set id = random_id() %} - +
{% endif %} {% endmacro %} + + +{% macro select(title, name, options, state, cls="") %} + {% if state.visible %} +
+ +
+ {% endif %} +{% endmacro %} diff --git a/project/main/views/__init__.py b/project/main/views/__init__.py index 921e792..40fe17f 100644 --- a/project/main/views/__init__.py +++ b/project/main/views/__init__.py @@ -1,4 +1,4 @@ -from .complex_form import ComplexFormHandleView, ComplexFormView +from .complex_form.views 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 deleted file mode 100644 index 77e9afe..0000000 --- a/project/main/views/complex_form.py +++ /dev/null @@ -1,88 +0,0 @@ -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/main/views/complex_form/__init__.py b/project/main/views/complex_form/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/project/main/views/complex_form/state_models.py b/project/main/views/complex_form/state_models.py new file mode 100644 index 0000000..a3b6894 --- /dev/null +++ b/project/main/views/complex_form/state_models.py @@ -0,0 +1,67 @@ +from dataclasses import dataclass + + +@dataclass +class CheckboxState: + checked: bool = False + visible: bool = True + enabled: bool = True + + +@dataclass +class SelectState: + value: str = None + visible: bool = True + enabled: bool = True + + +@dataclass +class RouteModuleState: + enabled: CheckboxState + smart_routing: CheckboxState + predictive_pickup: CheckboxState + automatic_planning: CheckboxState + geo_routing: CheckboxState + packing_model: SelectState + prekitting_to_box: CheckboxState + prekitting_to_pallet: CheckboxState + real_time_stock: CheckboxState + warehouse: CheckboxState + end_warehouse_tracking: CheckboxState + custom_forms_in_routing: CheckboxState + + @staticmethod + def from_form(values: dict[str, str]) -> "RouteModuleState": + return RouteModuleState( + enabled=CheckboxState(checked=values.get("route_module") == "on"), + smart_routing=CheckboxState(checked=values.get("smart_routing") == "on"), + predictive_pickup=CheckboxState( + checked=values.get("predictive_pickup") == "on" + ), + automatic_planning=CheckboxState( + checked=values.get("automatic_planning") == "on" + ), + geo_routing=CheckboxState(checked=values.get("geo_routing") == "on"), + packing_model=SelectState(value=values.get("packing_model")), + prekitting_to_box=CheckboxState( + checked=values.get("prekitting_to_box") == "on" + ), + prekitting_to_pallet=CheckboxState( + checked=values.get("prekitting_to_pallet") == "on" + ), + real_time_stock=CheckboxState( + checked=values.get("real_time_stock") == "on" + ), + warehouse=CheckboxState(checked=values.get("warehouse") == "on"), + end_warehouse_tracking=CheckboxState( + checked=values.get("end_warehouse_tracking") == "on" + ), + custom_forms_in_routing=CheckboxState( + checked=values.get("custom_forms_in_routing") == "on" + ), + ) + + +@dataclass +class FormState: + route_module: RouteModuleState diff --git a/project/main/views/complex_form/states.py b/project/main/views/complex_form/states.py new file mode 100644 index 0000000..e69de29 diff --git a/project/main/views/complex_form/views.py b/project/main/views/complex_form/views.py new file mode 100644 index 0000000..8b08540 --- /dev/null +++ b/project/main/views/complex_form/views.py @@ -0,0 +1,82 @@ +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.complex_form.state_models import CheckboxState, FormState, SelectState, RouteModuleState +from project.main.views.demo_view_base import DemoViewBase + + + + +# def control_state(state: FormState) -> None: +# state.smart_routing.enabled = state.route_module.checked +# state.geo_routing.enabled = state.route_module.checked +# state.warehouse.enabled = state.route_module.checked +# state.custom_forms_in_routing.enabled = state.route_module.checked +# +# state.predictive_pickup.visible = ( +# state.smart_routing.checked and state.smart_routing.enabled +# ) +# state.automatic_planning.visible = ( +# state.smart_routing.checked and state.smart_routing.enabled +# ) +# # state.prekitting_to_box.visible = ( +# # state.use_packing_model_per_route.enabled +# # and state.use_packing_model_per_route.checked +# # ) +# # state.prekitting_to_pallet.visible = ( +# # state.use_packing_model_per_route.enabled +# # and state.use_packing_model_per_route.checked +# # ) +# # state.real_time_stock.visible = ( +# # state.use_packing_model_per_route.enabled +# # and state.use_packing_model_per_route.checked +# # ) +# state.end_warehouse_tracking.visible = state.warehouse.checked +# +# print(state) +# print("-" * 100) + + +class ComplexFormView(DemoViewBase): + template_name = "main/complex_form/container.html" + active_section = "complex-form" + title = "Complex Form" + + def get_context_data(self, **kwargs) -> dict[str, Any]: + context = super().get_context_data(**kwargs) + + state = FormState( + route_module=RouteModuleState.from_form(values={}) + ) + # control_state(state) + + context.update( + { + "form_state": state, + } + ) + return context + + def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponse: + ... + + +class ComplexFormHandleView(TemplateView): + template_name = "main/complex_form/route_module.html" + + def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponse: + print(request.POST) + state = FormState( + route_module=RouteModuleState.from_form(values=request.POST), + ) + # state = route_module_form_to_state(values=request.POST) + # control_state(state) + + return self.render_to_response( + context={ + "form_state": state, + } + )