diff --git a/devplacepy/admin_sections.py b/devplacepy/admin_sections.py new file mode 100644 index 00000000..126d91a1 --- /dev/null +++ b/devplacepy/admin_sections.py @@ -0,0 +1,38 @@ +# retoor + +from dataclasses import dataclass + + +@dataclass(frozen=True) +class AdminSection: + key: str + label: str + href: str + icon: str + + +ADMIN_SECTIONS: tuple[AdminSection, ...] = ( + AdminSection("users", "Users", "/admin/users", "\U0001f465"), + AdminSection("news", "News", "/admin/news", "\U0001f4f0"), + AdminSection("media", "Media", "/admin/media", "\U0001f5bc️"), + AdminSection("moderation", "Moderation", "/admin/moderation", "\U0001f6a9"), + AdminSection("trash", "Trash", "/admin/trash", "\U0001f5d1️"), + AdminSection("services", "Services", "/admin/services", "⚙️"), + AdminSection("gateway", "Gateway", "/admin/gateway", "\U0001f500"), + AdminSection("containers", "Containers", "/admin/containers", "\U0001f4e6"), + AdminSection("workspaces", "Workspaces", "/admin/workspaces", "\U0001f4bb"), + AdminSection("devii-tasks", "Devii tasks", "/admin/devii-tasks", "⏰"), + AdminSection("communities", "Communities", "/admin/communities", "\U0001f310"), + AdminSection("nodes", "Community nodes", "/admin/nodes", "\U0001f4e1"), + AdminSection("bots", "Bot Monitor", "/admin/bots", "\U0001f916"), + AdminSection("game", "Code Farm", "/admin/game", "\U0001f69c"), + AdminSection("ai-usage", "AI usage", "/admin/ai-usage", "\U0001f4ca"), + AdminSection("statistics", "Statistics", "/admin/statistics", "\U0001f4c8"), + AdminSection("audit-log", "Audit Log", "/admin/audit-log", "\U0001f4cb"), + AdminSection("backups", "Backups", "/admin/backups", "\U0001f4be"), + AdminSection("issues", "Ticket Planning", "/admin/issues/planning", "\U0001f5c2️"), + AdminSection("notifications", "Notifications", "/admin/notifications", "\U0001f514"), + AdminSection("settings", "Settings", "/admin/settings", "\U0001f527"), +) + +ADMIN_SECTION_KEYS: frozenset[str] = frozenset(section.key for section in ADMIN_SECTIONS) diff --git a/devplacepy/database/nodes.py b/devplacepy/database/nodes.py index c2ae1780..8b9c1a89 100644 --- a/devplacepy/database/nodes.py +++ b/devplacepy/database/nodes.py @@ -592,14 +592,23 @@ def raise_flag(kind: str, *, device_uid: str = "", user_uid: str = "", detail: s return table.find_one(uid=uid) -def list_flags(status: str = FLAG_OPEN) -> list[dict]: +def list_flags(status: str = FLAG_OPEN, limit: int | None = None) -> list[dict]: + window = {} if limit is None else {"_limit": limit} return list( get_table("node_flags").find( - status=status, deleted_at=None, order_by=["-created_at"] + status=status, deleted_at=None, order_by=["-created_at"], **window ) ) +def count_flags(status: str = FLAG_OPEN) -> int: + return get_table("node_flags").count(status=status, deleted_at=None) + + +def count_all_devices() -> int: + return get_table("node_devices").count(deleted_at=None) + + def resolve_flag(flag_uid: str, actor_uid: str) -> bool: return ( _execute( diff --git a/devplacepy/responses.py b/devplacepy/responses.py index 6037b455..809e68d6 100644 --- a/devplacepy/responses.py +++ b/devplacepy/responses.py @@ -19,7 +19,14 @@ def wants_json(request: Request) -> bool: return "application/json" in accept and "text/html" not in accept -def respond(request: Request, template: str, context: dict, *, model: type[BaseModel]): +def respond( + request: Request, + template: str, + context: dict, + *, + model: type[BaseModel], + status_code: int = 200, +): if wants_json(request): try: payload = model.model_validate(context).model_dump(mode="json") @@ -29,7 +36,9 @@ def respond(request: Request, template: str, context: dict, *, model: type[BaseM return JSONResponse(payload) from devplacepy.templating import templates - return templates.TemplateResponse(request, template, context) + return templates.TemplateResponse( + request, template, context, status_code=status_code + ) def action_result( diff --git a/devplacepy/routers/CLAUDE.md b/devplacepy/routers/CLAUDE.md index 5f2146c7..e2cb4a67 100644 --- a/devplacepy/routers/CLAUDE.md +++ b/devplacepy/routers/CLAUDE.md @@ -179,6 +179,10 @@ All dates displayed to users use European DD/MM/YYYY format. Implemented via: - **`time_ago()`** returns `DD/MM/YYYY` for items older than 30 days (instead of `"Xmo ago"`) - Services page has a JS `formatDate()` function for live polling updates +### Admin page specification + +Every admin route and template conforms to `devplacepy/routers/admin/CLAUDE.md` (A1-A11). Read it before adding or changing anything under `/admin`; it is enforced by `tests/unit/admin_spec.py`. + ### Admin pagination Both `/admin/users` and `/admin/news` use offset-based pagination via a reusable component: diff --git a/devplacepy/routers/admin/CLAUDE.md b/devplacepy/routers/admin/CLAUDE.md new file mode 100644 index 00000000..97c21b7e --- /dev/null +++ b/devplacepy/routers/admin/CLAUDE.md @@ -0,0 +1,82 @@ +# CLAUDE.md + +The specification every `/admin` page follows. Claude Code loads it whenever a file under `routers/admin/` is touched; the root, `routers/`, `templates/`, `static/js/` and `static/css/` `CLAUDE.md` files point here. A page that breaks a rule below is a defect, not a style choice. `tests/unit/admin_spec.py` enforces the mechanical rules (A1, A2, A5, A6, A7, A9, A10) against the real templates and routers, so a violation fails the suite. + +Code references: the section registry is `devplacepy/admin_sections.py`; the caps and floors are `ADMIN_LIST_CAP` and `ADMIN_POLL_FLOOR_SECONDS` in `routers/admin/_shared.py`; the cap note is `templates/_admin_cap_note.html`. + +## A1. Backend rendered, always + +A GET of an admin page returns the complete content in the HTML: every table, card, key figure, tab label and empty state is rendered by Jinja from data the handler computed. A placeholder such as "Loading..." in a template is forbidden. The page must be readable and correct with JavaScript off. + +JavaScript only enhances: live refresh, charts drawn on a ``, sorting, modals. The numbers and tables around a chart are server rendered; the chart is drawn from JSON embedded in the page. + +The only permitted live-first surface is a page whose whole purpose is a live channel (WebSocket) such as the Bot Monitor and the container terminal, and even those render their initial frame on the server. + +## A2. One handler, one builder, one model + +- The HTML route calls `respond(request, "