Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

route_user.py 4.8 KiB

před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if user == "userExists":
  50. raise HTTPException(
  51. status_code=400, detail=f"User with this email already exists"
  52. )
  53. return user
  54. @router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED)
  55. def create_driver(
  56. driver: DriverCreate,
  57. db: Session = Depends(get_db),
  58. current_user: User = Depends(get_current_user),
  59. ):
  60. if current_user.Role != "Admin":
  61. raise HTTPException(
  62. status_code=403, detail="You are not authorized to perform this action"
  63. )
  64. driver = create_new_driver(driver=driver, db=db)
  65. if driver == "userExists":
  66. raise HTTPException(
  67. status_code=400, detail=f"User with this email already exists"
  68. )
  69. return driver
  70. @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK)
  71. def get_all_users(db: Session = Depends(get_db), role: str = None):
  72. if role is None:
  73. users = list_users(db=db)
  74. return users
  75. users = list_users(db=db, role=role)
  76. return users
  77. @router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED)
  78. def update_user(
  79. user_id: int,
  80. user: UserCreate,
  81. db: Session = Depends(get_db),
  82. current_user: User = Depends(get_current_user),
  83. ):
  84. if current_user.Role != "Admin":
  85. raise HTTPException(
  86. status_code=403, detail="You are not authorized to perform this action"
  87. )
  88. user = replace_user_data(user_id=user_id, user_data=user, db=db)
  89. if user == "userNotFound":
  90. raise HTTPException(status_code=404, detail="User not found")
  91. return user
  92. @router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK)
  93. def get_user_me(
  94. current_user: Annotated[User, Depends(get_current_user)],
  95. db: Annotated[Session, Depends(get_db)],
  96. ):
  97. print("Getting current user...")
  98. return current_user
  99. @router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
  100. def get_user(user_id: int, db: Session = Depends(get_db)):
  101. user = get_user_by_id(user_id=user_id, role="Any", db=db)
  102. if not user:
  103. raise HTTPException(status_code=404, detail="User not found")
  104. return user
  105. @router.get(
  106. "/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK
  107. )
  108. def get_driver(driver_id: int, db: Session = Depends(get_db)):
  109. driver = get_user_by_id(user_id=driver_id, role="Driver", db=db)
  110. if not driver:
  111. raise HTTPException(status_code=404, detail="Driver not found")
  112. res = driver.__dict__
  113. res["AssignedVehicle"] = driver.vehicle
  114. return driver
  115. @router.delete("/{user_id}", status_code=status.HTTP_200_OK)
  116. def delete_user(
  117. user_id: int,
  118. db: Session = Depends(get_db),
  119. current_user: User = Depends(get_current_user),
  120. ):
  121. if current_user.Role != "Admin":
  122. raise HTTPException(
  123. status_code=403, detail="You are not authorized to perform this action"
  124. )
  125. result = delete_user_data(id=user_id, db=db)
  126. if result == "userNotFound":
  127. raise HTTPException(status_code=404, detail="User not found")
  128. return result
  129. @router.get("/search/", response_model=UsersPage)
  130. def search_users(
  131. db: Session = Depends(get_db),
  132. name: str = None,
  133. role: str = None,
  134. page: int = 1,
  135. per_page: int = 20,
  136. ):
  137. return user_search_query(db=db, name=name, role=role, page=page, per_page=per_page)