25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

maintenancejob.py 3.5 KiB

1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. result = []
  54. for job in maintenancejobs:
  55. job_dict = job.__dict__
  56. job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
  57. for part in job_dict["CarPartsList"]:
  58. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  59. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  60. job_dict["AssignedTo"] = job.CreatedBy.__dict__
  61. job_dict["Vehicle"] = job.Vehicle.__dict__
  62. result.append(job_dict)
  63. return maintenancejobs
  64. def get_maintenance_job(maintenancejob_id: int, db: Session):
  65. maintenancejob = (
  66. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  67. )
  68. res = maintenancejob.__dict__
  69. res["CarPartsList"] = [part.__dict__ for part in maintenancejob.CarParts]
  70. for part in maintenancejob.CarPartsList:
  71. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  72. # print(type(result.CarPartsList))
  73. res["TotalCost"] = calculate_total_cost(maintenancejob.CarParts)
  74. # print(result.TotalCost)
  75. if maintenancejob.Status == "Complete":
  76. res["FinishedBy"] = maintenancejob.FinishedBy.__dict__
  77. res["Vehicle"] = maintenancejob.Vehicle.__dict__
  78. print("DB Access complete")
  79. return maintenancejob
  80. def change_maintenance_status(maintenancejob_id: int, status: str, worker_id: int, db: Session):
  81. maintenancejob = (
  82. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  83. )
  84. if maintenancejob is None:
  85. return None
  86. if (status == "Complete"):
  87. maintenancejob.MaintenanceWorker = worker_id
  88. maintenancejob.Status = status
  89. db.commit()
  90. db.refresh(maintenancejob)
  91. return maintenancejob
  92. else:
  93. return None