You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

route_report.py 1.5 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536
  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")