No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

route_user.py 3.8 KiB

hace 1 año
hace 1 año
hace 1 año
hace 1 año
hace 1 año
hace 1 año
hace 1 año
hace 1 año
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Routes for user. MAIN PART OF THE API
  2. from fastapi import APIRouter, HTTPException, status
  3. from sqlalchemy.orm import Session
  4. from fastapi import Depends
  5. from typing import List, Annotated
  6. from apis.v1.route_auth import get_current_user
  7. from db.models.user import User
  8. from schemas.user import UserCreate, ShowUser, ShowDriver, DriverCreate
  9. from db.session import get_db
  10. from db.repository.user import (
  11. create_new_user,
  12. list_users,
  13. get_user_by_id,
  14. replace_user_data,
  15. create_new_driver,
  16. delete_user_data,
  17. )
  18. router = APIRouter()
  19. @router.post("/", response_model=ShowUser, status_code=status.HTTP_201_CREATED)
  20. def create_user(
  21. user: UserCreate,
  22. db: Session = Depends(get_db),
  23. current_user: User = Depends(get_current_user),
  24. ):
  25. if current_user.Role != "Admin":
  26. raise HTTPException(
  27. status_code=403, detail="You are not authorized to perform this action"
  28. )
  29. # if current_user.Role != "Admin":
  30. # raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  31. user = create_new_user(user=user, db=db)
  32. return user
  33. @router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED)
  34. def create_driver(
  35. driver: DriverCreate,
  36. db: Session = Depends(get_db),
  37. current_user: User = Depends(get_current_user),
  38. ):
  39. if current_user.Role != "Admin":
  40. raise HTTPException(
  41. status_code=403, detail="You are not authorized to perform this action"
  42. )
  43. driver = create_new_driver(driver=driver, db=db)
  44. return driver
  45. @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK)
  46. def get_all_users(db: Session = Depends(get_db), role: str = None):
  47. if role is None:
  48. users = list_users(db=db)
  49. return users
  50. users = list_users(db=db, role=role)
  51. return users
  52. @router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED)
  53. def update_user(
  54. user_id: int,
  55. user: UserCreate,
  56. db: Session = Depends(get_db),
  57. current_user: User = Depends(get_current_user),
  58. ):
  59. if current_user.Role != "Admin":
  60. raise HTTPException(
  61. status_code=403, detail="You are not authorized to perform this action"
  62. )
  63. user = replace_user_data(user_id=user_id, user=user, db=db)
  64. return user
  65. @router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK)
  66. def get_user_me(
  67. current_user: Annotated[User, Depends(get_current_user)],
  68. db: Annotated[Session, Depends(get_db)],
  69. ):
  70. print("Getting current user...")
  71. return current_user
  72. @router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
  73. def get_user(user_id: int, db: Session = Depends(get_db)):
  74. user = get_user_by_id(user_id=user_id, db=db)
  75. if not user:
  76. raise HTTPException(status_code=404, detail="User not found")
  77. return user
  78. @router.get(
  79. "/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK
  80. )
  81. def get_driver(driver_id: int, db: Session = Depends(get_db)):
  82. driver = get_user_by_id(user_id=driver_id, role="Driver", db=db)
  83. if not driver:
  84. raise HTTPException(status_code=404, detail="Driver not found")
  85. res = driver.__dict__
  86. res["AssignedVehicle"] = driver.vehicle
  87. return driver
  88. @router.delete("/{user_id}", status_code=status.HTTP_200_OK)
  89. def delete_user(
  90. user_id: int,
  91. db: Session = Depends(get_db),
  92. current_user: User = Depends(get_current_user),
  93. ):
  94. if current_user.Role != "Admin":
  95. raise HTTPException(
  96. status_code=403, detail="You are not authorized to perform this action"
  97. )
  98. result = delete_user_data(id=user_id, db=db)
  99. if result == "userNotFound":
  100. raise HTTPException(status_code=404, detail="User not found")
  101. return result