Files
jobboard/app/web/pages.py
T
WolfandClaude Fable 5.1 a9290a6926 Roles is newsdesk's feed: rail, feed and reader, with read state and tags
The Roles tab takes newsdesk's shell, ported from its index.html and
app.css rather than rewritten: a filter rail (Show read, Best first or
Newest first, first seen, tag chips with counts, the old filters behind a
disclosure), the feed in newsdesk's row layout (score gutter, title,
company, place, badges, age, tags, the summary as excerpt, judge buttons)
and the reader as a third column that replaces the list on a phone. The
router pushes /roles/{uid}, which the server also renders with the reader
open, so the separate read page and role.js go. j/k, u/n/d, S, Enter,
Escape, o, /, f and c work as in newsdesk. The phone rules come with their
comments.

Two things the row lacked: roles.read_at, stamped when the reader opens a
role or a vote is cast, behind include_read on /api/roles; and role_tags,
written by summarise alongside the summary and served with counts by
/api/filters, filtered in SQL before LIMIT. Migration 5.

PyCharm run configurations in .run/ for serve, pull, seed and pytest.
core.autocrlf=input is set locally so a Windows-side checkout commits LF.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-28 12:37:45 +02:00

193 lines
7.3 KiB
Python

"""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/<name>`: 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. `/roles/{uid}` is the Roles page again,
with that role open in its reader: one page, so a link to a role and the
role opened from the feed are the same screen. 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, HTTPException, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from app.models import REMOTE, STATUSES
from app.web.deps import StoreDep
_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, role=None)
@router.get("/roles/{uid}", response_class=HTMLResponse)
def role(request: Request, uid: str, store: StoreDep) -> HTMLResponse:
"""The Roles page with one role open in its reader, which is where
opening a role from the feed puts the address bar. `roles.js` reads the
uid from the path; the lookup here makes an unknown uid a 404 page rather
than an empty reader, and gives the tab the role's title. `active_path`
keeps Roles lit in the top bar."""
found = store.role(uid)
if found is None:
raise HTTPException(404, f"no such role: {uid}")
return page(request, "roles.html", functions=FUNCTION_OPTIONS,
remote_options=REMOTE_OPTIONS, role=found, active_path="/")
@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)