Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

97 linhas
3.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. if not get_vehicle_by_id(fueling_task.VehicleId, db=db):
  11. return "novehicle"
  12. driver = get_car_driver(fueling_task.VehicleId, db=db)
  13. if not driver:
  14. return "nodriver"
  15. fueling_task_object = FuelingTask(
  16. DriverId=driver.Id,
  17. VehicleId=fueling_task.VehicleId,
  18. CreatedById=current_user,
  19. Description=fueling_task.Description,
  20. Date=fueling_task.Date,
  21. Cost=fueling_task.Cost,
  22. FuelRefilled=fueling_task.FuelRefilled,
  23. GasStationName=fueling_task.GasStationName,
  24. ImageBefore=fueling_task.ImageBefore.file.read(),
  25. ImageAfter=fueling_task.ImageAfter.file.read(),
  26. )
  27. db.add(fueling_task_object)
  28. db.commit()
  29. db.refresh(fueling_task_object)
  30. print(driver.__dict__)
  31. driverobj = driver.__dict__
  32. driverobj["AssignedVehicle"] = driver.vehicle.__dict__
  33. resobj = fueling_task_object.__dict__
  34. resobj["Driver"] = driverobj
  35. ia = fueling_task_object.ImageAfter
  36. ib = fueling_task_object.ImageBefore
  37. if ia is not None:
  38. ia = base64.b64encode(ia).decode("ascii")
  39. if ib is not None:
  40. ib = base64.b64encode(ib).decode("ascii")
  41. resobj["ImageBefore"] = ib
  42. resobj["ImageAfter"] = ia
  43. return resobj
  44. def delete_fueling_task(fueling_task_id: int, db: Session):
  45. fueling_task = (
  46. db.query(FuelingTask).filter(FuelingTask.Id == fueling_task_id).first()
  47. )
  48. if fueling_task:
  49. db.delete(fueling_task)
  50. db.commit()
  51. return True
  52. return False
  53. def get_fueling_task_by_id(fuel_task_id: int, db: Session):
  54. fuel_task = db.query(FuelingTask).filter(FuelingTask.Id == fuel_task_id).first()
  55. if not fuel_task:
  56. return "notfound"
  57. res = fuel_task.__dict__
  58. driver = get_user_by_id(fuel_task.DriverId, role="Driver", db=db)
  59. driver_obj = driver.__dict__
  60. imagebefore = fuel_task.ImageBefore
  61. imageafter = fuel_task.ImageAfter
  62. imagebeforeBase64 = base64.b64encode(imagebefore).decode("ascii")
  63. imageafterBase64 = base64.b64encode(imageafter).decode("ascii")
  64. res["Creator"] = fuel_task.CreatedById
  65. res["ImageBefore"] = imagebeforeBase64
  66. res["ImageAfter"] = imageafterBase64
  67. res["Driver"] = driver_obj
  68. res["Driver"]["AssignedVehicle"] = driver.vehicle
  69. return res
  70. def get_all_fueling_tasks(db: Session):
  71. fuel_task = db.query(FuelingTask).all()
  72. result = []
  73. for task in fuel_task:
  74. res = task.__dict__
  75. res["ImageBefore"] = ""
  76. res["ImageAfter"] = ""
  77. res["Creator"] = task.CreatedById
  78. driver = get_user_by_id(task.DriverId, role="Driver", db=db)
  79. driver_obj = driver.__dict__
  80. res["Driver"] = driver_obj
  81. res["Driver"]["AssignedVehicle"] = driver.vehicle
  82. result.append(res)
  83. x = {"FuelingTasks": result}
  84. return x