.
This commit is contained in:
@@ -38,14 +38,76 @@ def rota_create(request):
|
||||
|
||||
|
||||
def worker_create(request):
|
||||
# Support both full-page and HTMX modal flows. If the request comes from
|
||||
# HTMX (header `HX-Request: true`) we return the modal partial so it can
|
||||
# be injected into the `#modal` container. On successful HTMX POST we
|
||||
# return an out-of-band swap to refresh the worker list and clear the
|
||||
# modal.
|
||||
is_hx = request.headers.get("HX-Request", "false").lower() == "true"
|
||||
|
||||
if request.method == "POST":
|
||||
form = WorkerForm(request.POST)
|
||||
if form.is_valid():
|
||||
worker = form.save()
|
||||
if is_hx:
|
||||
# Return updated worker list and close modal
|
||||
# If a rota context was supplied via querystring, we do not need it
|
||||
# here because the partial will re-render from DB.
|
||||
# Render worker list OOB and clear modal container
|
||||
# We need a rota to render the list — try to use `rota_id` query param
|
||||
# rota_id may be provided as a querystring or as a hidden form field.
|
||||
rota_id = request.GET.get("rota_id") or request.POST.get("rota_id")
|
||||
rota = None
|
||||
if rota_id:
|
||||
try:
|
||||
rota = get_object_or_404(RotaSchedule, pk=int(rota_id))
|
||||
except Exception:
|
||||
rota = None
|
||||
|
||||
# If we created a worker and a rota_id was provided, create Assignment
|
||||
if rota is not None:
|
||||
from .models import Assignment
|
||||
try:
|
||||
Assignment.objects.create(worker=worker, rota=rota)
|
||||
except Exception:
|
||||
# best-effort: try using the M2M add as fallback
|
||||
try:
|
||||
rota.workers.add(worker)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if rota is not None:
|
||||
worker_list_html = render_to_string(
|
||||
"rota/partials/worker_list.html",
|
||||
{"rota": rota},
|
||||
request=request,
|
||||
)
|
||||
resp = (
|
||||
f'<div id="worker-list" hx-swap-oob="true">{worker_list_html}</div>'
|
||||
+ '<div id="modal" hx-swap-oob="true"></div>'
|
||||
)
|
||||
response = HttpResponse(resp)
|
||||
response["HX-Trigger"] = "closeModal"
|
||||
return response
|
||||
# If we couldn't render the worker list (no rota), just close modal
|
||||
response = HttpResponse('<div id="modal" hx-swap-oob="true"></div>')
|
||||
response["HX-Trigger"] = "closeModal"
|
||||
return response
|
||||
# Non-HTMX POST -> redirect
|
||||
return redirect("/")
|
||||
else:
|
||||
form = WorkerForm()
|
||||
|
||||
# HTMX GET: return modal partial
|
||||
if is_hx:
|
||||
modal_html = render_to_string(
|
||||
"rota/partials/worker_form_modal.html",
|
||||
{"form": form, "form_action": request.get_full_path()},
|
||||
request=request,
|
||||
)
|
||||
return HttpResponse(modal_html)
|
||||
|
||||
# Non-HTMX GET: full page
|
||||
return render(request, "rota/worker_form.html", {"form": form})
|
||||
|
||||
|
||||
@@ -86,7 +148,7 @@ def worker_edit(request, rota_id, worker_id):
|
||||
# invalid: return form modal with errors
|
||||
modal_html = render_to_string(
|
||||
"rota/partials/worker_form_modal.html",
|
||||
{"form": form, "form_action": request.path, "rota": rota},
|
||||
{"form": form, "form_action": request.get_full_path(), "rota": rota},
|
||||
request=request,
|
||||
)
|
||||
return HttpResponse(modal_html)
|
||||
|
||||
Reference in New Issue
Block a user