Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

route_user.py 4.5 KiB

há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
há 1 ano
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Routes for user. MAIN PART OF THE API
  2. from math import ceil
  3. from fastapi import APIRouter, HTTPException, status, Query
  4. from sqlalchemy.orm import Session
  5. from sqlalchemy import or_
  6. from fastapi import Depends
  7. from typing import List, Annotated
  8. from apis.v1.route_auth import get_current_user
  9. from core.config import settings
  10. from db.models.user import User
  11. from schemas.user import (
  12. UserCreate,
  13. ShowUser,
  14. ShowDriver,
  15. DriverCreate,
  16. OutputUser,
  17. UsersPage,
  18. )
  19. from db.session import get_db
  20. from db.repository.user import (
  21. create_new_user,
  22. list_users,
  23. get_user_by_id,
  24. replace_user_data,
  25. create_new_driver,
  26. delete_user_data,
  27. get_users_by_name,
  28. user_search_query,
  29. )
  30. router = APIRouter()
  31. @router.post("/", response_model=ShowUser, status_code=status.HTTP_201_CREATED)
  32. def create_user(
  33. user: UserCreate,
  34. db: Session = Depends(get_db),
  35. current_user: User = Depends(get_current_user),
  36. ):
  37. if user.Role not in settings.ALLOWED_ROLES:
  38. raise HTTPException(
  39. status_code=400,
  40. detail=f"Role {user.Role} is not allowed. Allowed roles are {settings.ALLOWED_ROLES}",
  41. )
  42. if current_user.Role != "Admin":
  43. raise HTTPException(
  44. status_code=403, detail="You are not authorized to perform this action"
  45. )
  46. # if current_user.Role != "Admin":
  47. # raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  48. user = create_new_user(user=user, db=db)
  49. return user
  50. @router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED)
  51. def create_driver(
  52. driver: DriverCreate,
  53. db: Session = Depends(get_db),
  54. current_user: User = Depends(get_current_user),
  55. ):
  56. if current_user.Role != "Admin":
  57. raise HTTPException(
  58. status_code=403, detail="You are not authorized to perform this action"
  59. )
  60. driver = create_new_driver(driver=driver, db=db)
  61. return driver
  62. @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK)
  63. def get_all_users(db: Session = Depends(get_db), role: str = None):
  64. if role is None:
  65. users = list_users(db=db)
  66. return users
  67. users = list_users(db=db, role=role)
  68. return users
  69. @router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED)
  70. def update_user(
  71. user_id: int,
  72. user: UserCreate,
  73. db: Session = Depends(get_db),
  74. current_user: User = Depends(get_current_user),
  75. ):
  76. if current_user.Role != "Admin":
  77. raise HTTPException(
  78. status_code=403, detail="You are not authorized to perform this action"
  79. )
  80. user = replace_user_data(user_id=user_id, user_data=user, db=db)
  81. if user == "userNotFound":
  82. raise HTTPException(status_code=404, detail="User not found")
  83. return user
  84. @router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK)
  85. def get_user_me(
  86. current_user: Annotated[User, Depends(get_current_user)],
  87. db: Annotated[Session, Depends(get_db)],
  88. ):
  89. print("Getting current user...")
  90. return current_user
  91. @router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
  92. def get_user(user_id: int, db: Session = Depends(get_db)):
  93. user = get_user_by_id(user_id=user_id, role="Any", db=db)
  94. if not user:
  95. raise HTTPException(status_code=404, detail="User not found")
  96. return user
  97. @router.get(
  98. "/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK
  99. )
  100. def get_driver(driver_id: int, db: Session = Depends(get_db)):
  101. driver = get_user_by_id(user_id=driver_id, role="Driver", db=db)
  102. if not driver:
  103. raise HTTPException(status_code=404, detail="Driver not found")
  104. res = driver.__dict__
  105. res["AssignedVehicle"] = driver.vehicle
  106. return driver
  107. @router.delete("/{user_id}", status_code=status.HTTP_200_OK)
  108. def delete_user(
  109. user_id: int,
  110. db: Session = Depends(get_db),
  111. current_user: User = Depends(get_current_user),
  112. ):
  113. if current_user.Role != "Admin":
  114. raise HTTPException(
  115. status_code=403, detail="You are not authorized to perform this action"
  116. )
  117. result = delete_user_data(id=user_id, db=db)
  118. if result == "userNotFound":
  119. raise HTTPException(status_code=404, detail="User not found")
  120. return result
  121. @router.get("/search/", response_model=UsersPage)
  122. def search_users(
  123. db: Session = Depends(get_db),
  124. name: str = None,
  125. role: str = None,
  126. page: int = 1,
  127. per_page: int = 20,
  128. ):
  129. return user_search_query(db=db, name=name, role=role, page=page, per_page=per_page)