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.
 
 

65 lines
2.3 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(fueling_task: CreateFuelingTask, current_user: int, db: Session):
  8. if not get_vehicle_by_id(fueling_task.VehicleId, db=db):
  9. return "novehicle"
  10. driver = get_car_driver(fueling_task.VehicleId, db=db)
  11. if not driver:
  12. return "nodriver"
  13. fueling_task_object = FuelingTask(
  14. DriverId=driver.Id,
  15. VehicleId=fueling_task.VehicleId,
  16. CreatedById=current_user,
  17. Description=fueling_task.Description,
  18. Date=fueling_task.Date,
  19. Cost=fueling_task.Cost,
  20. FuelRefilled=fueling_task.FuelRefilled,
  21. GasStationName=fueling_task.GasStationName,
  22. ImageBefore=fueling_task.ImageBefore.file.read(),
  23. ImageAfter=fueling_task.ImageAfter.file.read(),
  24. )
  25. db.add(fueling_task_object)
  26. db.commit()
  27. db.refresh(fueling_task_object)
  28. print(driver.__dict__)
  29. driverobj = driver.__dict__
  30. driverobj["AssignedVehicle"] = driver.vehicle.__dict__
  31. fueling_task_object.Driver = driverobj
  32. return fueling_task_object
  33. def delete_fueling_task(fueling_task_id: int, db: Session):
  34. fueling_task = db.query(FuelingTask).filter(FuelingTask.Id == fueling_task_id).first()
  35. if fueling_task:
  36. db.delete(fueling_task)
  37. db.commit()
  38. return True
  39. return False
  40. def get_fueling_task_by_id(fuel_task_id: int, db: Session):
  41. fuel_task = db.query(FuelingTask).filter(FuelingTask.Id == fuel_task_id).first()
  42. if not fuel_task:
  43. return "notfound"
  44. res = fuel_task.__dict__
  45. driver = get_user_by_id(fuel_task.DriverId, role="Driver", db=db)
  46. driver_obj = driver.__dict__
  47. imagebefore = fuel_task.ImageBefore
  48. imageafter = fuel_task.ImageAfter
  49. imagebeforeBase64 = base64.b64encode(imagebefore).decode('ascii')
  50. imageafterBase64 = base64.b64encode(imageafter).decode('ascii')
  51. res["ImageBefore"] = imagebeforeBase64
  52. res["ImageAfter"] = imageafterBase64
  53. res["Driver"] = driver_obj
  54. res["Driver"]["AssignedVehicle"] = driver.vehicle
  55. return res