62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
|
|
from ..api_utils import load_accounts, load_records, load_teachers
|
|
from ..auth import verify_records_auth
|
|
from ..config import ADMIN_TASKS_PATH, COURSE_SUMMARIES_ROOT
|
|
from ..data import (
|
|
account_to_dict,
|
|
query_public_records,
|
|
submit_public_correction_tasks,
|
|
submit_public_deletion_tasks,
|
|
)
|
|
from ..schemas import CorrectionSubmitPayload, DeletionSubmitPayload
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/api/records")
|
|
def records(
|
|
q: str = Query(..., min_length=1, description="自然语言查询,例如:王鑫鹏5月数学课"),
|
|
limit: int = Query(200, ge=1, le=1000),
|
|
_user: str = Depends(verify_records_auth),
|
|
):
|
|
return query_public_records(load_records(), load_teachers(), q, limit=limit, summaries_root=COURSE_SUMMARIES_ROOT)
|
|
|
|
|
|
@router.get("/api/student-account/{student}")
|
|
def record_student_account(student: str, _user: str = Depends(verify_records_auth)):
|
|
for account in load_accounts():
|
|
if account.student == student or account.student_id == student:
|
|
return account_to_dict(account)
|
|
raise HTTPException(status_code=404, detail=f"未找到学生账户: {student}")
|
|
|
|
|
|
@router.post("/api/corrections")
|
|
def submit_corrections(payload: CorrectionSubmitPayload, _user: str = Depends(verify_records_auth)):
|
|
try:
|
|
result = submit_public_correction_tasks(
|
|
ADMIN_TASKS_PATH,
|
|
load_records(),
|
|
load_teachers(),
|
|
[item.dict() for item in payload.items],
|
|
)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
return {"ok": True, **result}
|
|
|
|
|
|
@router.post("/api/deletions")
|
|
def submit_deletions(payload: DeletionSubmitPayload, _user: str = Depends(verify_records_auth)):
|
|
try:
|
|
result = submit_public_deletion_tasks(
|
|
ADMIN_TASKS_PATH,
|
|
load_records(),
|
|
[item.dict() for item in payload.items],
|
|
)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
return {"ok": True, **result}
|