Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. import datetime
  2.  
  3. class Flight:
  4.  
  5. def __init__(self, flight_id=None, pilots=None, crew=None, departing_from=None, departure_date_time=None, destination=None, arrival_date_time=None, airplane=None):
  6. self.__flight_id = flight_id
  7. self.__pilots = pilots
  8. self.__crew = crew
  9. self.__departing_from = departing_from
  10. self.__departure_date_time = departure_date_time
  11. self.__destination = destination
  12. self.__arrival_date_time = arrival_date_time
  13. self.__airplane = airplane
  14.  
  15.  
  16. def calc_arrival_time(self):
  17. ''' Calculates the arrival time of the flight'''
  18. distance = self.__destination.get_distance()
  19. ###### WARNING HARD CODED SPEED VALUE ######
  20. time = int(distance) / 900
  21. ## TO DO, GIVE AIRPLANES SPEEED UNITS ##
  22. # Changing the time into a datetime object
  23. self.__departure_date_time = datetime.datetime.strptime(self.__departure_date_time,"%Y-%m-%dT%H:%M")
  24. self.__arrival_date_time = self.__departure_date_time + datetime.timedelta(hours=time) # calculation arrival time
  25. return time # retuns how long it takes to fly
  26.  
  27.  
  28.  
  29.  
  30. def is_valid(self):
  31. valid = True
  32. if self.__pilots and self.__crew:
  33. if len(self.__pilots) < 2:
  34. valid = False
  35. elif len(self.__crew) < 3:
  36. valid = False
  37. elif not self.__flight_id:
  38. valid = False
  39. elif not self.__departing_from:
  40. valid = False
  41. elif not self.__departure_date_time:
  42. valid = False
  43. elif not self.__destination:
  44. valid = False
  45. elif not self.__arrival_date_time:
  46. valid = False
  47. elif not self.__airplane:
  48. valid = False
  49. else:
  50. valid = False
  51. return valid
  52.  
  53.  
  54. def edit(self, object_type, item):
  55. if object_type == "Pilot" or object_type == "Crew":
  56. worked = self.edit_staff(object_type, item)
  57. return worked
  58.  
  59. def edit_options(self):
  60. '''Used by the ui to know what he can edit'''
  61. return ["Pilot", "Crew"]
  62.  
  63. def edit_staff(self, staff_type, staff):
  64. ''' Used by the ui to add new staff members to an existing voyage'''
  65. if staff_type == "Pilot":
  66. self.__pilots.append(staff)
  67. else:
  68. self.__crew.append(staff)
  69. return True
  70.  
  71. def edit_departure(self, new_departure):
  72. self.__departing_from = new_departure
  73.  
  74. def edit_destination(self, new_destination):
  75. self.__destination = new_destination
  76.  
  77. def edit_departure_time(self, new_time):
  78. '''Takes in a departure time from the user'''
  79. if len(new_time) == 16:
  80. if new_time[4] == '-' and new_time[7] == '-' and new_time[10] == ',' and new_time [13] == ':':
  81. self.__departure_date_time = new_time.replace(',', "T") # creates a time object
  82. time = self.calc_arrival_time() # gets the time the flight takes
  83. return (self.__arrival_date_time, time) # returns the time and arrival date
  84. return False
  85.  
  86. def set_departure_time(self, new_time):
  87. self.__departure_date_time = new_time
  88.  
  89. def set_arrival_time(self, time):
  90. self.__arrival_date_time = time
  91.  
  92.  
  93. def get_type(self):
  94. return "voyage"
  95.  
  96. def get_date(self):
  97. return self.__departure_date_time
  98.  
  99. def get_staff(self):
  100. return self.__pilots
  101.  
  102. def get_destination(self):
  103. return self.__destination
  104.  
  105. def get_search_reference(self):
  106. return self.__flight_id
  107.  
  108.  
  109. def save(self):
  110. if self.__pilots:
  111. pilots_str = ";".join(self.__pilots)
  112. else:
  113. pilots_str = None
  114. if self.__crew:
  115. crew_str = ";".join(self.__crew)
  116. else:
  117. crew_str = None
  118.  
  119. validity = self.is_valid()
  120. return "{},{},{},{},{},{},{},{}".format(self.__flight_id, self.__departing_from.get_airport_id(), self.__destination.get_airport_id(), self.__departure_date_time, \
  121. self.__arrival_date_time, pilots_str, crew_str, validity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement