選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

36 行
1.5 KiB

  1. from fastapi import Depends, APIRouter, HTTPException, status
  2. from fastapi.responses import StreamingResponse
  3. from requests import Session
  4. from apis.v1.route_auth import get_current_user
  5. from db.models.user import User
  6. from db.repository.report import get_repot_jobsdone_by_driver, get_pdf
  7. from db.session import get_db
  8. router = APIRouter()
  9. @router.get("/jobsdone/{driver_id}", status_code=status.HTTP_200_OK)
  10. def get_report_jobsdone(driver_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
  11. if current_user.Role != "Admin":
  12. raise HTTPException(
  13. status_code=403, detail="You are not authorized to perform this action"
  14. )
  15. report = get_repot_jobsdone_by_driver(driver_id, db)
  16. if report == "notdriver":
  17. raise HTTPException(
  18. status_code=404, detail=f"Driver with id {driver_id} not found"
  19. )
  20. return report
  21. @router.get("/pdf/{driver_id}", status_code=status.HTTP_200_OK)
  22. def get_report_pdf(driver_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
  23. if current_user.Role != "Admin":
  24. raise HTTPException(
  25. status_code=403, detail="You are not authorized to perform this action"
  26. )
  27. report = get_pdf(driver_id, db)
  28. if report == "notdriver":
  29. raise HTTPException(
  30. status_code=404, detail=f"Driver with id {driver_id} not found"
  31. )
  32. return StreamingResponse(open(report, "rb"), media_type="application/pdf")