コード例 #1
0
 def __init__(self):
     self.data = IOAPI()
コード例 #2
0
class EmployeeLL:
    def __init__(self):
        self.data = IOAPI()

# Method to register a new employee

    def RegisterEmployee(self, name_str, ssn_str, address_str, phone_str,
                         email_str, isPilot_bool, planeLicense_str):
        try:
            self.data.addEmployee(
                Employee(name_str, ssn_str, address_str, phone_str, email_str,
                         isPilot_bool, planeLicense_str))
            return 1  #Just a status code to say if the call was successfull, successfull if >= 0
        except EntryInDatabase:
            return -1

# Method to get an employee be its ssn

    def GetEmployeeBySSN(self, ssn):
        return self.data.getEmployeeBySSN(ssn)

# Method to get a list of all the pilots

    def ListPilots(self):
        employees = self.data.getAllEmployees()
        pilots = []
        for emp in employees:
            if emp.pilot_bool:
                pilots.append(emp)
        pilots.sort(key=lambda x: x.name)  # sort the lists based on name
        return pilots

# Method to get a list of all flight attendants

    def ListFlightAttendats(self):
        employees = self.data.getAllEmployees()
        flightAttendats = []
        for emp in employees:
            if not emp.pilot_bool:
                flightAttendats.append(emp)
        flightAttendats.sort(
            key=lambda x: x.name)  # sort the lists based on name
        return flightAttendats

# Method to list all the employees sorted by name and divided by position

    def ListAllEmployees(
            self):  # Lists all employees, first Pilots then flightAttendants
        pilots = self.ListPilots()
        flightAttendats = self.ListFlightAttendats()
        return pilots + flightAttendats

# Method to list all employees that have a name containing the input

    def ListAllEmployeesWithName(self, name):
        employees = self.data.getAllEmployees()
        ret_lst = []
        for emp in employees:
            if name in emp.name:
                ret_lst.append(emp)
        if len(ret_lst) == 0:
            raise EntryNotInDatabase('There is no employee with that name')
        return ret_lst

# Method to update an employee

    def UpdateEmployeeInfo(self, employee):
        try:
            self.data.updateEmployee(employee)
            return 1
        except EntryNotInDatabase:
            return -1

# Method to list all unassigned employees in the system for a certain date

    def ListUnassignedEmployees(
        self, date_iso
    ):  # not implemeted, to be implemented once datetime slides appear
        dateParced = parse(date_iso)
        voyages = self.data.getAllVoyages()
        employees = self.data.getAllEmployees()
        unassigned_employees = []
        for emp in employees:
            assigned = False
            for voy in voyages:
                if emp.ssn in voy.pilots_lst or emp.ssn in voy.flightAttendants_lst:
                    parsedStartTime = parse(voy.departureTime)
                    parsedEndTime = parse(self._getEndTimeOfVoyage(voy))

                    # testing if the departure or the return date are on the tested date

                    assigned = assigned or (((dateParced.year == parsedStartTime.year and dateParced.month == parsedStartTime.month and dateParced.day == parsedStartTime.day)\
                        and (dateParced.year == parsedEndTime.year and dateParced.month == parsedEndTime.month and dateParced.day == parsedEndTime.day)))

            if not assigned:
                unassigned_employees.append(emp)
        unassigned_employees.sort(key=lambda x: x.name)
        unassigned_employees.sort(key=lambda x: x.pilot_bool, reverse=True)
        return unassigned_employees

# Method to list all assigned Employees

    def ListAssignedEmployees(self, date_iso):
        dateParced = parse(date_iso)
        voyages = self.data.getAllVoyages()
        employees = self.data.getAllEmployees()
        assigned_employees = []
        for emp in employees:
            assigned = False
            for voy in voyages:
                if emp.ssn in voy.pilots_lst or emp.ssn in voy.flightAttendants_lst:
                    parsedStartTime = parse(voy.departureTime)
                    parsedEndTime = parse(self._getEndTimeOfVoyage(voy))

                    # testing if the departure or the return date are on the tested date

                    assigned = assigned or ((dateParced.year == parsedStartTime.year and dateParced.month == parsedStartTime.month and dateParced.day == parsedStartTime.day)\
                        and (dateParced.year == parsedEndTime.year and dateParced.month == parsedEndTime.month and dateParced.day == parsedEndTime.day))

            if assigned:
                assigned_employees.append(emp)
        return assigned_employees

