Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

179 рядки
5.5 KiB

  1. # Creating a new user in the database
  2. from math import ceil
  3. from sqlalchemy.orm import Session
  4. from schemas.user import OutputUser, UserCreate, DriverCreate
  5. from db.models.user import User
  6. from core.hashing import Hasher
  7. from db.models.drivetask import DriveTask
  8. def create_new_user(user: UserCreate, db: Session):
  9. print("Creating new user" + str(user))
  10. if get_user_by_email(user.Email, db):
  11. return "userExists"
  12. user_object = User(
  13. Email=user.Email,
  14. Name=user.Name,
  15. MiddleName=user.MiddleName,
  16. LastName=user.LastName,
  17. GovernmentId=user.GovernmentId,
  18. Address=user.Address,
  19. ContactNumber=user.ContactNumber,
  20. Role=user.Role,
  21. HashedPassword=Hasher.get_password_hash(user.Password),
  22. )
  23. db.add(user_object)
  24. db.commit()
  25. db.refresh(user_object)
  26. return user_object
  27. def create_new_driver(driver: DriverCreate, db: Session):
  28. print("Creating new driver" + str(driver))
  29. if get_user_by_email(driver.Email, db):
  30. return "userExists"
  31. driver_object = User(
  32. Email=driver.Email,
  33. Name=driver.Name,
  34. MiddleName=driver.MiddleName,
  35. LastName=driver.LastName,
  36. GovernmentId=driver.GovernmentId,
  37. Address=driver.Address,
  38. ContactNumber=driver.ContactNumber,
  39. Role="Driver",
  40. HashedPassword=Hasher.get_password_hash(driver.Password),
  41. DrivingLicenseNumber=driver.DrivingLicenseNumber,
  42. )
  43. db.add(driver_object)
  44. db.commit()
  45. db.refresh(driver_object)
  46. return driver_object
  47. def get_user_by_id(user_id: int, role: str, db: Session):
  48. print("Retrieving user by id: " + str(user_id))
  49. user = db.query(User).filter(User.Id == user_id).first()
  50. if not user:
  51. return False
  52. if user.Role != role and role != "Any":
  53. return False
  54. return user
  55. def get_user_by_email(email: str, db: Session):
  56. print("Retrieving user by email: " + str(email))
  57. user = db.query(User).filter(User.Email == email).first()
  58. return user
  59. def get_user_by_phone(phone: str, db: Session):
  60. print("Retrieving user by phone: " + str(phone))
  61. user = db.query(User).filter(User.ContactNumber == phone).first()
  62. return user
  63. def verify_driver_exists(driver_id: int, db: Session):
  64. print("Verifying driver exists: " + str(driver_id))
  65. driver = db.query(User).filter(User.Id == driver_id).first()
  66. if not driver:
  67. return False
  68. if driver.Role != "Driver":
  69. return False
  70. return True
  71. def get_car_driver(vehicle_id: int, db: Session):
  72. print("Retrieving driver for vehicle: " + str(vehicle_id))
  73. driver = db.query(User).filter(User.AssignedVehicle == vehicle_id).first()
  74. if not driver:
  75. return False
  76. return driver
  77. def list_users(db: Session, role: str = "Any"):
  78. print("Listing users")
  79. users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
  80. return users
  81. def get_users_by_name(
  82. db: Session, name: str = "", role: str = None, page: int = 1, per_page: int = 20
  83. ):
  84. print("Listing users by name")
  85. if role == "Admin":
  86. return None
  87. if role is None:
  88. users = db.query(User).filter(User.Name.like(f"{name}%"), User.Role != "Admin")
  89. else:
  90. users = db.query(User).filter(User.Name.like(f"{name}%"), User.Role == role)
  91. users = users.offset((page - 1) * per_page).limit(per_page).all()
  92. return users
  93. def replace_user_data(user_id: int, user_data: UserCreate, db: Session):
  94. print("Replacing user data" + str(user_data) + " for user id: " + str(user_id))
  95. user = db.query(User).filter(User.Id == user_id).first()
  96. if not user:
  97. return "userNotFound"
  98. user.Email = user_data.Email
  99. user.Name = user_data.Name
  100. user.MiddleName = user_data.MiddleName
  101. user.LastName = user_data.LastName
  102. user.GovernmentId = user_data.GovernmentId
  103. user.Address = user_data.Address
  104. user.ContactNumber = user_data.ContactNumber
  105. user.Role = user_data.Role
  106. if user_data.Password == "":
  107. print("Password not changed")
  108. else:
  109. user.HashedPassword = Hasher.get_password_hash(user_data.Password)
  110. db.commit()
  111. db.refresh(user)
  112. return user
  113. def delete_user_data(id: int, db: Session):
  114. print("Deleting user by id: " + str(id))
  115. user = db.query(User).filter(User.Id == id).first()
  116. if not user:
  117. return "userNotFound"
  118. drivetasks = db.query(DriveTask).filter(DriveTask.DriverId == id).all()
  119. for task in drivetasks: # delete all tasks assigned to this user
  120. db.delete(task)
  121. db.delete(user)
  122. db.commit()
  123. return "userDeleted"
  124. def user_search_query(
  125. db: Session, name: str = "", role: str = None, page: int = 1, per_page: int = 20
  126. ):
  127. if name == "" or name is None:
  128. query = db.query(User)
  129. else:
  130. query = db.query(User).filter(User.Name.like(f"{name}%"))
  131. if role is not None and role != "Admin":
  132. query = query.filter(User.Role == role)
  133. total_users = query.count()
  134. total_pages = ceil(total_users / per_page)
  135. users = query.offset((page - 1) * per_page).limit(per_page).all()
  136. output_users = [
  137. OutputUser(
  138. id=user.Id,
  139. Name=user.Name,
  140. MiddleName=user.MiddleName,
  141. LastName=user.LastName,
  142. ContactNumber=user.ContactNumber,
  143. Address=user.Address,
  144. Email=user.Email,
  145. Role=user.Role,
  146. AssignedVehicle=None,
  147. )
  148. for user in users
  149. ]
  150. return {"users": output_users, "total_pages": total_pages}