Complex form

This commit is contained in:
Eden Kirin
2024-04-06 22:04:57 +02:00
parent ad14f2fe12
commit 5e1989a60f
9 changed files with 211 additions and 107 deletions

View File

@ -1,6 +1,5 @@
{% extends "main/base/layout.html" %} {% extends "main/base/layout.html" %}
{% block content %} {% block content %}
<form <form
hx-post="{{ url("complex-form-handle") }}" hx-post="{{ url("complex-form-handle") }}"
@ -8,7 +7,7 @@
hx-target="#complex-form-content" hx-target="#complex-form-content"
> >
<div id="complex-form-content"> <div id="complex-form-content">
{% include "main/complex_form_content.html" %} {% include "main/complex_form/route_module.html" %}
</div> </div>
</form> </form>
{% endblock %} {% endblock %}

View File

@ -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_1 = "ms-4" %}
{% set indent_2 = "ms-5" %} {% set indent_2 = "ms-5" %}
{% set state = form_state.route_module %}
{{ checkbox( {{ checkbox(
title="Route module", title="Route module",
name="route_module", name="route_module",
state=state.route_module state=state.enabled
) }} ) }}
{{ checkbox( {{ checkbox(
title="Smart routing", title="Smart routing",
@ -14,18 +16,38 @@
state=state.smart_routing, state=state.smart_routing,
cls=indent_1 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( {{ checkbox(
title="Geo routing", title="Geo routing",
name="geo_routing", name="geo_routing",
state=state.geo_routing, state=state.geo_routing,
cls=indent_1 cls=indent_1
) }} ) }}
{{ checkbox(
title="Use packing model per route", {{ select(
name="use_packing_model_per_route", title="Packing model",
state=state.use_packing_model_per_route, 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 cls=indent_1
) }} ) }}
{{ checkbox( {{ checkbox(
title="Prekitting to box", title="Prekitting to box",
name="prekitting_to_box", name="prekitting_to_box",
@ -44,18 +66,18 @@
state=state.real_time_stock, state=state.real_time_stock,
cls=indent_2 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( {{ checkbox(
title="Warehouse", title="Warehouse",
name="warehouse", name="warehouse",
state=state.warehouse, state=state.warehouse,
cls=indent_1 cls=indent_1
) }} ) }}
{{ checkbox(
title="End warehouse tracking",
name="end_warehouse_tracking",
state=state.end_warehouse_tracking,
cls=indent_2
) }}
{{ checkbox( {{ checkbox(
title="Custom forms in routing", title="Custom forms in routing",
name="custom_forms_in_routing", name="custom_forms_in_routing",

View File

@ -1,4 +1,4 @@
{% macro checkbox(title, name, checked, state, cls="") %} {% macro checkbox(title, name, state, cls="") %}
{% if state.visible %} {% if state.visible %}
{% set id = random_id() %} {% set id = random_id() %}
@ -8,7 +8,7 @@
type="checkbox" type="checkbox"
id="{{ id }}" id="{{ id }}"
name="{{ name }}" name="{{ name }}"
{% if state.checked %}checked{% endif %} {% if state.checked and state.enabled %}checked {% endif %}
{% if not state.enabled %}disabled {% endif %} {% if not state.enabled %}disabled {% endif %}
> >
<label class="form-check-label" for="{{ id }}"> <label class="form-check-label" for="{{ id }}">
@ -17,3 +17,25 @@
</div> </div>
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
{% macro select(title, name, options, state, cls="") %}
{% if state.visible %}
<div class="{{ cls }}">
<select
name="{{ name }}"
class="form-select form-select-sm"
>
{% for value, title in options.items() %}
<option
value="{{ value }}"
{% if value == state.value %}selected {% endif %}
{% if not state.enabled %}disabled {% endif %}
>
{{ title }}
</option>
{% endfor %}
</select>
</div>
{% endif %}
{% endmacro %}

View File

@ -1,4 +1,4 @@
from .complex_form import ComplexFormHandleView, ComplexFormView from .complex_form.views import ComplexFormHandleView, ComplexFormView
from .filter_list import FilterListView from .filter_list import FilterListView
from .form_validation import FormValidationView from .form_validation import FormValidationView
from .home import HomeView from .home import HomeView

View File

@ -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,
}
)

View File

@ -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

View File

@ -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,
}
)