fix: make layout templates generic — remove app-specific nav and config
Lint / JS (eslint) (push) Successful in 10s

php/layout.php: nav is now data-driven via $navLinks array (supports
  top-level links, dropdowns, adminOnly flag); removed tinker_tickets
  hardcoded nav items; moved APP_CONFIG note to comment
python/base.html: nav driven by nav_links list from context; removed
  gandalf-specific routes (links_page, inspector, suppressions_page);
  removed APP_CONFIG.ticketWebUrl from shared script block; added
  nav_links format documentation in header comment

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-18 13:53:29 -04:00
parent f61705afb8
commit 75c57092f8
2 changed files with 73 additions and 44 deletions
+30 -15
View File
@@ -17,8 +17,14 @@
* $nonce string CSP nonce from SecurityHeadersMiddleware::getNonce() * $nonce string CSP nonce from SecurityHeadersMiddleware::getNonce()
* $currentUser array ['username', 'name', 'is_admin', 'groups'] * $currentUser array ['username', 'name', 'is_admin', 'groups']
* $pageTitle string Page <title> suffix * $pageTitle string Page <title> suffix
* $activeNav string Which nav link is active ('dashboard','tickets',etc.) * $activeNav string Which nav key is active — must match a $navLinks entry
* $config array From config/config.php * $config array From config/config.php
* $navLinks array Navigation items:
* [['href' => '/path', 'key' => 'mykey', 'label' => 'My Page'], ...]
* Nested (dropdown):
* ['label' => 'Admin', 'key' => 'admin', 'adminOnly' => true, 'children' => [
* ['href' => '/admin/users', 'label' => 'Users'],
* ]]
*/ */
// Defensive defaults // Defensive defaults
@@ -27,6 +33,7 @@ $currentUser = $currentUser ?? [];
$pageTitle = $pageTitle ?? 'Dashboard'; $pageTitle = $pageTitle ?? 'Dashboard';
$activeNav = $activeNav ?? ''; $activeNav = $activeNav ?? '';
$config = $config ?? []; $config = $config ?? [];
$navLinks = $navLinks ?? [];
$isAdmin = $currentUser['is_admin'] ?? false; $isAdmin = $currentUser['is_admin'] ?? false;
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
@@ -40,7 +47,7 @@ $isAdmin = $currentUser['is_admin'] ?? false;
<!-- Unified design system CSS --> <!-- Unified design system CSS -->
<link rel="stylesheet" href="/web_template/base.css"> <link rel="stylesheet" href="/web_template/base.css">
<!-- App-specific CSS (extends base, never overrides variables without good reason) --> <!-- App-specific CSS (extends base, never overrides variables without good reason) -->
<link rel="stylesheet" href="/assets/css/app.css?v=<?php echo $config['CSS_VERSION'] ?? '20260314'; ?>"> <link rel="stylesheet" href="/assets/css/app.css?v=<?php echo $config['CSS_VERSION'] ?? '1'; ?>">
<link rel="icon" href="/assets/images/favicon.png" type="image/png"> <link rel="icon" href="/assets/images/favicon.png" type="image/png">
</head> </head>
@@ -67,25 +74,31 @@ $isAdmin = $currentUser['is_admin'] ?? false;
</div> </div>
<nav class="lt-nav" aria-label="Main navigation"> <nav class="lt-nav" aria-label="Main navigation">
<a href="/" class="lt-nav-link <?php echo $activeNav === 'dashboard' ? 'active' : ''; ?>">Dashboard</a> <?php foreach ($navLinks as $link): ?>
<a href="/tickets" class="lt-nav-link <?php echo $activeNav === 'tickets' ? 'active' : ''; ?>">Tickets</a> <?php
$skipAdminOnly = !empty($link['adminOnly']) && !$isAdmin;
if ($skipAdminOnly) continue;
?>
<?php if ($isAdmin): ?> <?php if (!empty($link['children'])): ?>
<?php $parentActive = str_starts_with($activeNav, $link['key']); ?>
<div class="lt-nav-dropdown"> <div class="lt-nav-dropdown">
<a href="#" class="lt-nav-link <?php echo str_starts_with($activeNav, 'admin') ? 'active' : ''; ?>"> <a href="#" class="lt-nav-link <?php echo $parentActive ? 'active' : ''; ?>">
Admin <?php echo htmlspecialchars($link['label']); ?>
</a> </a>
<ul class="lt-nav-dropdown-menu"> <ul class="lt-nav-dropdown-menu">
<li><a href="/admin/templates">Templates</a></li> <?php foreach ($link['children'] as $child): ?>
<li><a href="/admin/workflow">Workflow</a></li> <li><a href="<?php echo htmlspecialchars($child['href']); ?>"><?php echo htmlspecialchars($child['label']); ?></a></li>
<li><a href="/admin/recurring-tickets">Recurring</a></li> <?php endforeach; ?>
<li><a href="/admin/custom-fields">Custom Fields</a></li>
<li><a href="/admin/user-activity">User Activity</a></li>
<li><a href="/admin/audit-log">Audit Log</a></li>
<li><a href="/admin/api-keys">API Keys</a></li>
</ul> </ul>
</div> </div>
<?php else: ?>
<a href="<?php echo htmlspecialchars($link['href']); ?>"
class="lt-nav-link <?php echo $activeNav === $link['key'] ? 'active' : ''; ?>">
<?php echo htmlspecialchars($link['label']); ?>
</a>
<?php endif; ?> <?php endif; ?>
<?php endforeach; ?>
</nav> </nav>
</div> </div>
@@ -130,6 +143,8 @@ $isAdmin = $currentUser['is_admin'] ?? false;
username: <?php echo json_encode($currentUser['username'] ?? ''); ?>, username: <?php echo json_encode($currentUser['username'] ?? ''); ?>,
isAdmin: <?php echo $isAdmin ? 'true' : 'false'; ?>, isAdmin: <?php echo $isAdmin ? 'true' : 'false'; ?>,
}; };
// App-specific config: set window.APP_CONFIG in your app's own <script> block,
// not here. This file is shared across all apps.
</script> </script>
<!-- Unified design system JS --> <!-- Unified design system JS -->
@@ -137,7 +152,7 @@ $isAdmin = $currentUser['is_admin'] ?? false;
<!-- App-specific JS (cache-busted) --> <!-- App-specific JS (cache-busted) -->
<script nonce="<?php echo $nonce; ?>" <script nonce="<?php echo $nonce; ?>"
src="/assets/js/app.js?v=<?php echo $config['JS_VERSION'] ?? '20260314'; ?>"> src="/assets/js/app.js?v=<?php echo $config['JS_VERSION'] ?? '1'; ?>">
</script> </script>
<!-- Per-page inline JS goes here in the including view, e.g.: --> <!-- Per-page inline JS goes here in the including view, e.g.: -->
+35 -21
View File
@@ -17,7 +17,8 @@
Required Flask setup (app.py): Required Flask setup (app.py):
- Pass `nonce` into every render_template() call via a context processor - Pass `nonce` into every render_template() call via a context processor
- Pass `user` dict from _get_user() helper - Pass `user` dict from _get_user() helper
- Pass `config` dict with APP_NAME, etc. - Pass `config` dict with APP_NAME, APP_SUBTITLE, etc.
- Pass `nav_links` list of dicts defining navigation
Context processor example: Context processor example:
@app.context_processor @app.context_processor
@@ -25,6 +26,16 @@
import base64, os import base64, os
nonce = base64.b64encode(os.urandom(16)).decode() nonce = base64.b64encode(os.urandom(16)).decode()
return dict(nonce=nonce, user=_get_user(), config=_config()) return dict(nonce=nonce, user=_get_user(), config=_config())
nav_links format (pass from route or context processor):
nav_links = [
{'href': url_for('index'), 'key': 'dashboard', 'label': 'Dashboard'},
{'href': url_for('settings'), 'key': 'settings', 'label': 'Settings'},
# Admin-only dropdown:
{'label': 'Admin', 'key': 'admin', 'admin_only': True, 'children': [
{'href': url_for('admin_users'), 'label': 'Users'},
]},
]
#} #}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@@ -64,24 +75,28 @@
</div> </div>
<nav class="lt-nav" aria-label="Main navigation"> <nav class="lt-nav" aria-label="Main navigation">
{# Each page sets {% block active_nav %}pagename{% endblock %} #}
{% set active = self.active_nav() | default('') %} {% set active = self.active_nav() | default('') %}
<a href="{{ url_for('index') }}" {% for link in nav_links | default([]) %}
class="lt-nav-link {% if active == 'dashboard' %}active{% endif %}"> {% if not link.get('admin_only') or 'admin' in user.groups %}
Dashboard {% if link.get('children') %}
<div class="lt-nav-dropdown">
<a href="#" class="lt-nav-link {% if active.startswith(link.key) %}active{% endif %}">
{{ link.label }} ▾
</a> </a>
<a href="{{ url_for('links_page') }}" <ul class="lt-nav-dropdown-menu">
class="lt-nav-link {% if active == 'links' %}active{% endif %}"> {% for child in link.children %}
Link Debug <li><a href="{{ child.href }}">{{ child.label }}</a></li>
</a> {% endfor %}
<a href="{{ url_for('inspector') }}" </ul>
class="lt-nav-link {% if active == 'inspector' %}active{% endif %}"> </div>
Inspector {% else %}
</a> <a href="{{ link.href }}"
<a href="{{ url_for('suppressions_page') }}" class="lt-nav-link {% if active == link.key %}active{% endif %}">
class="lt-nav-link {% if active == 'suppressions' %}active{% endif %}"> {{ link.label }}
Suppressions
</a> </a>
{% endif %}
{% endif %}
{% endfor %}
</nav> </nav>
</div> </div>
@@ -107,17 +122,16 @@
All <script> tags MUST carry the nonce attribute for CSP. All <script> tags MUST carry the nonce attribute for CSP.
========================================================= --> ========================================================= -->
<!-- Runtime config (no CSRF needed for Gandalf — SameSite=Strict) --> <!-- Runtime config injected by the server -->
<script nonce="{{ nonce }}"> <script nonce="{{ nonce }}">
window.APP_CONFIG = {
ticketWebUrl: {{ config.get('ticket_api', {}).get('web_url', 'https://t.lotusguild.org/ticket/') | tojson }},
};
window.CURRENT_USER = { window.CURRENT_USER = {
username: {{ user.username | tojson }}, username: {{ user.username | tojson }},
name: {{ (user.name or user.username) | tojson }}, name: {{ (user.name or user.username) | tojson }},
groups: {{ user.groups | tojson }}, groups: {{ user.groups | tojson }},
isAdmin: {{ ('admin' in user.groups) | lower }}, isAdmin: {{ ('admin' in user.groups) | lower }},
}; };
// App-specific config: set window.APP_CONFIG in your app's own template block,
// not here. This file is shared across all apps.
</script> </script>
<!-- Unified design system JS --> <!-- Unified design system JS -->
@@ -136,6 +150,6 @@
{% block active_nav %}dashboard{% endblock %} {% block active_nav %}dashboard{% endblock %}
Values: dashboard | links | inspector | suppressions Value must match a 'key' in your nav_links list.
--------------------------------------------------------------- #} --------------------------------------------------------------- #}
{% block active_nav %}{% endblock %} {% block active_nav %}{% endblock %}