Files
appfactory-portal/app/templates/layout.py
T
2026-06-10 07:56:02 +02:00

105 lines
3.5 KiB
Python

import html
from ..config import PORTAL_PREFIX
APP_NAME = "CSBot Services Portal"
def page(title: str, body: str, user=None) -> str:
nav = ""
user_panel = ""
if user:
username = html.escape(user.get("username", ""))
display_name = html.escape(user.get("display_name") or user.get("username", ""))
admin_links = ""
if (user.get("role") or "").lower() == "admin":
admin_links = '<a href="/portal/admin/users">Users</a>'
nav = f"""
<nav>
<a class="nav-link" href="/portal/operations">Přehled</a>
<a class="nav-link" href="/portal/apps">Služby</a>
<div class="nav-menu">
<button type="button" class="nav-menu-button">Provoz</button>
<div class="nav-menu-panel">
<a href="/portal/jobs">Úlohy</a>
<a href="/portal/scheduled-scripts">Pl&aacute;novan&eacute; skripty</a>
<a href="/portal/migration-readiness">Migration Readiness</a>
<a href="/portal/alerting/rules">Alerting</a>
<a href="/portal/deployments">Nasazení</a>
<a href="/portal/incidents">Incidenty</a>
<a href="/portal/workers">Workery</a>
</div>
</div>
<div class="nav-menu">
<button type="button" class="nav-menu-button">Správa</button>
<div class="nav-menu-panel">
{admin_links}
<a href="/portal/audit">Audit</a>
<a href="/portal/backups">Zálohy</a>
</div>
</div>
</nav>
"""
user_panel = f"""
<div class="user-menu">
<span>{display_name}</span>
<span class="muted">@{username}</span>
<form method="post" action="/portal/logout">
<button type="submit" class="btn-secondary">Odhlásit</button>
</form>
</div>
"""
return f"""
<html>
<head>
<title>{APP_NAME} - {html.escape(title)}</title>
<link rel="stylesheet" href="{PORTAL_PREFIX}/static/styles.css">
<script src="{PORTAL_PREFIX}/static/portal.js"></script>
</head>
<body>
<header>
<a class="brand" href="/portal/operations" aria-label="{APP_NAME}">
<img src="{PORTAL_PREFIX}/static/csbot-logo.svg" alt="CSBOT">
<span>{APP_NAME}</span>
</a>
{nav}
{user_panel}
</header>
<main>
{body}
</main>
</body>
</html>
"""
def render_result(title, back_url, sections, extra_link=None, extra_label=None, user=None):
rendered_sections = ""
for section_title, content in sections:
rendered_sections += f"""
<h2>{html.escape(section_title)}</h2>
<pre>{html.escape(content)}</pre>
"""
extra = ""
if extra_link and extra_label:
extra = f'<p><a class="btn" href="{extra_link}">{html.escape(extra_label)}</a></p>'
return page(
title,
f"""
<div class="card">
<h2>{html.escape(title)}</h2>
{extra}
<p><a href="{back_url}">&larr; Zpět</a></p>
</div>
<div class="card">
{rendered_sections}
</div>
""",
user=user,
)