# Method to list all pilots with a priveledge to a certain aircraft model

    def ListPilotsWithAircraftPrivilege(self, aircraft_model):
        pilots = self.ListPilots()
        model_pilots = []
        for pil in pilots:
            if pil.planeType == aircraft_model:
                model_pilots.append(pil)
        model_pilots.sort(key=lambda x: x.name)
        return model_pilots

# Method to get the End time of a voyage

    def _getEndTimeOfVoyage(self, voyage):
        destination = self.data.getDestinationByDestinationID(
            voyage.destination)

        StartTime_dateTime = parse(voyage.departureTime)

        flightTimeHours = float(destination.flight_duration)
        flightTimeMinutes = (flightTimeHours % 1) * 60
        flightTimeSeconds = (flightTimeMinutes % 1) * 60
        flightTimeHours = int(flightTimeHours)
        flightTimeMinutes = int(flightTimeMinutes)
        flightTimeSeconds = int(flightTimeSeconds)
        endTime = StartTime_dateTime + relativedelta(
            hours=+(flightTimeHours * 2 + 1),
            minutes=+2 * flightTimeMinutes,
            seconds=+2 * flightTimeSeconds)
        return endTime.isoformat()


# Method to get a work summary of an employee by ssn

    def GetWorkSummaryBySsn(self, apiSelf, employeeSSN, current_date):
        voyages_in_week = apiSelf.ListVoyagesForGivenWeek(current_date)
        emp_voyages = []

        for voyage in voyages_in_week:
            if employeeSSN in voyage.pilots_lst or employeeSSN in voyage.flightAttendants_lst:
                emp_voyages.append(voyage)

        return emp_voyages
コード例 #3
0
class VoyageLL:
    def __init__(self):
        self.data = IOAPI()

# Method to list all voyages in the system

    def ListAllVoyages(self):
        voyages = self.data.getAllVoyages()
        return voyages

# Method to list all upcoming voyages

    def ListUpcomingVoyages(self):
        voyages = self.data.getAllVoyages()
        nowParsed = datetime.now()
        upcomingVoyages = []
        for voy in voyages:
            parsedTime = parse(voy.departureTime)
            if parsedTime > nowParsed:
                upcomingVoyages.append(voy)
        upcomingVoyages.sort(key=lambda x: parse(x.departureTime))
        return upcomingVoyages

# Method to get a specifiv voyage by voyage id

    def getVoyageByVoyageID(self, voyageID):
        return self.data.getVoyageByVoyageID(voyageID)

# Method to add staff to a voyage

    def AddStaffToVoyage(self, voyageID, employeeSSN):

        voyage = self.data.getVoyageByVoyageID(voyageID)
        if voyage.aircraftID == '' or voyage.aircraftID == None:
            raise AircraftNotRegistered
        employee = self.data.getEmployeeBySSN(employeeSSN)
        try:
            aircraft = self.data.getAircraftByAircraftID(voyage.aircraftID)
        except EntryNotInDatabase:
            raise AircraftNotRegistered

        if str(employeeSSN) in voyage.pilots_lst or str(
                employeeSSN) in voyage.flightAttendants_lst:
            raise EmployeeAlreadyAssigned(
                'This employee is already assigned to this voyage')

        if employee.pilot_bool:
            if employee.planeType != aircraft.model:
                print('hi')
            voyage.pilots_lst.append(employeeSSN)
        else:
            voyage.flightAttendants_lst.append(employeeSSN)

        self.data.updateVoyage(voyage)

# Method to assign an aircraft to a voyage

    def assignAircraftToVoyage(self, voyageID, aircraftID):

        voyage = self.data.getVoyageByVoyageID(voyageID)
        voyage.aircraftID = aircraftID
        self.data.updateVoyage(voyage)

# Method to add voyage to the system

    def addVoyage(self, apiself, destination_id, flightTime_str):

        if not self._isDepartureTimeFree(flightTime_str):
            raise DepartureTimeOccupied(
                'There is already a flight at that time')

        freeEmployees = apiself.ListUnassignedEmployees(flightTime_str)
        freePilots = 0
        freeFlightAttendants = 0
        for emp in freeEmployees:
            if emp.pilot_bool:
                freePilots += 1
            else:
                freeFlightAttendants += 1

        if freePilots < 2 or freeFlightAttendants < 1:
            raise ToFewAvailableEmployees(
                "There are not enough available employees on this date to create a voyage"
            )

        flightIDs_lst_str = self._GenerateFlightID(destination_id,
                                                   flightTime_str)
        voyage = Voyage(self._GenerateNewVoyageID(),
                        destination_id,
                        flightTime_str,
                        outgoingFlightID=flightIDs_lst_str[0],
                        incomingFlightID=flightIDs_lst_str[1])
        self.data.addVoyage(voyage)

    # Method to add recuring voyages
    def AddRecurringVoyages(self, apiself, destination_id, flightTime_str,
                            dayInterval_int, quantity_int):
        parsedTime = parse(flightTime_str)
        deltaTime = relativedelta(days=+dayInterval_int)

        for _ in range(quantity_int):
            self.addVoyage(apiself, destination_id, parsedTime.isoformat())
            parsedTime = parsedTime + deltaTime
        return

# List all voyages for a given day

    def ListVoyagesForGivenDay(self, date_iso):
        voyages = self.data.getAllVoyages()
        voyagesOnDay_lst = []
        dateParced = parse(date_iso)
        for voy in voyages:
            parsedStartTime = parse(voy.departureTime)
            parsedEndTime = parse(self._getEndTimeOfVoyage(voy))

            if ((dateParced.year == parsedStartTime.year and dateParced.month == parsedStartTime.month and dateParced.day == parsedStartTime.day)\
                and (dateParced.year == parsedEndTime.year and dateParced.month == parsedEndTime.month and dateParced.day == parsedEndTime.day)):
                voyagesOnDay_lst.append(voy)
        return voyagesOnDay_lst

# list all voyages in the upcoming week

    def ListVoyagesForGivenWeek(
            self, date_iso):  # the input date is the first day in the week
        voyages = self.data.getAllVoyages()
        voyagesInWeek_lst = []
        dateParced = parse(date_iso)
        weekLaterParced = dateParced + relativedelta(days=+7)
        for voy in voyages:
            parsedStartTime = parse(voy.departureTime)
            parsedEndTime = parse(self._getEndTimeOfVoyage(voy))
            if (parsedEndTime > dateParced and parsedEndTime < weekLaterParced
                ) or (parsedStartTime > dateParced
                      and parsedStartTime < weekLaterParced):
                voyagesInWeek_lst.append(voy)
        return voyagesInWeek_lst

# List all pilots for a voyage

    def ListVoyagePilots(self, voyageID):
        voyage = self.data.getVoyageByVoyageID(voyageID)
        pilotsSSN_lst = voyage.pilots_lst
        pilots_emp = []
        for pilotSSN in pilotsSSN_lst:
            pilots_emp.append(self.data.getEmployeeBySSN(pilotSSN))
        return pilots_emp

# list all flightattendants for a voyage

    def ListVoyageFlightAttendants(self, voyageID):
        voyage = self.data.getVoyageByVoyageID(voyageID)
        flightAttendantsSSN_lst = voyage.flightAttendants_lst
        flightAttendats_emp = []
        for flightAttendatSSN in flightAttendantsSSN_lst:
            flightAttendats_emp.append(
                self.data.getEmployeeBySSN(flightAttendatSSN))
        return flightAttendats_emp

# update the voyage captain

    def UpdateVoyageCaptain(self, voyageID, pilotSSN):
        voyage = self.data.getVoyageByVoyageID(voyageID)
        voyage.captain = pilotSSN
        self.data.updateVoyage(voyage)

# update the head flight attendant for the voyage

    def UpdateVoyageHeadFlightAttendant(self, voyageID, flightAttendantSSN):
        voyage = self.data.getVoyageByVoyageID(voyageID)
        voyage.headFlightAttendant = flightAttendantSSN
        self.data.updateVoyage(voyage)

