def create_returnflight(departure, instant_write=True, make_serial=True): """ Creates an instance of ReturnFlight from a Departure instance. Args: departure: instance of Departure. instant_write: boolean, if True write to csv file and return boolean, else return ReturnFlight instance. make_serial: boolean, if True create new serial for instances, else don't create new serial. """ attribute_dict = departure.get_attributes() # Get destination ID dest = attribute_dict["arrivingAt"] # Flight time in minutes (int) flight_time = DBLayer.generic_search("Destinations.csv", "ID", dest, "flight_time") flight_time = int(flight_time[0]) if make_serial: # Find last serial in csv file and increment serial = int( DBLayer.generic_search("RoundTrips.csv", "valid", "True", "flightID")[-1]) attribute_dict["flightID"] = serial + 1 # Get departure time (iso format string) departure_time = attribute_dict["departure"] # Parse iso format string and add flight time minutes departure_time = dt.datetime.strptime(departure_time, "%Y-%m-%dT%H:%M:%S") arrival_time = departure_time + dt.timedelta(minutes=flight_time) attribute_dict["arrival"] = arrival_time.isoformat() departure.__init__(attribute_dict) # Prepare attribute dict for ReturnFlight class # Switch ID for departingFrom and ArrivingAt attribute_dict["departingFrom"] = attribute_dict["arrivingAt"] attribute_dict["arrivingAt"] = "KEF" # Add 1 hour wait time departure_time = arrival_time + dt.timedelta(hours=1) attribute_dict["departure"] = departure_time.isoformat() # Increment serial attribute_dict["flightID"] = str(int(attribute_dict["flightID"]) + 1) # Calculate arrival time back to KEF arrival_time = departure_time + dt.timedelta(minutes=flight_time) attribute_dict["arrival"] = arrival_time.isoformat() ret_flight = ReturnFlight(attribute_dict) if instant_write: finished = Create.create_rtrip(departure, ret_flight) return finished else: return ret_flight
def read_rtrip(filter_column="valid", key_word="True"): """ Looks in rtrip csv file for rows matching given conditions. Returns list of rows in round trip file. """ attribute_dict_list = DBLayer.generic_search('RoundTrips.csv', filter_column, key_word) return attribute_dict_list
def replace_rtrip_row(departure, returnflight): """ Function for updating rows in round trip csv file. Edits two lines instead of one. Args: departure: updated instance of Departure class returnflight: updated instance of ReturnFlight class """ new_dep_attributes = departure.get_attributes(for_csv=True) new_ret_attributes = returnflight.get_attributes(for_csv=True) old_departure_dict = DBLayer.generic_search( "RoundTrips.csv", "flightID", new_dep_attributes["flightID"])[0] old_returnflight_dict = DBLayer.generic_search( "RoundTrips.csv", "flightID", new_ret_attributes["flightID"])[0] bool1 = Update.replace_row(old_departure_dict, departure) bool2 = Update.replace_row(old_returnflight_dict, returnflight) return bool1 and bool2
def read_dest(filter_column="valid", key_word="True"): """ Looks in destination csv file for rows matching given conditions. Returns list of instances. """ attribute_dict_list = DBLayer.generic_search('Destinations.csv', filter_column, key_word) dest_list = [ ] for attribute_dict in attribute_dict_list: destination = Destination(attribute_dict) dest_list.append(destination) return dest_list
def read_staff(filter_column, key_word): """ Looks in staff csv file for rows matching given conditions. Returns list of instances. """ attribute_dict_list = DBLayer.generic_search('Staff.csv', filter_column, key_word) staff_list = [ ] for attribute_dict in attribute_dict_list: employee = Employee(attribute_dict) staff_list.append(employee) return staff_list
def read_airplane(filter_column="valid", key_word="True"): """ Looks in airplane csv file for rows matching given conditions. Returns list of instances. """ attribute_dict_list = DBLayer.generic_search('Airplanes.csv', filter_column, key_word) airplane_list = [ ] for attribute_dict in attribute_dict_list: airplane = Airplane(attribute_dict) airplane_list.append(airplane) return airplane_list
def update_rtrip_csv_file(): """ Goes through RoundTrips.csv, and sets "past" column to be True if flight date is before current day, False otherwise. Achieved by changing rows into model classes and writing file again. """ flight_data = DBLayer.get_csv_data("RoundTrips.csv") updated_data = [] outbound = True for flight_row in flight_data: if outbound: departure = Departure(flight_row, from_csv=True) updated_data.append(departure.get_attributes(for_csv=True)) outbound = False elif not outbound: returnflight = ReturnFlight(flight_row) updated_data.append(returnflight.get_attributes(for_csv=True)) outbound = True flights_updated_bool = DBLayer.write_csv_file("RoundTrips.csv", updated_data) return flights_updated_bool
def update_crew(departure, employee_list): crew_list = BLLayer.create_crew_members(departure, employee_list, instant_write=False) # ** Remove selected employees from other flights on same day ** dep_str = departure.get_attributes()["departure"][0:10] # Get flights on same day departures_on_date = Read.departures_on_date(dep_str) # Get ssns for all busy employees on given date and corresponding flightID busy_ssn_list = [] for departure in departures_on_date: flightID = departure.get_attributes()["flightID"] crew = Read.flight_crew(flightID) for member in crew: busy_ssn_list.append(member.get_attributes()["ssn"]) busy_ssn_list.append(member.get_attributes()["flightID"]) # Get employees which have been selected to man current departure and are also assigned another flight on same day busy_employees = [] for employee in employee_list: ssn = employee.get_attributes()["ssn"] if ssn in busy_ssn_list: flightID = busy_ssn_list[busy_ssn_list.index(ssn) + 1] employee_id_tuple = (employee, flightID) busy_employees.append(employee_id_tuple) # Function changes old flight assignment validity to False bool1 = DBLayer.invalidate_crew_members(busy_employees) # Replace crew for the current departure instance bool2 = Update.replace_crew(departure, crew_list) finished = bool1 and bool2 return finished
def form_input_check(state, instance): """ Checks if primary keys in each new form are unique. Also checks if other fields (such as airplane license) correspond with data in database (such as airplane names). """ attribute_dict = instance.get_attributes() if state == "new_employee": ssn_list = DBLayer.generic_search("Staff.csv", "valid", "True", result_column="ssn") if attribute_dict["ssn"] in ssn_list: attribute_dict["ssn"] = "Villa" airplane_types = DBLayer.generic_search("Airplanes.csv", "valid", "True", result_column="type") if attribute_dict["license"] not in airplane_types: attribute_dict["license"] = "Villa" elif state == "new_airplane": airplane_names = DBLayer.generic_search("Airplanes.csv", "valid", "True", result_column="name") if attribute_dict["name"] in airplane_names: attribute_dict["name"] = "Villa" elif state == "new_dest": dest_id_list = DBLayer.generic_search("Destinations.csv", "valid", "True", result_column="ID") if attribute_dict["ID"] in dest_id_list: attribute_dict["ID"] = "Villa" elif state == "new_rtrip": departure = dt.datetime.strptime(attribute_dict["departure"], "%Y-%m-%dT%H:%M:%S") arrival_at_dest = dt.datetime.strptime(attribute_dict["arrival"], "%Y-%m-%dT%H:%M:%S") # Time when landing back home arrival = departure + 2 * (arrival_at_dest - departure) + dt.timedelta(hours=1) dest_id_list = DBLayer.generic_search("Destinations.csv", "valid", "True", result_column="ID") if attribute_dict[ "arrivingAt"] not in dest_id_list or attribute_dict[ "arrivingAt"] == "KEF": attribute_dict["arrivingAt"] = "Villa" airplane_names = DBLayer.generic_search("Airplanes.csv", "valid", "True", result_column="name") if attribute_dict["aircraft_name"] not in airplane_names: attribute_dict["aircraft_name"] = "Villa" else: airplane_available = BLLayer.available_airplane_check( attribute_dict["aircraft_name"], departure, arrival) if not airplane_available: attribute_dict["aircraft_name"] = "Villa" runway_empty = BLLayer.runway_check(departure) if not runway_empty: attribute_dict["departure"] = "Villa" instance.__init__(attribute_dict) return instance
def paging_system(state, display_data={}, aircraft_name=""): """ Creates a key,value pair in display_Data dict, key: "data", value: list of object instances. """ if state == "dest_list": data = Read.read_dest("valid", "True") elif state == "staff_list": data = Read.read_staff("valid", "True") # If only showing staff allowed to fly a certain airplane if aircraft_name != "": qualified_staff = [] airplane_type = DBLayer.generic_search("Airplanes.csv", "name", aircraft_name, result_column="type")[0] for employee in data: aircraft_license = employee.get_attributes()["license"] if aircraft_license == airplane_type: qualified_staff.append(employee) # Only show qualified staff for given airplane type data = qualified_staff elif state == "airplane_list": data = Read.read_airplane("valid", "True") elif state == "employee_list": data = Read.read_staff("valid", "True") elif state == "non_busy_staff": display_data, data = BLLayer.filter_busy_staff(display_data) elif state == "busy_staff": display_data, data = BLLayer.filter_busy_staff(display_data, busy=True) elif state == "rtrip_list": attribute_dict_list = Read.read_rtrip("valid", "True") data = BLLayer.create_rtrip_list(attribute_dict_list) elif state == "employee_schedule": display_data = BLLayer.create_schedule(display_data) display_data["page_size"] = len(display_data["data"]) display_data["end"] = len(display_data["data"]) return display_data display_data["data"] = data # If showing dates in pager show in weeks if state == "date_list": display_data["page_size"] = 7 display_data["rtrip"] = False # Show only 4 round trips in pager elif state == "rtrip_list": display_data["page_size"] = 4 display_data["rtrip"] = True # Default number of entries in pager is 5 else: display_data["page_size"] = 5 display_data["rtrip"] = False display_data["end"] = len(display_data["data"]) return display_data