Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

115 righe
3.5 KiB

  1. from datetime import datetime
  2. from sqlalchemy.orm import Session
  3. from schemas.drivetask import CreateTask
  4. from db.models.drivetask import DriveTask
  5. from db.repository.user import get_user_by_id
  6. def create_new_task(task: CreateTask, db: Session):
  7. print("Creating new task" + str(task) + " by user " + str(task.DriverId))
  8. driver = get_user_by_id(task.DriverId, "Driver", db)
  9. if not driver:
  10. return "notdriver"
  11. elif driver.Role != "Driver":
  12. return "notdriver"
  13. task_object = DriveTask(
  14. DriverId=task.DriverId,
  15. Description=task.Description,
  16. Status="Pending",
  17. StartLocation=task.StartLocation,
  18. EndLocation=task.EndLocation,
  19. )
  20. db.add(task_object)
  21. db.commit()
  22. db.refresh(task_object)
  23. return task_object
  24. def change_task_status(task_id: int, status: str, distance: int, db: Session):
  25. print("Changing task status to " + status + " for task " + str(task_id))
  26. task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
  27. if not task:
  28. return "notaskfound"
  29. if status == "In Progress":
  30. # see if there are any other tasks in progress by this driver, if yes, cancel
  31. task.StartDateTime = datetime.now()
  32. if status == "Completed":
  33. task.DistanceCovered = distance
  34. task.EndDateTime = datetime.now()
  35. task.Status = status
  36. db.commit()
  37. db.refresh(task)
  38. return task
  39. def get_task_driver(task_id: int, db: Session):
  40. task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
  41. if not task:
  42. return "notaskfound"
  43. driver = get_user_by_id(task.DriverId, "Any", db)
  44. if not driver:
  45. return "notdriver"
  46. return driver
  47. def get_tasks_by_driver(driver_id: int, db: Session):
  48. driver = get_user_by_id(driver_id, "Any", db)
  49. if not driver:
  50. return "notdriver"
  51. if driver.Role != "Driver":
  52. return "notdriver"
  53. tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
  54. return tasks
  55. def get_task_by_id(task_id: int, db: Session):
  56. task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
  57. if not task:
  58. return "notaskfound"
  59. task.Driver = task.CreatedBy.__dict__
  60. task.Driver["AssignedVehicle"] = task.CreatedBy.vehicle
  61. return task
  62. def get_all_tasks(status: str, db: Session):
  63. if status != "Any":
  64. tasks = db.query(DriveTask).filter(DriveTask.Status == status).all()
  65. else:
  66. tasks = db.query(DriveTask).all()
  67. for task in tasks:
  68. task.Driver = task.CreatedBy.__dict__
  69. task.Driver["AssignedVehicle"] = task.CreatedBy.vehicle
  70. return tasks
  71. def edit_task(task_id: int, task: CreateTask, db: Session):
  72. tasks = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
  73. if not tasks:
  74. return "notaskfound"
  75. tasks.Description = task.Description
  76. tasks.StartLocation = task.StartLocation
  77. tasks.EndLocation = task.EndLocation
  78. db.commit()
  79. db.refresh(tasks)
  80. return tasks
  81. def get_active_route_by_driver(driver_id: int, db: Session):
  82. driver = get_user_by_id(driver_id, "Driver", db)
  83. if not driver:
  84. return "notdriver"
  85. tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
  86. for task in tasks:
  87. if task.Status == "In Progress":
  88. return task
  89. return "noroute"
  90. def get_my_routes(driver_id: int, db: Session):
  91. driver = get_user_by_id(driver_id, "Driver", db)
  92. if not driver:
  93. return "notdriver"
  94. tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
  95. return tasks