# sell seats for the voyage

    def SellSeatsForVoyageOutgoing(self, voyageID, soldSeats):
        voyage = self.data.getVoyageByVoyageID(voyageID)

        try:
            aircraft = self.data.getAircraftByAircraftID(voyage.aircraftID)
        except EntryNotInDatabase:
            raise AircraftNotRegistered(
                'The voyage does not have a Aircraft registered')
        if (int(voyage.seatingSoldOutgoing) + int(soldSeats)) > int(
                aircraft.total_seats_int):

            raise NotEnoughSeats("There are not enough available seats")
        else:
            voyage.seatingSoldOutgoing = int(
                voyage.seatingSoldOutgoing) + int(soldSeats)
            self.data.updateVoyage(voyage)
            return

# sell seats for the incoming flight of a voyage

    def SellSeatsForVoyageIncoming(self, voyageID, soldSeats):
        voyage = self.data.getVoyageByVoyageID(voyageID)
        aircraft = self.data.getAircraftByAircraftID(voyage.aircraftID)
        if (int(voyage.seatingSoldIncoming) + int(soldSeats)) > int(
                aircraft.total_seats_int):
            return -1
        else:
            voyage.seatingSoldIncoming = int(
                voyage.seatingSoldIncoming) + int(soldSeats)
            self.data.updateVoyage(voyage)
            return 1

# List all voyages for a given destination

    def ListVoyagesForDestination(self, dest_id):
        voyages = self.data.getAllVoyages()
        voyForDest = []

        for voy in voyages:
            if str(voy.destination) == str(dest_id):
                voyForDest.append(voy)
        return voyForDest

# returns weather the voyage has been fully staffed

    def IsFullyStaffed(self, voyage):
        return len(voyage.pilots_lst) > 1 and len(
            voyage.flightAttendants_lst) > 0

# Get the status of the voyage

    def GetVoyageStatus(self, voyage):
        voyageTimes = self._getTimeOfVoyageActivities(voyage)
        timeNow = datetime.now()
        if timeNow < voyageTimes[0]:
            return "Not Departed"
        if timeNow < voyageTimes[1]:
            return "In Transit To Dest"
        if timeNow < voyageTimes[2]:
            return "Idle At Destination"
        if timeNow < voyageTimes[3]:
            return "In Transit To Iceland"
        else:
            return "Voyage is Complete"

# Private Method to get all inner times of a voyage

    def _getTimeOfVoyageActivities(
        self, voyage
    ):  # returns list [<Departure from iceland>, <Arrival at destination>, <departure from destination>, <Arrival at Iceland>]
        destination = self.data.getDestinationByDestinationID(
            voyage.destination)

        try:
            StartTime_dateTime = datetime.strptime(voyage.departureTime,
                                                   '%Y-%m-%dT%H:%M:%S.%f')
        except ValueError:
            StartTime_dateTime = datetime.strptime(voyage.departureTime,
                                                   '%Y-%m-%dT%H:%M:%S')

        flightTime = float(
            destination.flight_duration
        )  # consider changing this to int so as to not miss the disimal places!
        deltaHours = flightTime
        deltaMinutes = (deltaHours % 1) * 60
        deltaSeconds = (deltaMinutes % 1) * 60
        deltaHours = int(deltaHours)
        deltaMinutes = int(deltaMinutes)
        deltaSeconds = int(deltaSeconds)

        DepartureFromIceland = parse(StartTime_dateTime.isoformat())
        ArrivalAtDestination = parse((StartTime_dateTime + relativedelta(
            hours=+(flightTime), minutes=+deltaMinutes,
            seconds=+deltaSeconds)).isoformat())
        DepartureFromDestination = parse(
            (StartTime_dateTime +
             relativedelta(hours=+(flightTime + 1),
                           minutes=+deltaMinutes,
                           seconds=+deltaSeconds)).isoformat())
        ArrivalAtIceland = parse(
            (StartTime_dateTime +
             relativedelta(hours=+(flightTime * 2 + 1),
                           minutes=+deltaMinutes * 2,
                           seconds=+deltaSeconds * 2)).isoformat())
        return [
            DepartureFromIceland, ArrivalAtDestination,
            DepartureFromDestination, ArrivalAtIceland
        ]  # Assuming the rest at destination is 1 hour

