Enhance worker creation modal to include rota-specific choices and shift options, improving user experience and data handling.

This commit is contained in:
Ross
2025-12-19 22:03:34 +00:00
parent cf9e9ee854
commit 0f9cd52911
2 changed files with 72 additions and 18 deletions
+27 -1
View File
@@ -373,9 +373,35 @@ def worker_create(request):
# HTMX GET: return modal partial
if is_hx:
# If a rota_id query param is provided, include the rota in the
# modal context so the template can offer rota-specific choices
rota = None
rota_id = request.GET.get("rota_id") or request.POST.get("rota_id")
if rota_id:
try:
rota = get_object_or_404(RotaSchedule, pk=int(rota_id))
except Exception:
rota = None
# Prepare a JSON-encoded list of shift names for client-side use
shift_names_json = '[]'
try:
import json as _json
if rota is not None and getattr(rota, 'shifts', None):
# rota.shifts is stored as list of dicts
names = []
for s in (rota.shifts or []):
try:
names.append(s.get('name'))
except Exception:
continue
shift_names_json = _json.dumps(names)
except Exception:
shift_names_json = '[]'
modal_html = render_to_string(
"rota/partials/worker_form_modal.html",
{"form": form, "form_action": request.get_full_path()},
{"form": form, "form_action": request.get_full_path(), "rota": rota, "shift_names_json": shift_names_json},
request=request,
)
return HttpResponse(modal_html)