27 lines
834 B
Python
27 lines
834 B
Python
import time
|
|
from typing import Callable
|
|
|
|
from .db import Database
|
|
|
|
AuditFn = Callable[[str, str, str, str | None, str | None], None]
|
|
|
|
|
|
def record(db: Database, user: str, action: str, target: str,
|
|
old: str | None = None, new: str | None = None) -> None:
|
|
db.execute(
|
|
"INSERT INTO audit(ts, user, action, target, old_value, new_value) VALUES (?,?,?,?,?,?)",
|
|
(int(time.time()), user, action, target, old, new),
|
|
)
|
|
|
|
|
|
def recorder(db: Database) -> AuditFn:
|
|
return lambda user, action, target, old=None, new=None: record(db, user, action, target, old, new)
|
|
|
|
|
|
def recent(db: Database, limit: int = 100) -> list[dict]:
|
|
rows = db.query(
|
|
"SELECT ts, user, action, target, old_value, new_value FROM audit ORDER BY id DESC LIMIT ?",
|
|
(limit,),
|
|
)
|
|
return [dict(row) for row in rows]
|