# private method to say weather a time is free for an new flight

    def _isDepartureTimeFree(self, flightTime_str_iso):
        voyages = self.data.getAllVoyages()
        parsedTime = parse(flightTime_str_iso)
        for voy in voyages:
            if parsedTime == parse(voy.departureTime):
                return False
        return True

# private method to generate a new flight id

    def _GenerateFlightID(self, destinationID, departureTime):
        outgoingFlightID_str = 'NA'
        incomingFlightID_str = 'NA'
        destinationID_str = str(destinationID)
        if len(destinationID_str) == 1:
            destinationID_str = '0' + destinationID_str
        outgoingFlightID_str += destinationID_str
        incomingFlightID_str += destinationID_str
        highestFlightID = self._HighestFlightID(destinationID, departureTime)
        outgoingFlightID_str += str(highestFlightID)
        incomingFlightID_str += str(highestFlightID + 1)
        return [outgoingFlightID_str, incomingFlightID_str]

# private methdo to find the hightest flight id

    def _HighestFlightID(
        self, destinationID, date_iso
    ):  # simply finds the flight on the desired day with the highest flight id so you can generate one that's higher
        voyages = self.data.getAllVoyages()
        parsedDate = parse(date_iso)
        highestID = 0
        for voy in voyages:
            if str(voy.destination) == str(destinationID):
                parsedVoyTime = parse(voy.departureTime)
                if parsedDate.year == parsedVoyTime.year and parsedDate.month == parsedVoyTime.month and parsedDate.day == parsedVoyTime.day:
                    if voy.incomingFlightID != None and voy.incomingFlightID != '' and int(
                            voy.incomingFlightID[-1]) > highestID:
                        highestID = int(voy.incomingFlightID[-1]) + 1
        return highestID

# private method to get teh end of the given voyage

    def _getEndTimeOfVoyage(self, voyage):
        destination = self.data.getDestinationByDestinationID(
            voyage.destination)
        # try:
        #     StartTime_dateTime = datetime.strptime(voyage.departureTime, '%Y-%m-%dT%H:%M:%S.%f')
        # except ValueError:
        #     StartTime_dateTime = datetime.strptime(voyage.departureTime, '%Y-%m-%dT%H:%M:%S')

        StartTime_dateTime = parse(voyage.departureTime)

        flightTimeHours = float(destination.flight_duration)
        flightTimeMinutes = (flightTimeHours % 1) * 60
        flightTimeSeconds = (flightTimeMinutes % 1) * 60
        flightTimeHours = int(flightTimeHours)
        flightTimeMinutes = int(flightTimeMinutes)
        flightTimeSeconds = int(flightTimeSeconds)
        # print(flightTime)
        endTime = StartTime_dateTime + relativedelta(
            hours=+(flightTimeHours * 2 + 1),
            minutes=+2 * flightTimeMinutes,
            seconds=+2 * flightTimeSeconds)
        return endTime.isoformat()


# private method to generate a new voyage id

    def _GenerateNewVoyageID(self):
        voyages = self.data.getAllVoyages()
        iteration = 1
        highest_id = 0
        for voy in voyages:
            if iteration <= 1:
                highest_id = int(voy.voyageID)
            else:
                if int(voy.voyageID) > highest_id:
                    highest_id = int(voy.voyageID)

            iteration += 1
        return highest_id + 1  # returns an integer that is 1 higher than the highest id any voyage has in the database
コード例 #4
0
ファイル: AircraftLL.py プロジェクト: eysta00/Group53
class AircraftLL:
    def __init__(self):
        self.data = IOAPI()

# Method to register a new employee

    def RegisterAircraft(self, manufacturer_str, model_str, totalseats_str):
        aircraft_id = self._GenerateNewAircraftID()
        self.data.addAircraft(
            Aircraft(aircraft_id, manufacturer_str, model_str, totalseats_str))

# Method to return a sorted list of all employees

    def ListAllAircrafts(self):
        aircrafts = self.data.getAllAircrafts()
        aircrafts.sort(key=lambda x: x.model)
        return aircrafts

