feat: add HTML template rendering and test notification functionality
This commit is contained in:
+21
-22
@@ -17,6 +17,21 @@ def _fmt_dt(dt: datetime | None) -> str:
|
||||
return str(dt)
|
||||
|
||||
|
||||
HTML_TEMPLATE = (
|
||||
"<!doctype html><html><head><meta charset='utf-8'><title>thc-webhook admin</title>"
|
||||
"<style>body{{font-family:sans-serif;max-width:900px;margin:24px;}}textarea,input{{font-family:inherit;}}</style>"
|
||||
"</head><body>"
|
||||
"<h1>Admin: templates</h1>"
|
||||
"<p>Edits are saved to the templates JSON file on disk.</p>"
|
||||
"{content}"
|
||||
"</body></html>"
|
||||
)
|
||||
|
||||
|
||||
def get_html_template(content) -> str:
|
||||
return HTML_TEMPLATE.format(content=content)
|
||||
|
||||
|
||||
def create_app(
|
||||
*,
|
||||
get_state: Callable[[], dict],
|
||||
@@ -32,11 +47,7 @@ def create_app(
|
||||
locations = state.get("last_locations") or []
|
||||
locations_html = "".join(f"<li>{loc}</li>" for loc in locations)
|
||||
|
||||
return (
|
||||
"<!doctype html>"
|
||||
"<html><head><meta charset='utf-8'><title>thc-webhook</title>"
|
||||
"<style>body{font-family:sans-serif;max-width:900px;margin:24px;}</style>"
|
||||
"</head><body>"
|
||||
return get_html_template(
|
||||
"<h1>thc-webhook</h1>"
|
||||
"<h2>Status</h2>"
|
||||
"<ul>"
|
||||
@@ -56,7 +67,6 @@ def create_app(
|
||||
"<h2>Locations (latest)</h2>"
|
||||
f"<p>Total: {len(locations)}</p>"
|
||||
f"<ul>{locations_html}</ul>"
|
||||
"</body></html>"
|
||||
)
|
||||
|
||||
@app.get("/admin")
|
||||
@@ -79,18 +89,13 @@ def create_app(
|
||||
)
|
||||
|
||||
blocks_html = "".join(blocks)
|
||||
return (
|
||||
"<!doctype html>"
|
||||
"<html><head><meta charset='utf-8'><title>thc-webhook admin</title>"
|
||||
"<style>body{font-family:sans-serif;max-width:900px;margin:24px;}textarea,input{font-family:inherit;}</style>"
|
||||
"</head><body>"
|
||||
return get_html_template(
|
||||
"<h1>Admin: templates</h1>"
|
||||
"<p>Edits are saved to the templates JSON file on disk.</p>"
|
||||
"<form method='post'>"
|
||||
f"{blocks_html}"
|
||||
"<button type='submit'>Save</button>"
|
||||
"</form>"
|
||||
"</body></html>"
|
||||
)
|
||||
|
||||
@app.post("/admin")
|
||||
@@ -121,23 +126,17 @@ def create_app(
|
||||
|
||||
if errors:
|
||||
msg = "<br>".join(errors)
|
||||
return (
|
||||
"<!doctype html><html><body>"
|
||||
return get_html_template(
|
||||
"<h1>Admin: templates</h1>"
|
||||
f"<p style='color:#b00;'>Errors:<br>{msg}</p>"
|
||||
"<p><a href='/admin'>Back</a></p>"
|
||||
"</body></html>",
|
||||
400,
|
||||
)
|
||||
), 400
|
||||
|
||||
save_templates(templates_path, updated)
|
||||
return (
|
||||
"<!doctype html><html><body>"
|
||||
return get_html_template(
|
||||
"<h1>Admin: templates</h1>"
|
||||
"<p>Saved.</p>"
|
||||
"<p><a href='/admin'>Back</a></p>"
|
||||
"</body></html>",
|
||||
200,
|
||||
)
|
||||
), 200
|
||||
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user