You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

127 lines
4.1 KiB

  1. import base64
  2. from sqlalchemy.orm import Session
  3. from schemas.fuelingtask import CreateFuelingTask, OutputFuelingTask
  4. from db.models.fuelingtask import FuelingTask
  5. from db.repository.user import get_car_driver, get_user_by_id
  6. from db.repository.vehicle import get_vehicle_by_id
  7. def create_fueling_task(
  8. fueling_task: CreateFuelingTask, current_user: int, db: Session
  9. ):
  10. print(
  11. "Creating new fueling task"
  12. + str(fueling_task)
  13. + " by user "
  14. + str(current_user)
  15. )
  16. if not get_vehicle_by_id(fueling_task.VehicleId, db=db):
  17. return "novehicle"
  18. driver = get_car_driver(fueling_task.VehicleId, db=db)
  19. if not driver:
  20. return "nodriver"
  21. fueling_task_object = FuelingTask(
  22. DriverId=driver.Id,
  23. VehicleId=fueling_task.VehicleId,
  24. CreatedById=current_user,
  25. Description=fueling_task.Description,
  26. Date=fueling_task.Date,
  27. Cost=fueling_task.Cost,
  28. FuelRefilled=fueling_task.FuelRefilled,
  29. GasStationName=fueling_task.GasStationName,
  30. ImageBefore=fueling_task.ImageBefore.file.read(),
  31. ImageAfter=fueling_task.ImageAfter.file.read(),
  32. )
  33. db.add(fueling_task_object)
  34. db.commit()
  35. db.refresh(fueling_task_object)
  36. driverobj = driver.__dict__
  37. driverobj["AssignedVehicle"] = driver.vehicle.__dict__
  38. resobj = fueling_task_object.__dict__
  39. resobj["Driver"] = driverobj
  40. resobj["Creator"] = current_user
  41. ia = fueling_task_object.ImageAfter
  42. ib = fueling_task_object.ImageBefore
  43. if ia is not None:
  44. ia = base64.b64encode(ia).decode("ascii")
  45. if ib is not None:
  46. ib = base64.b64encode(ib).decode("ascii")
  47. resobj["ImageBefore"] = ib
  48. resobj["ImageAfter"] = ia
  49. return resobj
  50. def delete_fueling_task(fueling_task_id: int, db: Session):
  51. print("Deleting fueling task by id: " + str(fueling_task_id))
  52. fueling_task = (
  53. db.query(FuelingTask).filter(FuelingTask.Id == fueling_task_id).first()
  54. )
  55. if fueling_task:
  56. db.delete(fueling_task)
  57. db.commit()
  58. return True
  59. return False
  60. def get_fueling_task_by_id(fuel_task_id: int, db: Session):
  61. print("Retrieving fueling task by id: " + str(fuel_task_id))
  62. fuel_task = db.query(FuelingTask).filter(FuelingTask.Id == fuel_task_id).first()
  63. if not fuel_task:
  64. return "notfound"
  65. res = fuel_task.__dict__
  66. driver = get_user_by_id(fuel_task.DriverId, role="Driver", db=db)
  67. driver_obj = driver.__dict__
  68. imagebefore = fuel_task.ImageBefore
  69. imageafter = fuel_task.ImageAfter
  70. imagebeforeBase64 = base64.b64encode(imagebefore).decode("ascii")
  71. imageafterBase64 = base64.b64encode(imageafter).decode("ascii")
  72. res["Creator"] = fuel_task.CreatedById
  73. res["ImageBefore"] = imagebeforeBase64
  74. res["ImageAfter"] = imageafterBase64
  75. res["Driver"] = driver_obj
  76. res["Driver"]["AssignedVehicle"] = driver.vehicle
  77. return res
  78. def get_all_fueling_tasks(db: Session):
  79. print("Retrieving all fueling tasks")
  80. fuel_task = db.query(FuelingTask).all()
  81. result = []
  82. for task in fuel_task:
  83. res = task.__dict__
  84. res["ImageBefore"] = ""
  85. res["ImageAfter"] = ""
  86. res["Creator"] = task.CreatedById
  87. driver = get_user_by_id(task.DriverId, role="Driver", db=db)
  88. driver_obj = driver.__dict__
  89. res["Driver"] = driver_obj
  90. res["Driver"]["AssignedVehicle"] = driver.vehicle
  91. result.append(res)
  92. x = {"FuelingTasks": result}
  93. return x
  94. def get_fuel_tasks_by_creator(creator_id: int, db: Session):
  95. print(f"Retrieving all fueling tasks created by user ID: {creator_id}")
  96. fueling_tasks = (
  97. db.query(FuelingTask).filter(FuelingTask.CreatedById == creator_id).all()
  98. )
  99. result = []
  100. for task in fueling_tasks:
  101. res = task.__dict__
  102. res["ImageBefore"] = None
  103. res["ImageAfter"] = None
  104. res["Creator"] = task.CreatedById
  105. driver = get_user_by_id(task.DriverId, role="Driver", db=db)
  106. driver_obj = driver.__dict__
  107. res["Driver"] = driver_obj
  108. res["Driver"]["AssignedVehicle"] = driver.vehicle
  109. result.append(res)
  110. return {"FuelingTasks": result}