template.py 857 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. from fastapi import APIRouter, Query, Depends
  5. from fastapi.responses import FileResponse
  6. from models.user import Admin
  7. from utils.depends import get_current_user
  8. from core.config import settings
  9. router = APIRouter()
  10. @router.get("/templates", summary="模板下载")
  11. async def download(tmpl: str = Query(..., description="模板类型,取值:teacher/student/work/exam"),
  12. current_user: Admin = Depends(get_current_user)):
  13. if tmpl in ["teacher", "student"]:
  14. filename = f"tmpl_{tmpl}.xlsx"
  15. else:
  16. filename = f"tmpl_category_{tmpl}.xlsx"
  17. tmpl_path = os.path.join(settings.TEMPLATE_PATH, filename)
  18. if not os.path.exists(tmpl_path):
  19. return {"errcode": 400, "mess": "模板不存在!"}
  20. return FileResponse(tmpl_path, filename=filename)