Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

vehicle.py 4.1 KiB

1 rok temu
1 rok temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from sqlalchemy.orm import Session
  2. from schemas.vehicle import CreateVehicle, UpdateVehicle, VehicleLocation
  3. from db.models.vehicle import Vehicle
  4. from db.base import User
  5. from db.repository.user import get_car_driver
  6. def create_new_vehicle(vehicle: CreateVehicle, db: Session):
  7. print("Creating new vehicle" + str(vehicle))
  8. vehicle_object = Vehicle(
  9. **vehicle.model_dump(), Fuel=0, Status="Inactive", CurrentLocation=[]
  10. )
  11. db.add(vehicle_object)
  12. db.commit()
  13. db.refresh(vehicle_object)
  14. return vehicle_object
  15. def update_vehicle_geoloc(vehicle_id: int, location: VehicleLocation, db: Session):
  16. print("Updating vehicle geolocation" + str(location))
  17. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  18. vehicle_object = vehicle_db.first()
  19. if not vehicle_object:
  20. return "vehiclenotfound"
  21. print("Location: " + str(location.CurrentLocation))
  22. vehicle_object.CurrentLocation = location.CurrentLocation
  23. db.add(vehicle_object)
  24. db.commit()
  25. db.refresh(vehicle_object)
  26. return vehicle_object
  27. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  28. print("Assigning vehicle " + str(vehicle_id) + " to driver " + str(driver_id))
  29. if driver_id != None:
  30. driver = (
  31. db.query(User)
  32. .filter((User.Id == driver_id) & (User.Role == "Driver"))
  33. .first()
  34. )
  35. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  36. if not vehicle:
  37. return "novehicle"
  38. if not driver:
  39. return "nodriver"
  40. driver.AssignedVehicle = vehicle_id
  41. else:
  42. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  43. if not vehicle:
  44. return "novehicle"
  45. if vehicle.Status == "Active":
  46. return "alreadyassigned"
  47. driver = get_car_driver(vehicle_id, db)
  48. if driver:
  49. driver.AssignedVehicle = None
  50. else:
  51. return "nodriver"
  52. db.add(driver)
  53. db.commit()
  54. return vehicle
  55. def list_vehicles(db: Session):
  56. print("Listing vehicles")
  57. vehicles = db.query(Vehicle).all()
  58. # assign AssignedDriver to all vehicles based on their drivers
  59. for vehicle in vehicles:
  60. driver = get_car_driver(vehicle.Id, db)
  61. if driver:
  62. vehicle.AssignedDriver = driver.__dict__
  63. else:
  64. vehicle.AssignedDriver = None
  65. return vehicles
  66. def get_vehicle_by_id(vehicle_id: int, db: Session):
  67. print("Retrieving vehicle by id: " + str(vehicle_id))
  68. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  69. if not vehicle:
  70. return None
  71. driver = get_car_driver(vehicle.Id, db)
  72. if driver:
  73. vehicle.AssignedDriver = driver.__dict__
  74. else:
  75. vehicle.AssignedDriver = None
  76. return vehicle
  77. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  78. print("Replacing vehicle data for vehicle " + str(id) + " with " + str(vehicle))
  79. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  80. vehicle_object = vehicle_db.first()
  81. if not vehicle_object:
  82. return "vehiclenotfound"
  83. # vehicle_object.CurrentLocation = vehicle.CurrentLocation
  84. vehicle_object.Fuel = vehicle.Fuel
  85. vehicle_object.LicensePlate = vehicle.LicensePlate
  86. # vehicle_object.DriverHistory = vehicle.DriverHistory
  87. vehicle_object.Type = vehicle.Type
  88. vehicle_object.Mileage = vehicle.Mileage
  89. vehicle_object.Model = vehicle.Model
  90. vehicle_object.Status = vehicle.Status
  91. # res = assign_vehicle_driver(id, vehicle.AssignedDriver, db)
  92. # if res == "nodriver":
  93. # return "driverNotFound"
  94. vehicle_object.Capacity = vehicle.Capacity
  95. vehicle_object.Year = vehicle.Year
  96. db.add(vehicle_object)
  97. db.commit()
  98. # vehicle_object.AssignedDriver = vehicle.AssignedDriver
  99. return vehicle_object
  100. def delete_vehicle_data(id: int, db: Session):
  101. print("Deleting vehicle data for vehicle " + str(id))
  102. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  103. vehicle_object = vehicle_db.first()
  104. if not vehicle_object:
  105. return "vehiclenotfound"
  106. db.delete(vehicle_object)
  107. db.commit()
  108. return vehicle_object