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.

maintenancejob.py 3.9 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import base64
  2. from sqlalchemy.orm import Session
  3. from schemas.maintenancejob import CreateMaintenanceJob
  4. from db.models.maintenancejob import MaintenanceJob
  5. from schemas.carpart import CreateCarPart
  6. from db.models.carpart import CarPart
  7. from db.repository.user import get_car_driver
  8. def create_new_maintenancejob(
  9. maintenancejob: CreateMaintenanceJob, db: Session
  10. ):
  11. vehicledriver = get_car_driver(maintenancejob.VehicleID, db)
  12. if (vehicledriver != False):
  13. vehicledriver = vehicledriver.Id
  14. else:
  15. return "nodriver"
  16. maintenancejob_object = MaintenanceJob(
  17. Description=maintenancejob.Description,
  18. VehicleID=maintenancejob.VehicleID,
  19. Date=maintenancejob.Date,
  20. VehicleDriverId=get_car_driver(maintenancejob.VehicleID, db).Id,
  21. Status="Requested",
  22. )
  23. print("OBJECT CREATED")
  24. db.add(maintenancejob_object)
  25. db.commit()
  26. db.refresh(maintenancejob_object)
  27. return maintenancejob_object
  28. def create_car_part(car_part: CreateCarPart, db: Session):
  29. car_part_object = CarPart(
  30. ParentId=car_part.ParentId,
  31. Name=car_part.Name,
  32. Number=car_part.Number,
  33. Condition=car_part.Condition,
  34. ImageURL=car_part.image.file.read(),
  35. Cost=car_part.Cost,
  36. )
  37. print("OBJECT CREATED")
  38. db.add(car_part_object)
  39. db.commit()
  40. print("OBJECT SAVED")
  41. db.refresh(car_part_object)
  42. print("OBJECT REFRESHED")
  43. res_obj = car_part_object.__dict__
  44. res_obj["image"] = base64.b64encode(car_part_object.ImageURL).decode("ascii")
  45. return res_obj
  46. def calculate_total_cost(car_parts: CarPart):
  47. total_cost = 0
  48. for part in car_parts:
  49. total_cost += part.Cost
  50. return total_cost
  51. def get_all_maintenance_jobs(db: Session):
  52. maintenancejobs = db.query(MaintenanceJob).all()
  53. print("DB Access complete")
  54. result = []
  55. for job in maintenancejobs:
  56. job_dict = job.__dict__
  57. print(job_dict)
  58. job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
  59. print(len(job_dict["CarPartsList"]))
  60. for part in job_dict["CarPartsList"]:
  61. if (part["ImageURL"] is None) or (part["ImageURL"] == ""):
  62. part["image"] = ""
  63. else:
  64. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  65. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  66. if job.Status == "Complete":
  67. job_dict["FinishedBy"] = job.FinishedBy.__dict__
  68. job_dict["Vehicle"] = job.Vehicle.__dict__
  69. result.append(job_dict)
  70. print("Returning...")
  71. return maintenancejobs
  72. def get_maintenance_job(maintenancejob_id: int, db: Session):
  73. maintenancejob = (
  74. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  75. )
  76. if maintenancejob is None:
  77. return None
  78. res = maintenancejob.__dict__
  79. res["CarPartsList"] = [part.__dict__ for part in maintenancejob.CarParts]
  80. for part in maintenancejob.CarPartsList:
  81. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  82. # print(type(result.CarPartsList))
  83. res["TotalCost"] = calculate_total_cost(maintenancejob.CarParts)
  84. # print(result.TotalCost)
  85. if maintenancejob.Status == "Complete":
  86. res["FinishedBy"] = maintenancejob.FinishedBy.__dict__
  87. res["Vehicle"] = maintenancejob.Vehicle.__dict__
  88. print("DB Access complete")
  89. return maintenancejob
  90. def change_maintenance_status(maintenancejob_id: int, status: str, worker_id: int, db: Session):
  91. maintenancejob = (
  92. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  93. )
  94. if maintenancejob is None:
  95. return None
  96. if (status == "Complete"):
  97. maintenancejob.MaintenanceWorker = worker_id
  98. maintenancejob.Status = status
  99. db.commit()
  100. db.refresh(maintenancejob)
  101. return maintenancejob
  102. else:
  103. return None