def show_available_staff(form_data): """ Returns list of staff not flying on departure date.""" dep_str = form_data["instance"].get_attributes()["departure"][0:10] departure_list = Read.departures_on_date(dep_str) flightID_list = [] for departure in departure_list: flightID_list.append(departure.get_attributes()["flightID"]) busy_ssn_list = [] for flightID in flightID_list: crew_list = Read.flight_crew(flightID) for crew_row in crew_list: busy_ssn_list.append(crew_row.get_attributes()["ssn"]) all_staff = Read.read_staff("valid", "True") data = [] for employee in all_staff: if employee.get_attributes()["ssn"] not in busy_ssn_list: data.append(employee) form_data["data"] = data form_data["page_size"] = 7 form_data["rtrip"] = False return form_data
def filter_busy_staff(display_data, busy=False): # If no date set, use today as starting point if display_data["datetime"] == "": today = dt.datetime.today() display_data["datetime"] = dt.datetime(today.year, today.month, today.day) else: if display_data["action"] == "next_date": display_data["datetime"] = display_data[ "datetime"] + dt.timedelta(days=1) display_data["action"] = "" elif display_data["action"] == "prev_date": display_data["datetime"] = display_data[ "datetime"] - dt.timedelta(days=1) display_data["action"] = "" if busy: display_data["destinations"] = {} departure_list = Read.departures_on_date( display_data["datetime"].isoformat()[0:10]) ssn_list = [] for departure in departure_list: crew_list = Read.flight_crew( departure.get_attributes()["flightID"]) for crew in crew_list: ssn_list.append(crew.get_attributes()["ssn"]) if busy: key = crew.get_attributes()["ssn"] display_data["destinations"][key] = departure read_data = Read.read_staff("valid", "True") data = [] if not busy: for employee in read_data: if employee.get_attributes()["ssn"] not in ssn_list: data.append(employee) else: for employee in read_data: if employee.get_attributes()["ssn"] in ssn_list: data.append(employee) return display_data, data
def create_schedule(display_data): """ Creates one week from last monday, finds flights in week Returns list of flights if an employees ssn is found to match in Crew csv. """ display_data["data"] = [] if "week" in display_data: last_monday = dt.datetime.today().date( ) + display_data["week"] * dt.timedelta(days=7) else: last_monday = dt.datetime.today().date() display_data["week"] = 0 while last_monday.weekday() != 0: last_monday = last_monday - dt.timedelta(days=1) current_week = [] week_day = last_monday # Get YYYY-MM-DD strings for each day in week, starting on last monday for _ in range(7): current_week.append(week_day.isoformat()[0:10]) week_day = week_day + dt.timedelta(days=1) # Find flights on each day in given week, add to week_flights list week_flights = [] for date in current_week: departures = Read.departures_on_date(date) week_flights.append(departures) # If employee's ssn matches one found in crew for a departure, add departure and returning flight to employee's schedule staff_schedule = [] for departure_list in week_flights: for departure in departure_list: crew_row_list = Read.flight_crew( departure.get_attributes()["flightID"]) for crew_row in crew_row_list: ssn = crew_row.get_attributes()["ssn"] if ssn == display_data["instance"].get_attributes()["ssn"]: returnflight = BLLayer.create_returnflight( departure, instant_write=False, make_serial=False) rtrip = RTrip(departure, returnflight) staff_schedule.append(rtrip) display_data["data"] = staff_schedule display_data["datetime"] = last_monday display_data["week_end"] = week_day - dt.timedelta(days=1) return display_data
def available_airplane_check(airplane, departure_time, arrival_time): """ Checks if airplane is in use in another flight at same time as newly created flight. Args: airplane: airplane name departure_time: datetime for when departing from KEF arrival_time: datetime for when arriving back in KEF """ day_of = departure_time.isoformat()[0:10] day_before = (departure_time - dt.timedelta(days=1)).isoformat()[0:10] departures_on_date = [] departures_on_date.append(Read.departures_on_date(day_of)) departures_on_date.append(Read.departures_on_date(day_before)) same_airplane_rtrips = [] for dep in departures_on_date: if dep.get_attributes()["aircraft_name"] == airplane: ret = BLLayer.create_returnflight(dep, instant_write=False, make_serial=False) rtrip = RTrip(dep, ret) same_airplane_rtrips.append(rtrip) no_overlap = True if len(same_airplane_rtrips) > 0: for rtrip in same_airplane_rtrips: # Get start and end of round trip flight_start = dt.datetime.strptime(rtrip.flight_start(), "%Y-%m-%dT%H:%M:%S") flight_end = dt.datetime.strptime(rtrip.flight_end(), "%Y-%m-%dT%H:%M:%S") # If round trip starts more than an hour after the airplane has arrived, no overlap if flight_start > arrival_time + dt.timedelta(hours=1): no_overlap = True # If round trip ends more than an hour before the flight, no overlap elif flight_end + dt.timedelta(hours=1) < departure_time: no_overlap = True else: no_overlap = False break return no_overlap
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 runway_check(new_dep_time): """ Checks if new planned departure takes off within 10 minutes of another departure.""" dep_str = new_dep_time.isoformat()[0:10] same_day_dep_list = Read.departures_on_date(dep_str) runway_empty = True for departure in same_day_dep_list: old_dep_time = dt.datetime.strptime( departure.get_attributes()["departure"], "%Y-%m-%dT%H:%M:%S") if old_dep_time - new_dep_time < dt.timedelta( minutes=10) or new_dep_time - old_dep_time < dt.timedelta( minutes=10): runway_empty = False return runway_empty
def create_rtrip_list(attribute_dict_list): """ Creates list of flights for Pager, marks departing flight as manned or unmanned and returns list. """ rtrip_list = [] count = 0 for attribute_dict in attribute_dict_list: if attribute_dict["direction"] == "outbound": departure = Departure(attribute_dict, from_csv=True) manned_status = False head_pilot = False assistant_pilot = False head_steward = False crew = Read.flight_crew(attribute_dict["flightID"]) for member in crew: rank = member.get_rank() if rank == "Yfirflugmaður": head_pilot = True elif rank == "Aðstoðarflugmaður": assistant_pilot = True elif rank == "Yfirflugþjónn": head_steward = True manned_status = head_pilot and assistant_pilot and head_steward if manned_status: break departure.set_manned(manned_status) count += 1 elif attribute_dict["direction"] == "inbound": returnflight = ReturnFlight(attribute_dict) count += 1 # Collects two rows, creates Departure and ReturnFlight instances in order # Creates RTrip instance containing the two connected instances if count == 2: rtrip = RTrip(departure, returnflight) count = 0 rtrip_list.append(rtrip) return rtrip_list
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