"""The tabs: eight pages, declared once. `TABS` is the whole navigation. `base.html` renders it, every page marks itself active by its own `path`, and adding a tab is one entry here plus one template. Every page is a shell. Roles, Applications, Agents and Companies are filled and driven by `static/roles.js` from the JSON routes in `web/api.py`; Jobs, Schedules and Account are shells around the modules' own elements. What the server renders is only what a select needs to offer, so the options cannot drift from what the store accepts. `FUNCTIONS` is `plugins.ats`'s, not `app`'s: the nine job functions are the classifier's vocabulary, and this reaches `plugins/` the same way `app.cli` does, since a plugin is app-agnostic and never reaches back into `app`. """ from __future__ import annotations import sys from dataclasses import dataclass from pathlib import Path from fastapi import APIRouter, Request from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates from app.models import REMOTE, STATUSES _ROOT = Path(__file__).resolve().parents[2] if str(_ROOT) not in sys.path: sys.path.insert(0, str(_ROOT)) from plugins.ats import FUNCTIONS # noqa: E402 TEMPLATE_DIR = Path(__file__).resolve().parent.parent / "templates" STATIC_DIR = Path(__file__).resolve().parent.parent / "static" @dataclass(frozen=True, slots=True) class Tab: path: str label: str template: str #: The navigation, in order. One list; `base.html` is its only reader. TABS: tuple[Tab, ...] = ( Tab("/", "Roles", "roles.html"), Tab("/applications", "Applications", "applications.html"), Tab("/agents", "Agents", "agents.html"), Tab("/companies", "Companies", "companies.html"), Tab("/jobs", "Jobs", "jobs.html"), Tab("/schedules", "Schedules", "schedules.html"), Tab("/messages", "Messages", "messages.html"), Tab("/account", "Account", "account.html"), ) #: `value, label` for each select option. `FUNCTIONS` is already the value a #: role's `function` column holds; only the label is dressed up. FUNCTION_OPTIONS = [(f, f.replace("/", " / ").capitalize()) for f in FUNCTIONS] REMOTE_OPTIONS = [(r, r.capitalize()) for r in REMOTE if r] STATUS_OPTIONS = [(s, s.capitalize()) for s in STATUSES] def asset(path: str) -> str: """A static URL stamped with the file's modification time, so a changed file is a different URL and a cached stale copy is never the answer.""" name = path.lstrip("/") try: stamp = int((STATIC_DIR / name).stat().st_mtime) except OSError: return f"/static/{name}" return f"/static/{name}?v={stamp}" templates = Jinja2Templates(directory=str(TEMPLATE_DIR)) templates.env.globals["TABS"] = TABS templates.env.globals["asset"] = asset #: Out of the OpenAPI schema: these are pages. router = APIRouter(include_in_schema=False) def page(request: Request, name: str, **context: object) -> HTMLResponse: return templates.TemplateResponse(request, name, context) @router.get("/", response_class=HTMLResponse) def roles(request: Request) -> HTMLResponse: return page(request, "roles.html", functions=FUNCTION_OPTIONS, remote_options=REMOTE_OPTIONS) @router.get("/applications", response_class=HTMLResponse) def applications(request: Request) -> HTMLResponse: return page(request, "applications.html", status_options=STATUS_OPTIONS) @router.get("/agents", response_class=HTMLResponse) def agents(request: Request) -> HTMLResponse: return page(request, "agents.html") @router.get("/companies", response_class=HTMLResponse) def companies(request: Request) -> HTMLResponse: return page(request, "companies.html") @router.get("/jobs", response_class=HTMLResponse) def jobs(request: Request) -> HTMLResponse: return page(request, "jobs.html") @router.get("/schedules", response_class=HTMLResponse) def schedules(request: Request) -> HTMLResponse: return page(request, "schedules.html") @router.get("/messages", response_class=HTMLResponse) def messages(request: Request) -> HTMLResponse: return page(request, "messages.html") @router.get("/account", response_class=HTMLResponse) def account(request: Request) -> HTMLResponse: return page(request, "account.html")