Files
FabledSteward/steward/audit/routes.py
T
bvandeusenandClaude Opus 5.5 7a444da871 feat(audit): every row keeps its date; dividers for Today, Yesterday, Last 7 days, Older (#5214)
The operator found "Today 08:03" in place of a date odd on the audit log,
and asked for the recency groups as dividers instead. |when now always
writes the date ("Thu 8 Oct 08:03"); mark_day_groups gives each entry its
group in the reader's zone, and a divider row opens each group. A scrolled
page carries the group the last one ended in, so it is not opened twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 08:22:03 -04:00

56 lines
2.5 KiB
Python

from __future__ import annotations
import json
from quart import Blueprint, render_template, request, current_app, session
from sqlalchemy import select
from steward.ansible.inventory_gen import scope_labels
from steward.audit.service import AREAS, action_text, detail_facts, entity_labels, mark_day_groups
from steward.auth.middleware import require_role
from steward.core import formatting as fmt
from steward.core.paging import keyset, next_page
from steward.models.audit import AuditEvent
from steward.models.users import UserRole
audit_bp = Blueprint("audit", __name__, url_prefix="/audit")
@audit_bp.get("/")
@require_role(UserRole.admin)
async def list_events():
"""Who changed what, newest first, a page at a time as the reader scrolls;
?before=<cursor> is the next page's rows alone (core.paging)."""
action_filter = request.args.get("action", "").strip()
async with current_app.db_sessionmaker() as db:
stmt = select(AuditEvent)
if action_filter:
stmt = stmt.where(AuditEvent.action.like(f"{action_filter}%"))
stmt = keyset(stmt, AuditEvent.timestamp, AuditEvent.id, request.args.get("before"))
events, after = next_page((await db.execute(stmt)).scalars().all(),
lambda e: (e.timestamp, e.id))
# Parse detail_json for display
parsed = []
for e in events:
detail = {}
if e.detail_json:
try:
detail = json.loads(e.detail_json)
except (ValueError, TypeError):
detail = {"raw": e.detail_json}
parsed.append({"event": e, "detail": detail})
labels = await entity_labels(db, parsed)
scopes = await scope_labels(db, [p["detail"].get("scope") for p in parsed
if isinstance(p["detail"].get("scope"), str)])
for item in parsed:
e = item["event"]
item["entity"] = labels.get((e.entity_type, str(e.entity_id))) if e.entity_id else None
item["action"] = action_text(e.action)
item["facts"] = detail_facts(item["detail"], scopes)
last_group = mark_day_groups(parsed, fmt.zone(session.get("tz")), request.args.get("group", ""))
ctx = {"events": parsed, "after": after, "action_filter": action_filter, "areas": AREAS,
"last_group": last_group}
if request.args.get("before"):
return await render_template("audit/_rows.html", **ctx)
return await render_template("audit/list.html", **ctx)