# Method to show the status of all aircrafts

    def ShowStatusOfAircrafts(
            self):  # returns a list of tuples (aircraft, status)
        aircrafts = self.data.getAllAircrafts()
        ret_list = []
        time = datetime.now().isoformat()
        for air in aircrafts:
            ret_list.append((air, self.AircraftStatus(air.aircraftID, time)))
        return ret_list

# Method to see employee of specific employee

    def AircraftStatus(self, aircraftID, time_iso):
        aircraft = self.data.getAircraftByAircraftID(aircraftID)
        voyages = self.data.getAllVoyages()
        timeParsed = parse(time_iso)
        for voy in voyages:
            if str(voy.aircraftID) == str(aircraftID):

                voyageTimes = self._getTimeOfVoyageActivities(voy)
                if timeParsed > voyageTimes[0] and timeParsed < voyageTimes[3]:
                    if timeParsed > voyageTimes[2]:
                        return "Returning to Iceland"
                    elif timeParsed > voyageTimes[1]:
                        return "Idle at Destination"
                    else:
                        return "Flying to Destination"
        return "Unoccupied"

# Method to list all available aircrafts

    def ListAvailableAircrafts(self, time):
        aircrafts = self.data.getAllAircrafts()
        availableAircrafts = []
        for air in aircrafts:
            if self.AircraftStatus(air.aircraftID, time) == "Unoccupied":
                availableAircrafts.append(air)
        return availableAircrafts


# Method to generate a new unique ID for a aircraft

    def _GenerateNewAircraftID(self):
        aircrafts = self.data.getAllAircrafts()
        iteration = 1
        highest_id = 0
        for air in aircrafts:
            if iteration <= 1:
                print('aircraftID: ', air.aircraftID)
                highest_id = int(air.aircraftID)
            else:
                if int(air.aircraftID) > highest_id:
                    highest_id = int(air.aircraftID)

            iteration += 1
        return highest_id + 1  # returns an integer that is 1 higher than the highest id any voyage has in the database

    def _getTimeOfVoyageActivities(
        self, voyage
    ):  # returns list [<Departure from iceland>, <Arrival at destination>, <departure from destination>, <Arrival at Iceland>]
        destination = self.data.getDestinationByDestinationID(
            voyage.destination)

        try:
            StartTime_dateTime = datetime.strptime(voyage.departureTime,
                                                   '%Y-%m-%dT%H:%M:%S.%f')
        except ValueError:
            StartTime_dateTime = datetime.strptime(voyage.departureTime,
                                                   '%Y-%m-%dT%H:%M:%S')

        flightTime = float(destination.flight_duration)
        deltaHours = flightTime
        deltaMinutes = (deltaHours % 1) * 60
        deltaSeconds = (deltaMinutes % 1) * 60
        deltaHours = int(deltaHours)
        deltaMinutes = int(deltaMinutes)
        deltaSeconds = int(deltaSeconds)

        DepartureFromIceland = parse(StartTime_dateTime.isoformat())
        ArrivalAtDestination = parse((StartTime_dateTime + relativedelta(
            hours=+(flightTime), minutes=+deltaMinutes,
            seconds=+deltaSeconds)).isoformat())
        DepartureFromDestination = parse(
            (StartTime_dateTime +
             relativedelta(hours=+(flightTime + 1),
                           minutes=+deltaMinutes,
                           seconds=+deltaSeconds)).isoformat())
        ArrivalAtIceland = parse(
            (StartTime_dateTime +
             relativedelta(hours=+(flightTime * 2 + 1),
                           minutes=+deltaMinutes * 2,
                           seconds=+deltaSeconds * 2)).isoformat())
        return [
            DepartureFromIceland, ArrivalAtDestination,
            DepartureFromDestination, ArrivalAtIceland
        ]  # Assuming the rest at destination is 1 hour
コード例 #5
0
ファイル: LLAPI.py プロジェクト: vesteinnbjarna/NaNAir
 def __init__(self):
     self.__ioAPI = IOAPI()
     self.destLL = DestinationLL(self.__ioAPI)
     self.voyLL = VoyageLL(self.__ioAPI)
     self.plaLL = PlaneLL(self.__ioAPI)
     self.empLL = EmployeeLL(self.__ioAPI)