Files
proc-rota/djangorota/rota/views.py
T
Ross e6943ccfc7 .
2025-12-09 12:16:59 +00:00

401 lines
13 KiB
Python

from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse
from .models import RotaSchedule, Worker
from .forms import WorkerForm, LeaveForm, RotaScheduleForm
from .services import run_rota_async
from .forms import ShiftForm
from django.views.decorators.http import require_http_methods
from django.template.loader import render_to_string
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.http import require_POST
def index(request):
rotas = RotaSchedule.objects.all().order_by("-created_at")
return render(request, "rota/index.html", {"rotas": rotas})
def rota_detail(request, rota_id):
rota = get_object_or_404(RotaSchedule, pk=rota_id)
if request.method == "POST":
# trigger a background run
generate_builder = request.POST.get("generate_builder") == "1"
builder_solve = request.POST.get("builder_solve") == "1"
run = run_rota_async(rota.id, generate_builder=generate_builder, builder_solve=builder_solve)
return redirect(reverse("rota:rota_run_detail", args=(run.id,)))
return render(request, "rota/rota_detail.html", {"rota": rota})
def rota_create(request):
if request.method == "POST":
form = RotaScheduleForm(request.POST)
if form.is_valid():
rota = form.save()
return redirect("rota:rota_detail", rota_id=rota.id)
else:
form = RotaScheduleForm()
return render(request, "rota/rota_form.html", {"form": form})
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})
def worker_detail(request, worker_id):
worker = get_object_or_404(Worker, pk=worker_id)
if request.method == "POST":
form = LeaveForm(request.POST)
if form.is_valid():
leave = form.save(commit=False)
leave.worker = worker
leave.save()
return redirect("rota:worker_detail", worker_id=worker.id)
else:
form = LeaveForm()
return render(request, "rota/worker_detail.html", {"worker": worker, "form": form})
@require_http_methods(["GET", "POST"])
def worker_edit(request, rota_id, worker_id):
rota = get_object_or_404(RotaSchedule, pk=rota_id)
worker = get_object_or_404(Worker, pk=worker_id)
if request.method == "POST":
form = WorkerForm(request.POST, instance=worker)
if form.is_valid():
form.save()
# Return updated worker list OOB and close modal
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
# invalid: return form modal with errors
modal_html = render_to_string(
"rota/partials/worker_form_modal.html",
{"form": form, "form_action": request.get_full_path(), "rota": rota},
request=request,
)
return HttpResponse(modal_html)
# GET: populate form and return modal
form = WorkerForm(instance=worker)
modal_html = render_to_string(
"rota/partials/worker_form_modal.html",
{"form": form, "form_action": request.path, "rota": rota},
request=request,
)
return HttpResponse(modal_html)
@require_http_methods(["GET", "POST"])
def worker_delete(request, rota_id, worker_id):
rota = get_object_or_404(RotaSchedule, pk=rota_id)
worker = get_object_or_404(Worker, pk=worker_id)
if request.method == "POST":
# remove assignment between worker and rota
try:
from .models import Assignment
Assignment.objects.filter(worker=worker, rota=rota).delete()
except Exception:
try:
rota.workers.remove(worker)
except Exception:
return HttpResponseBadRequest("Unable to remove worker from rota")
rota = get_object_or_404(RotaSchedule, pk=rota_id)
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
# GET: confirmation modal
modal_html = render_to_string(
"rota/partials/worker_confirm_delete.html",
{"rota": rota, "worker": worker},
request=request,
)
return HttpResponse(modal_html)
def rota_run_detail(request, run_id):
from .models import RotaRun
run = get_object_or_404(RotaRun, pk=run_id)
return render(request, "rota/rota_run_detail.html", {"run": run})
def rota_run_export_html(request, run_id):
from .models import RotaRun
run = get_object_or_404(RotaRun, pk=run_id)
return render(request, "rota/rota_run_export.html", {"run": run})
def rota_run_export_download(request, run_id):
from .models import RotaRun
run = get_object_or_404(RotaRun, pk=run_id)
html = render_to_string("rota/rota_run_export.html", {"run": run}, request=request)
response = HttpResponse(html, content_type="text/html; charset=utf-8")
filename = f"rota_run_{run.id}_export.html"
response["Content-Disposition"] = f'attachment; filename="{filename}"'
return response
def rota_export_builder(request, rota_id):
"""Build a `RotaBuilder` for the given `RotaSchedule` and return its HTML export.
Query parameters:
- solve=1 : attempt to solve the model (may require solver binaries and can be slow)
- solver=<name> : solver name passed to `build_and_solve`
"""
rota = get_object_or_404(RotaSchedule, pk=rota_id)
try:
rb = rota.to_rota_builder()
except Exception as exc:
return HttpResponseBadRequest(f"Unable to construct RotaBuilder: {exc}")
solve = request.GET.get("solve") == "1"
solver = request.GET.get("solver", "appsi_highs")
try:
# Build model (and optionally solve) - keep defaults safe
rb.build_and_solve(solve=solve, solver=solver)
except Exception as exc:
# Return error message in the page so it's visible in browser
content = f"<html><body><h1>Error running builder</h1><pre>{exc}</pre></body></html>"
return HttpResponse(content, status=500, content_type="text/html")
html = rb.get_worker_timetable_html(include_html_tag=True)
return HttpResponse(html, content_type="text/html; charset=utf-8")
@require_POST
def rota_runs_clear(request, rota_id):
"""Delete all RotaRun records for the given rota and redirect back.
This is a POST-only endpoint; the template uses an inline form with a
JavaScript confirmation to avoid accidental removal.
"""
rota = get_object_or_404(RotaSchedule, pk=rota_id)
# Delete all runs for this rota
from .models import RotaRun
RotaRun.objects.filter(rota=rota).delete()
return redirect("rota:rota_detail", rota_id=rota.id)
@require_http_methods(["GET", "POST"])
def shift_add(request, rota_id):
"""HTMX endpoint to render a shift form or accept submission to append a shift to the rota."""
rota = get_object_or_404(RotaSchedule, pk=rota_id)
if request.method == "POST":
form = ShiftForm(request.POST)
if form.is_valid():
data = form.cleaned_data
shift_dict = {
"sites": data["sites"],
"name": data["name"],
"length": float(data["length"]),
"days": data["days"],
"workers_required": int(data["workers_required"]),
"assign_as_block": bool(data["assign_as_block"]),
}
current = rota.shifts or []
current.append(shift_dict)
rota.shifts = current
rota.save()
# Return out-of-band swap: update shift-list and clear modal
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
# wrap shift list for OOB swap and clear form/modal containers
resp = (
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
+ '<div id="shift-form" hx-swap-oob="true"></div>'
+ '<div id="modal" hx-swap-oob="true"></div>'
)
response = HttpResponse(resp)
response["HX-Trigger"] = "closeModal"
return response
# invalid: return modal with form and errors
modal_html = render_to_string("rota/partials/shift_form_modal.html", {"form": form, "rota": rota, "form_action": request.path}, request=request)
return HttpResponse(modal_html)
else:
form = ShiftForm()
modal_html = render_to_string("rota/partials/shift_form_modal.html", {"form": form, "rota": rota, "form_action": request.path}, request=request)
return HttpResponse(modal_html)
@require_http_methods(["GET", "POST"])
def shift_edit(request, rota_id, idx):
rota = get_object_or_404(RotaSchedule, pk=rota_id)
try:
shift = rota.shifts[idx]
except Exception:
return HttpResponseBadRequest("Shift not found")
if request.method == "POST":
form = ShiftForm(request.POST)
if form.is_valid():
data = form.cleaned_data
shift_dict = {
"sites": data["sites"],
"name": data["name"],
"length": float(data["length"]),
"days": data["days"],
"workers_required": int(data["workers_required"]),
"assign_as_block": bool(data["assign_as_block"]),
}
current = rota.shifts or []
current[idx] = shift_dict
rota.shifts = current
rota.save()
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
resp = (
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
+ '<div id="shift-form" hx-swap-oob="true"></div>'
+ '<div id="modal" hx-swap-oob="true"></div>'
)
response = HttpResponse(resp)
response["HX-Trigger"] = "closeModal"
return response
modal_html = render_to_string("rota/partials/shift_form_modal.html", {"form": form, "rota": rota, "form_action": request.path}, request=request)
return HttpResponse(modal_html)
# GET: populate form with existing shift
initial = {
"name": shift.get("name"),
"sites": ",".join(shift.get("sites") or []),
"length": shift.get("length"),
"days": shift.get("days"),
"workers_required": shift.get("workers_required"),
"assign_as_block": shift.get("assign_as_block", False),
}
form = ShiftForm(initial=initial)
modal_html = render_to_string("rota/partials/shift_form_modal.html", {"form": form, "rota": rota, "form_action": request.path}, request=request)
return HttpResponse(modal_html)
@require_http_methods(["GET", "POST"])
def shift_delete(request, rota_id, idx):
rota = get_object_or_404(RotaSchedule, pk=rota_id)
try:
shift = rota.shifts[idx]
except Exception:
return HttpResponseBadRequest("Shift not found")
if request.method == "POST":
current = rota.shifts or []
try:
current.pop(idx)
except Exception:
return HttpResponseBadRequest("Invalid index")
rota.shifts = current
rota.save()
shift_list_html = render_to_string("rota/partials/shift_list.html", {"rota": rota}, request=request)
resp = (
f'<div id="shift-list" hx-swap-oob="true">{shift_list_html}</div>'
+ '<div id="shift-form" hx-swap-oob="true"></div>'
+ '<div id="modal" hx-swap-oob="true"></div>'
)
response = HttpResponse(resp)
response["HX-Trigger"] = "closeModal"
return response
# GET: confirmation modal
modal_html = render_to_string("rota/partials/shift_confirm_delete.html", {"rota": rota, "idx": idx, "shift": shift}, request=request)
return HttpResponse(modal_html)