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.
 
 

105 lines
3.4 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. ia = fueling_task_object.ImageAfter
  41. ib = fueling_task_object.ImageBefore
  42. if ia is not None:
  43. ia = base64.b64encode(ia).decode("ascii")
  44. if ib is not None:
  45. ib = base64.b64encode(ib).decode("ascii")
  46. resobj["ImageBefore"] = ib
  47. resobj["ImageAfter"] = ia
  48. return resobj
  49. def delete_fueling_task(fueling_task_id: int, db: Session):
  50. print("Deleting fueling task by id: " + str(fueling_task_id))
  51. fueling_task = (
  52. db.query(FuelingTask).filter(FuelingTask.Id == fueling_task_id).first()
  53. )
  54. if fueling_task:
  55. db.delete(fueling_task)
  56. db.commit()
  57. return True
  58. return False
  59. def get_fueling_task_by_id(fuel_task_id: int, db: Session):
  60. print("Retrieving fueling task by id: " + str(fuel_task_id))
  61. fuel_task = db.query(FuelingTask).filter(FuelingTask.Id == fuel_task_id).first()
  62. if not fuel_task:
  63. return "notfound"
  64. res = fuel_task.__dict__
  65. driver = get_user_by_id(fuel_task.DriverId, role="Driver", db=db)
  66. driver_obj = driver.__dict__
  67. imagebefore = fuel_task.ImageBefore
  68. imageafter = fuel_task.ImageAfter
  69. imagebeforeBase64 = base64.b64encode(imagebefore).decode("ascii")
  70. imageafterBase64 = base64.b64encode(imageafter).decode("ascii")
  71. res["Creator"] = fuel_task.CreatedById
  72. res["ImageBefore"] = imagebeforeBase64
  73. res["ImageAfter"] = imageafterBase64
  74. res["Driver"] = driver_obj
  75. res["Driver"]["AssignedVehicle"] = driver.vehicle
  76. return res
  77. def get_all_fueling_tasks(db: Session):
  78. print("Retrieving all fueling tasks")
  79. fuel_task = db.query(FuelingTask).all()
  80. result = []
  81. for task in fuel_task:
  82. res = task.__dict__
  83. res["ImageBefore"] = ""
  84. res["ImageAfter"] = ""
  85. res["Creator"] = task.CreatedById
  86. driver = get_user_by_id(task.DriverId, role="Driver", db=db)
  87. driver_obj = driver.__dict__
  88. res["Driver"] = driver_obj
  89. res["Driver"]["AssignedVehicle"] = driver.vehicle
  90. result.append(res)
  91. x = {"FuelingTasks": result}
  92. return x