Example #1
0
class Flight:
    def __init__(self):
        """
        return a new, empty flight object.
        """
        self.username = None
        self.flight_id = 0
        self.table_name = 'tb_' + str(self.flight_id)
        self.info_dict = dict()
        self.flight_type = None
        self.depart = None
        self.dest = None
        self.depart_date = None
        self.return_date = None
        self.filters = None
        self.notifications = None

    # store an object information in table general_info
    def __new_flight__(self,
                       flight_id: int,
                       username: str,
                       depart: str,
                       dest: str,
                       depart_date: str,
                       flight_type: str,
                       return_date: Optional[str] = None):
        """
        Creates a basic flight instance, its table, and its dict.
        :param flight_id:
        :param username:
        :param depart:
        :param dest:
        :param depart_date:
        :param flight_type:
        :param return_date:
        :return:
        COMMENT: need to increment flight_id by 1 in load_app
        """
        self.username = username
        self.flight_id = flight_id
        self.table_name = 'tb_' + str(flight_id)
        self.flight_type = flight_type
        self.depart = depart
        self.dest = dest
        self.depart_date = depart_date
        self.return_date = return_date

        # Create table and dicts
        mydb = mysql.connector.connect(host='localhost',
                                       user='******',
                                       passwd='flightplanner',
                                       database='FP_database')
        mycursor = mydb.cursor()

        set_up_command = 'CREATE TABLE {0} (Flight_id int, Date varchar(255), Min_eco int, Min_bus int, Avg_econ int,' \
                         ' Avg_bus int, Track_date varchar(255));'.format(self.table_name)
        mycursor.execute(set_up_command)
        mydb.commit()
        mycursor.close()

        self.info_dict = dict()

        self.filters = Filters()
        self.filters.require_filters()

        self.notifications = Notification()
        self.notifications.require_notifications()

    def __load_flight_dict__(self):
        """
        loads the flight's dictionary from flight table. Track_date as key in dict.
        :return: a flight object with loaded dict.
        """
        mydb = mysql.connector.connect(host='localhost',
                                       user='******',
                                       passwd='flightplanner',
                                       database='FP_database')
        mycursor = mydb.cursor()

        temp_dict = dict()
        mycursor.execute("SELECT * FROM {0};".format(self.table_name))
        data_list = mycursor.fetchall()

        for record in data_list:
            temp_dict[record[6]] = record[:6]

        self.info_dict = temp_dict
        mycursor.close()

    def commit_flight_db(self, min_eco: int, min_bus: int, avg_econ: int,
                         avg_bus: int):
        """
        Updates both flight table and flight dict using newly extracted info from web.
        :param min_eco:
        :param min_bus:
        :param avg_econ:
        :param avg_bus:
        :return:
        """
        mydb = mysql.connector.connect(host='localhost',
                                       user='******',
                                       passwd='flightplanner',
                                       database='FP_database')
        mycursor = mydb.cursor()
        record_command = "INSERT INTO {0} (Flight_id, Date, Min_eco, Min_bus, Avg_econ, Avg_bus, Track_date) VALUES" \
                         " ({1}, {2}, {3}, {4}, {5}, {6}, {7})".format(self.table_name, self.flight_id,
                                                                       self.depart_date, min_eco, min_bus, avg_econ,
                                                                       avg_bus, str(datetime.date.today()))
        mycursor.execute(record_command)
        mydb.commit()
        mycursor.close()

        # Now update the dict
        self.info_dict[str(
            datetime.date.today())] = (self.flight_id, self.depart_date,
                                       min_eco, min_bus, avg_econ, avg_bus)