Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. from object_classes.flight_creator import Flight
  2. import datetime
  3.  
  4. class Voyage:
  5.  
  6. def __init__(self, flight_out=None, flight_in=None):
  7. self.__flight_out = flight_out or Flight()
  8. self.__flight_in = flight_in or Flight()
  9. self.valid_voyage = None
  10.  
  11. def is_valid(self):
  12. '''Function to check if the voyage is valid'''
  13. self.__flight_out.is_valid()
  14. valid, invalid_list = self.__flight_in.is_valid() # saves if the flight is valid or not, if it is not valid, saves what is wrong
  15. return (valid, invalid_list)
  16.  
  17.  
  18. def edit(self, object_type, item):
  19. '''Function used by the ui to add pilots, or crew to the flight'''
  20. for element in item:
  21. object_type = object_type.lower() # saves the type to edit
  22. worked1 = self.__flight_out.edit_staff(object_type, element) # saves if it worked
  23. worked2 = self.__flight_in.edit_staff(object_type, element)
  24. return worked1 and worked2 # returns true if both worked
  25.  
  26. def edit_options(self):
  27. '''Used by the ui to know what he can edit'''
  28. return ["Pilot", "Crew"]
  29.  
  30.  
  31. def edit_airplane_ID(self, new_ID):
  32. '''Takes in an airplane id and replaces it'''
  33. self.__flight_out.edit_airplane(new_ID)
  34. self.__flight_in.edit_airplane(new_ID)
  35.  
  36.  
  37. def edit_destination(self, new_destination):
  38. '''Used by the ui to edit destinations in a flight'''
  39. where, rkv = new_destination
  40. self.__flight_out.edit_destination(where) # adds in where the airplane is going
  41. self.__flight_in.edit_destination(rkv)
  42. self.__flight_out.edit_departure(rkv)
  43. self.__flight_in.edit_departure(where)
  44.  
  45. def edit_departure_time(self, new_time):
  46. '''Takes in a departure time from the user, error checks it, then calculates the arrival time'''
  47. if len(new_time) == 16:
  48. try:
  49. datetime.datetime.strptime(new_time,"%Y-%m-%d,%H:%M") # checks if the time is valid
  50. except ValueError:
  51. return False
  52. arrival, time = self.__flight_out.edit_departure_time(new_time) # edits the departure time, returns the arrival
  53. departure_time = arrival + datetime.timedelta(hours=1) # adds one hour
  54. self.__flight_in.set_departure_time(departure_time) # sets the departure time for the flight home
  55. self.__flight_in.set_arrival_time(departure_time + datetime.timedelta(hours=time)) # calculates the time to go home
  56. return True
  57. return False
  58.  
  59. def get_staff(self, what_type):
  60. '''Returns the staff members of the flights'''
  61. return self.__flight_in.get_staff(what_type)
  62.  
  63. def get_date(self):
  64. '''Used to get the flight out from iceland date'''
  65. date1 = self.__flight_out.get_date()
  66. if type(date1) is str: # checks if the object is a string, if it is we need to add the seconds
  67. if len(date1) != 19:
  68. date1 += ":00" # add them
  69. date1 = datetime.datetime.strptime(date1,"%Y-%m-%dT%H:%M:%S") # creates the date time object
  70. return date1 # returns it
  71.  
  72.  
  73. def get_both_dates(self):
  74. '''Returns the flight in date and flight out date as objects'''
  75. date1 = self.__flight_out.get_date()
  76. date2 = self.__flight_in.get_date()
  77. datelist = []
  78. ret_datelist = []
  79. if str(date1)[0:10] == str(date2)[0:10]: # removes the time from the dates, and checks if the dates are the same
  80. datelist.append(date1) # if the dates are the same, returns only one date
  81. else:
  82. datelist.append(date1) # if the dates are diffrent return both dates
  83. datelist.append(date2)
  84. for date in datelist: # turns the dates into datetime objects
  85. if type(date) is str:
  86. if len(date) != 19: # if the date is missing seconds add them
  87. date += ":00"
  88. date = datetime.datetime.strptime(date,"%Y-%m-%dT%H:%M:%S") # creates the datetime object
  89. ret_datelist.append(date)
  90. return ret_datelist # returns the date/s
  91.  
  92. def set_id(self, flight_id):
  93. '''Takes in an id and adds it to the out and in flight'''
  94. if len(flight_id) != 6:
  95. flight_id = flight_id[:-1] + "0" + flight_id[-1] # adding the missing 0 if the last number is in the range 0-9
  96. self.__flight_out.set_id(flight_id)
  97. in_id = flight_id[:-1] + str(int(flight_id[-2:])+ 1) # adding one to the end of the id
  98. self.__flight_in.set_id(in_id)
  99.  
  100.  
  101.  
  102. def get_destination(self):
  103. ''' Returns the flight destination'''
  104. return self.__flight_out.get_destination()
  105.  
  106. def get_type(self):
  107. ''' Returns the class type'''
  108. return "voyage"
  109.  
  110. def save(self):
  111. ''' Gets a save string the data layer uses to save the flights'''
  112. result_str = self.__flight_out.save() + "\n" + self.__flight_in.save()
  113. return result_str
  114.  
  115. def display_str(self):
  116. '''Display string that the ui uses to display the flights'''
  117. return '{:15} {} {:15} {}'.format(self.__flight_out.display_str(), "|---|",self.__flight_in.display_str(), self.__flight_in.is_valid()[0])
  118.  
  119. def display_str_edit(self):
  120. '''Used by ui edit'''
  121. return '{:20}'.format(self.__flight_out.display_str())
  122.  
  123. def get_search_reference(self):
  124. '''Returns a serch refrence for the flight'''
  125. return self.__flight_out.get_search_reference()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement