"""HTML views for the operator request surface (#643). The form is deliberately a *preview* form. It has no initiate button, because initiating requires a confirmed POST to ``/api/v1/requests/apply`` and a stray form submission must not be able to produce one by accident. Nothing rendered here is trusted input: every interpolated value is escaped, and the page renders only values the service already produced rather than echoing a raw request body back. """ from __future__ import annotations import html import json from typing import Any from webui.layout import render_page from webui.request_service import ( REQUESTABLE_ROLES, WORK_KINDS, RequestError, RequestPreview, ) REQUESTS_PATH = "/requests" PREVIEW_API_PATH = "/api/v1/requests/preview" APPLY_API_PATH = "/api/v1/requests/apply" def _escape(text: Any) -> str: return html.escape(str(text if text is not None else ""), quote=True) REQUEST_PAGE_STYLES = """ """ def _options(values: tuple[str, ...], selected: Any) -> str: return "".join( f"" for value in values ) def _form(values: dict[str, Any] | None = None) -> str: current = dict(values or {}) number = current.get("work_number") return ( f"
" ) def _checks_block(preview: RequestPreview) -> str: rows = [] for check in preview.checks: verdict = "PASS" if check.ok else "FAIL" css = "verdict-ok" if check.ok else "verdict-fail" rows.append( "{_escape(check.name)} — {_escape(check.detail)} "
f""
"{_escape(action)}" for action in preview.prohibited_actions
)
request = preview.request
evidence = json.dumps(preview.allocator_evidence, indent=2, default=str)
return (
"{verdict} — {_escape(preview.detail)}
" "" f"Intent: {_escape(request.intent_summary)}
" f"{_checks_block(preview)}" f"Next safe action: " f"{_escape(preview.next_safe_action)}
" "Prohibited for this role: " + (prohibited or "none declared") + "
" "" "{_escape(evidence)}"
"{_escape(error.reason_code)} — " f"{_escape(error.detail)}
{field}" ) def render_requests_page( *, preview: RequestPreview | None = None, error: RequestError | None = None, submitted: dict[str, Any] | None = None, ) -> str: """Render the request form, plus a preview or rejection when one exists.""" body = ( "Submit a work request — desired role, issue or PR, and intent — " "and see whether it would be authorized before anything is reserved. " "Initiation goes through the allocator (#600/#613); this console never " "self-selects work, never approves, and never merges.
" + _form(submitted) + (_error_block(error) if error is not None else "") + (_preview_block(preview) if preview is not None else "") + f"" + REQUEST_PAGE_STYLES ) return render_page(title="Requests", body_html=body)