Files

60 lines
2.2 KiB
Python
Raw Permalink Normal View History

2026-06-08 15:16:07 +08:00
from pathlib import Path
from fastapi import APIRouter, Depends
2026-06-08 15:16:07 +08:00
from fastapi.responses import FileResponse
from sqlalchemy.orm import Session
from app.core.response import ApiResponse, ok
from app.core.user_context import UserContext, get_user_context
from app.db.session import get_db
from app.schemas.evaluation import EvaluationDetailResponse, EvaluationListResponse, ExportPdfResponse
from app.services.evaluation_service import EvaluationService
from app.services.pdf_export_service import PdfExportService
router = APIRouter()
@router.get("", response_model=ApiResponse[EvaluationListResponse])
def list_evaluations(ctx: UserContext = Depends(get_user_context), db: Session = Depends(get_db)):
"""历史评价:基于 user_id 查询完整训练后的评价记录。"""
return ok(EvaluationService(db).list_history(ctx.user_id))
@router.get("/{evaluation_id}", response_model=ApiResponse[EvaluationDetailResponse])
def get_evaluation_detail(
evaluation_id: int,
ctx: UserContext = Depends(get_user_context),
db: Session = Depends(get_db),
):
"""评价详情:校验 user_id 后返回完整评价报告。"""
return ok(EvaluationService(db).get_detail(evaluation_id, ctx.user_id))
@router.post("/{evaluation_id}/export-pdf", response_model=ApiResponse[ExportPdfResponse])
def export_pdf(
evaluation_id: int,
ctx: UserContext = Depends(get_user_context),
db: Session = Depends(get_db),
):
"""PDF 导出:生成评价报告 PDF 并保存导出记录。"""
export = PdfExportService(db).export(evaluation_id, ctx.user_id)
db.commit()
return ok(ExportPdfResponse(export_id=export.id, file_path=export.file_path))
2026-06-08 15:16:07 +08:00
@router.get("/{evaluation_id}/download-pdf", response_class=FileResponse)
def download_pdf(
evaluation_id: int,
ctx: UserContext = Depends(get_user_context),
db: Session = Depends(get_db),
):
"""PDF 下载:校验评价归属后生成报告,并以文件流方式触发浏览器下载。"""
export = PdfExportService(db).export(evaluation_id, ctx.user_id)
db.commit()
file_path = Path(export.file_path)
return FileResponse(
path=file_path,
media_type="application/pdf",
filename=file_path.name,
)