Add admin operation log rollback
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
|
||||
from ..auth import verify_admin_auth
|
||||
@@ -20,6 +22,7 @@ from ..data import (
|
||||
migrate_operation_log_labels,
|
||||
query_course_summaries,
|
||||
reject_admin_task,
|
||||
rollback_operation_log,
|
||||
update_course_summary_time,
|
||||
)
|
||||
|
||||
@@ -27,6 +30,16 @@ from ..data import (
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def rollback_backup_paths() -> list[Path]:
|
||||
return [
|
||||
CLASSNOTES_PATH,
|
||||
ACCOUNTS_PATH,
|
||||
ADMIN_TASKS_PATH,
|
||||
COURSE_SUMMARIES_ROOT,
|
||||
OPERATION_LOGS_PATH,
|
||||
]
|
||||
|
||||
|
||||
@router.get("/api/admin/tasks")
|
||||
def admin_tasks(
|
||||
status_filter: str = Query("", alias="status"),
|
||||
@@ -54,9 +67,29 @@ def admin_operation_logs(
|
||||
operation=operation,
|
||||
status_filter=status_filter,
|
||||
student=student,
|
||||
backup_paths=rollback_backup_paths(),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/admin/operation-logs/{log_id}/rollback")
|
||||
def admin_rollback_operation_log(log_id: str, _user: str = Depends(verify_admin_auth)):
|
||||
try:
|
||||
with write_lock:
|
||||
result = rollback_operation_log(OPERATION_LOGS_PATH, log_id, rollback_backup_paths())
|
||||
append_operation_log(
|
||||
OPERATION_LOGS_PATH,
|
||||
"撤回操作",
|
||||
"已撤回",
|
||||
target_log_id=log_id,
|
||||
target_backup_id=str(result.get("target_backup_id") or ""),
|
||||
backup_id=str(result.get("backup_id") or ""),
|
||||
restored_files=result.get("restored_files") or [],
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
return {"ok": True, **result}
|
||||
|
||||
|
||||
@router.get("/api/admin/course-summaries")
|
||||
def admin_course_summaries(
|
||||
q: str = Query(""),
|
||||
|
||||
Reference in New Issue
Block a user