This commit is contained in:
Ross
2025-12-09 12:36:18 +00:00
parent e6943ccfc7
commit 40135a282d
6 changed files with 92 additions and 8 deletions
+40 -1
View File
@@ -9,6 +9,7 @@ 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
from django.shortcuts import HttpResponseRedirect
def index(request):
@@ -213,6 +214,11 @@ def rota_run_export_html(request, run_id):
from .models import RotaRun
run = get_object_or_404(RotaRun, pk=run_id)
# If the export HTML was persisted with the run, return it directly.
if getattr(run, "export_html", None):
return HttpResponse(run.export_html, content_type="text/html; charset=utf-8")
# Fallback: render the export template which shows run.result.
return render(request, "rota/rota_run_export.html", {"run": run})
@@ -220,13 +226,46 @@ 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)
# Prefer persisted export HTML if available
if getattr(run, "export_html", None):
html = run.export_html
else:
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
@require_POST
def rota_run_export_regenerate(request, run_id):
"""Regenerate the HTML export for a completed run and persist it on the RotaRun.
This is a POST endpoint — regenerating may be slow if the solver is invoked.
"""
from .models import RotaRun
run = get_object_or_404(RotaRun, pk=run_id)
rota = run.rota
solve = request.POST.get("solve") == "1"
try:
rb = rota.to_rota_builder()
rb.build_and_solve(solve=solve)
html = rb.get_worker_timetable_html(include_html_tag=True)
run.export_html = html
run.save(update_fields=["export_html"])
except Exception as exc:
tb = traceback.format_exc()
run.log = (run.log or "") + "\n" + tb
run.save(update_fields=["log"])
return HttpResponse(f"Failed to regenerate export: {exc}", status=500)
# Redirect to the HTML view so user sees the generated page
return HttpResponseRedirect(reverse("rota:rota_run_export_html", args=(run.id,)))
def rota_export_builder(request, rota_id):
"""Build a `RotaBuilder` for the given `RotaSchedule` and return its HTML export.