feat(админка): обратимые действия и undo

This commit is contained in:
anor
2026-11-06 21:51:19 +10:00
parent d56b3d2fff
commit e15369c4d4
6 changed files with 265 additions and 0 deletions
+37
View File
@@ -269,6 +269,11 @@ def init_db():
conn.execute("CREATE TABLE IF NOT EXISTS attention_state ("
"key TEXT PRIMARY KEY, status TEXT NOT NULL, "
"snooze_until REAL, updated_at REAL NOT NULL)")
conn.execute("CREATE TABLE IF NOT EXISTS admin_actions ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, actor TEXT NOT NULL, "
"target TEXT NOT NULL, action TEXT NOT NULL, "
"before_json TEXT NOT NULL, after_json TEXT, "
"created_at REAL NOT NULL, undone_at REAL)")
# Что просили у движка смет и что он ответил — пишет ОСНОВНОЙ процесс, а не агент.
# Всё, что лежит в рабочей папке вкладки, агенту доступно на запись: 14.09.2026 он
# переписал файл запрета и выгрузил неполную смету для экспертизы. База ему недоступна.
@@ -1655,6 +1660,38 @@ def record_admin_visit(email: str, now: float | None = None) -> dict:
return {"previous": previous, "now": ts}
def record_admin_action(actor: str, target: str, action: str,
before: dict, after: dict, now: float | None = None) -> int:
import json as _json
ts = time.time() if now is None else now
with _conn() as conn:
cur = conn.execute(
"INSERT INTO admin_actions (actor, target, action, before_json, after_json, created_at) "
"VALUES (?,?,?,?,?,?)",
(actor, target, action, _json.dumps(before, ensure_ascii=False),
_json.dumps(after, ensure_ascii=False), ts))
return int(cur.lastrowid)
def get_admin_action(action_id: int) -> dict | None:
import json as _json
with _conn() as conn:
row = conn.execute("SELECT * FROM admin_actions WHERE id = ?", (action_id,)).fetchone()
if not row:
return None
d = dict(row)
d["before"] = _json.loads(d.pop("before_json") or "{}")
d["after"] = _json.loads(d.pop("after_json") or "{}")
return d
def mark_admin_action_undone(action_id: int, now: float | None = None) -> None:
ts = time.time() if now is None else now
with _conn() as conn:
conn.execute("UPDATE admin_actions SET undone_at = COALESCE(undone_at, ?) WHERE id = ?",
(ts, action_id))
def get_attention_states() -> dict:
with _conn() as conn:
rows = conn.execute("SELECT key, status, snooze_until FROM attention_state").fetchall()