No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

95 líneas
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["ImageBefore"] = imagebeforeBase64
  65. res["ImageAfter"] = imageafterBase64
  66. res["Driver"] = driver_obj
  67. res["Driver"]["AssignedVehicle"] = driver.vehicle
  68. return res
  69. def get_all_fueling_tasks(db: Session):
  70. fuel_task = db.query(FuelingTask).all()
  71. result = []
  72. for task in fuel_task:
  73. res = task.__dict__
  74. res["ImageBefore"] = ""
  75. res["ImageAfter"] = ""
  76. driver = get_user_by_id(task.DriverId, role="Driver", db=db)
  77. driver_obj = driver.__dict__
  78. res["Driver"] = driver_obj
  79. res["Driver"]["AssignedVehicle"] = driver.vehicle
  80. result.append(res)
  81. x = {"FuelingTasks": result}
  82. return x