Complex form

This commit is contained in:
Eden Kirin
2024-04-06 19:42:32 +02:00
parent 66edabab04
commit ad14f2fe12
7 changed files with 186 additions and 7 deletions

View File

@ -2,7 +2,13 @@
{% block content %}
<p>
This is some complex form content bellow.
</p>
<form
hx-post="{{ url("complex-form-handle") }}"
hx-trigger="change"
hx-target="#complex-form-content"
>
<div id="complex-form-content">
{% include "main/complex_form_content.html" %}
</div>
</form>
{% endblock %}

View File

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

View File

@ -0,0 +1,19 @@
{% macro checkbox(title, name, checked, state, cls="") %}
{% if state.visible %}
{% set id = random_id() %}
<div class="form-check {{ cls }}">
<input
class="form-check-input"
type="checkbox"
id="{{ id }}"
name="{{ name }}"
{% if state.checked %}checked{% endif %}
{% if not state.enabled %}disabled{% endif %}
>
<label class="form-check-label" for="{{ id }}">
{{ title }}
</label>
</div>
{% endif %}
{% endmacro %}

View File

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

View File

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