Create utils/audit.py
Browse files- utils/audit.py +26 -0
utils/audit.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import logging
|
| 3 |
+
from datetime import datetime, timezone
|
| 4 |
+
|
| 5 |
+
from db.init import get_conn
|
| 6 |
+
|
| 7 |
+
log = logging.getLogger("audit")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def audit(admin_id: str, action: str, target_user_id=None, details=None):
|
| 11 |
+
try:
|
| 12 |
+
with get_conn() as conn:
|
| 13 |
+
conn.execute(
|
| 14 |
+
"""INSERT INTO admin_audit(admin_id, action, target_user_id,
|
| 15 |
+
timestamp, details)
|
| 16 |
+
VALUES (?, ?, ?, ?, ?)""",
|
| 17 |
+
(
|
| 18 |
+
str(admin_id),
|
| 19 |
+
action,
|
| 20 |
+
target_user_id,
|
| 21 |
+
datetime.now(timezone.utc).isoformat(),
|
| 22 |
+
json.dumps(details or {}),
|
| 23 |
+
),
|
| 24 |
+
)
|
| 25 |
+
except Exception:
|
| 26 |
+
log.exception("audit write failed")
|