"""The pages: two tabs, a cog, and six settings pages behind it. `TABS` is the top bar, Roles and Applications, and `SETTINGS` is the row of sub-tabs under the cog at `/settings/`: the setup pages first (Ranking, Searches, Companies), the system ones after (Pulls, Messages, Account). `base.html` renders the first, `settings/_tabs.html` the second, every page marks itself active by its own `path`, and adding one is one entry here plus one template. `/settings` itself is Ranking. Every page is a shell. Each is filled and driven by its own script under `static/` from the JSON routes in `web/api.py`; Pulls 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. The pages that were tabs before the settings shell (`/agents`, `/companies`, `/sources`, `/jobs`, `/schedules`, `/messages`, `/account`) answer 301 to their new place, query string kept, so a bookmark still lands. `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`. The Jobindex areas and Adzuna countries on the Searches page come from `plugins.search` for the same reason. """ from __future__ import annotations import sys from collections.abc import Callable from dataclasses import dataclass from pathlib import Path from fastapi import APIRouter, Request from fastapi.responses import HTMLResponse, RedirectResponse 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 from plugins.search import ADZUNA_COUNTRIES, JOBINDEX_AREAS # 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 top bar, in order. One list; `base.html` is its only reader. TABS: tuple[Tab, ...] = ( Tab("/", "Roles", "roles.html"), Tab("/applications", "Applications", "applications.html"), ) #: The sub-tabs under the cog, in order; `settings/_tabs.html` renders them. SETTINGS: tuple[Tab, ...] = ( Tab("/settings/ranking", "Ranking", "settings/ranking.html"), Tab("/settings/searches", "Searches", "settings/searches.html"), Tab("/settings/companies", "Companies", "settings/companies.html"), Tab("/settings/pulls", "Pulls", "settings/pulls.html"), Tab("/settings/messages", "Messages", "settings/messages.html"), Tab("/settings/account", "Account", "settings/account.html"), ) #: Where each page that used to be a tab lives now. MOVED: dict[str, str] = { "/agents": "/settings/ranking", "/companies": "/settings/companies", "/sources": "/settings/searches", "/jobs": "/settings/pulls", "/schedules": "/settings/pulls", "/messages": "/settings/messages", "/account": "/settings/account", } #: `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] AREA_OPTIONS = list(JOBINDEX_AREAS.items()) COUNTRY_OPTIONS = [(c, c.upper()) for c in sorted(ADZUNA_COUNTRIES)] 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["SETTINGS"] = SETTINGS 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("/settings", response_class=RedirectResponse) def settings() -> RedirectResponse: return RedirectResponse(SETTINGS[0].path, status_code=302) @router.get("/settings/ranking", response_class=HTMLResponse) def ranking(request: Request) -> HTMLResponse: return page(request, "settings/ranking.html") @router.get("/settings/searches", response_class=HTMLResponse) def searches(request: Request) -> HTMLResponse: return page(request, "settings/searches.html", areas=AREA_OPTIONS, countries=COUNTRY_OPTIONS) @router.get("/settings/companies", response_class=HTMLResponse) def companies(request: Request) -> HTMLResponse: return page(request, "settings/companies.html") @router.get("/settings/pulls", response_class=HTMLResponse) def pulls(request: Request) -> HTMLResponse: return page(request, "settings/pulls.html") @router.get("/settings/messages", response_class=HTMLResponse) def messages(request: Request) -> HTMLResponse: return page(request, "settings/messages.html") @router.get("/settings/account", response_class=HTMLResponse) def account(request: Request) -> HTMLResponse: return page(request, "settings/account.html") def _moved(to: str) -> Callable[[Request], RedirectResponse]: """A 301 to `to`, carrying the query string, so `/agents?x=1` keeps it.""" def redirect(request: Request) -> RedirectResponse: query = request.url.query return RedirectResponse(f"{to}?{query}" if query else to, status_code=301) return redirect for _old, _new in MOVED.items(): router.add_api_route(_old, _moved(_new), methods=["GET"], response_class=RedirectResponse)