예제 #1
0
 def validationPassword(self, result: tuple):
     if result != None:
         log.debug(f"User {self.username} logged succesfully")
         return 3
     else:
         log.error(f"Password incorrect to user {self.username}")
         return -3
예제 #2
0
 def login(self):
     try:
         with CursorPool() as cursor:
             values: tuple = (self.username, )
             cursor.execute(self._LOGIN_USERNAME, values)
             result_login_username: tuple = cursor.fetchone()
             log.debug(result_login_username)
             validateUser = self.validationUsername(
                 result=result_login_username)
             if validateUser == self._STATUS_USERNAME_OK:
                 newValues: tuple = (self.username, self.password)
                 cursor.execute(self._LOGIN, newValues)
                 result_login: tuple = cursor.fetchone()
                 validatePassword = self.validationPassword(
                     result=result_login)
                 if validatePassword == self._STATUS_PASSWORD_OK:
                     user: User = User(result_login[0], result_login[1],
                                       result_login[2], result_login[3],
                                       result_login[4], result_login[5])
                     isAdmin: bool = user.isAdmin
                     return isAdmin
                 else:
                     return self._STATUS_PASSWORD_FAILED
             else:
                 return self._STATUS_USERNAME_FAILED
     except Exception as e:
         log.error(f"Error happened while login user {self.username}: {e}")
예제 #3
0
 def getConnection(cls):
     try:
         connection = cls.getPool().getconn()
         log.debug(f"Connection obtained: {connection}")
         return connection
     except Exception as e:
         log.error(f"Error happened while getting connection: {e}")
         sys.exit()
예제 #4
0
 def deleteBus(cls,bus:Bus):
     try:
         with CursorPool() as cursor:
             values:tuple = (bus.bus_plate,)
             cursor.execute(cls._DELETE,values)
             log.debug(f"Bus {bus.bus_plate} deleted succesfully")
             return cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while deleting bus: {e}")
예제 #5
0
 def insertPlace(cls,place:PlaceToTravel):
     try:
         with CursorPool() as cursor:
             values:tuple = (place.namePlace,)
             cursor.execute(cls._INSERT,values)
             log.debug(f"Place inserted: {place.namePlace}")
             return cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while inserting place: {e}")
예제 #6
0
 def deleteUser(cls, user: User):
     try:
         with CursorPool() as cursor:
             values: tuple = (user.idUser, )
             cursor.execute(cls._DELETE, values)
             log.debug(f"User deleted: {user}")
             return cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while deleting user: {e}")
예제 #7
0
 def updatePlace(cls,place:PlaceToTravel):
     try:
         with CursorPool() as cursor:
             values:tuple = (place.namePlace,place.idPlace)
             cursor.execute(cls._UPDATE,values)
             log.debug(f"Place updated: {place}")
             cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while updating place: {e}")
예제 #8
0
 def insertTerminal(cls,terminal:Terminal):
     try:
         with CursorPool() as cursor:
             values:tuple = (terminal.name,terminal.place,cls.asignNumberTerminal(terminal))
             cursor.execute(cls._INSERT,values)
             log.debug(f"Terminal inserted: {terminal.name}")
             return cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while inserting terminal {terminal.name}: {e}")
예제 #9
0
 def updateTerminal(cls,terminal:Terminal):
     try:
         with CursorPool() as cursor:
             values:tuple = (terminal.name,terminal.place,cls.asignNumberTerminal(terminal),terminal.idTerminal)
             cursor.execute(cls._UPDATE,values)
             log.debug(f"Terminal {terminal.name} updated succesfully")
             cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while updating terminal {terminal.name}: {e}")
예제 #10
0
 def deleteTerminal(cls,terminal:Terminal):
     try:
         with CursorPool() as cursor:
             values:tuple = (terminal.idTerminal,)
             cursor.execute(cls._DELETE,values)
             log.debug(f"Terminal {terminal.name} (id: {terminal.idTerminal} deleted succesffuly")
             cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while deleting terminal {terminal.name}: {e}")
예제 #11
0
 def __enter__(self):
     try:
         self._connection = Connection.getConnection()
         self._cursor = self._connection.cursor()
         log.debug(
             f"Connection and Cursor obtained succesfully: {self._cursor}")
         return self._cursor
     except Exception as e:
         log.error(f"Error happened while getting Cursor: {e}")
예제 #12
0
 def updateUser(cls, user: User):
     try:
         with CursorPool() as cursor:
             values: tuple = (user.username, user.password, user.name,
                              user.lastName, user.idUser)
             cursor.execute(cls._UPDATE, values)
             log.debug(f"User updated: {user}")
             return cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while updating user: {e}")
예제 #13
0
 def insertUser(cls, user: User):
     try:
         with CursorPool() as cursor:
             values: tuple = (user.username, user.password, user.name,
                              user.lastName, user.isAdmin)
             cursor.execute(cls._INSERT, values)
             log.debug(f"User registred: {user}")
             return cursor.rowcount
     except Exception as e:
         log.error(f"Error happened while inserting user: {e}")
예제 #14
0
 def obtenerCursor(cls):
     if cls._cursor is None:
         try:
             cls._cursor = cls.obtenerConexion().cursor()
             log.debug(f'Cursor exitoso: {cls._cursor}')
             return cls._cursor
         except Exception as e:
             log.error(f"Ocurrió una exception al obtener el cursor: {e}")
             sys.exit()
     else:
         return cls._cursor
예제 #15
0
 def __exit__(self, typeException, valueException, traceback):
     if valueException:
         self._connection.rollback()
         log.error(
             f"Error happened while executing query. We do rollback. Value Exception: {valueException}"
         )
     else:
         self._connection.commit()
         log.debug(f"Commit running")
     self._cursor.close()
     Connection.unlockConnection(self._connection)
예제 #16
0
 def terminalsByPlace(cls,terminal:Terminal) -> int:
     try:
         with CursorPool() as cursor:
             values:tuple = (terminal.place,)
             cursor.execute(cls._SELECT_TERMINAL_BY_PLACE,values)
             registers:tuple = cursor.fetchall()
             quantity:int = len(registers)
             log.debug(f"Terminals in this place ({terminal.place}): {quantity}")
             return quantity
     except Exception as e:
         log.error(f"Error happened while searching quantity terminals by place: {e}")
예제 #17
0
 def __exit__(self, tipo_excepcion, valor_excepcion, detalle_excepcion):
     log.debug('Se ejecuta método __exit__')
     if valor_excepcion:
         self._conexion.rollback()
         log.error(
             f'Ocurrió una excepción, se hace rollback: {valor_excepcion} {tipo_excepcion} {detalle_excepcion}'
         )
     else:
         self._conexion.commit()
         log.debug('Commit de la transacción')
     self._cursor.close()
     Conexion.liberarConexion(self._conexion)
예제 #18
0
 def insertBus(cls,bus:Bus):
     try:
         quantity_bus_by_terminal:int = cls.verifyQuantBusByTerminal(bus.terminal_id)
         if quantity_bus_by_terminal < 2:
             with CursorPool() as cursor:
                 values:tuple = (bus.bus_plate,bus.capacity,bus.terminal_id)
                 cursor.execute(cls._INSERT,values)
                 log.debug(f"Bus {bus.bus_plate} registered succesfully")
                 return cursor.rowcount
         else:
             log.error(f"Error happened while inserting bus. Exist {quantity_bus_by_terminal} buses on terminal with id {bus.terminal_id}")
     except Exception as e:
         log.error(f"Error happened while inserting bus: {e}")
예제 #19
0
 def obtenerConexion(cls):
     if cls._conexion is None:
         try:
             cls._conexion = db.connect(host=cls._HOST,
                                        user=cls._USERNAME,
                                        password=cls._PASSWORD,
                                        port=cls._PORT,
                                        database=cls._DATABASE)
             log.debug(f'Conexion exitosa: {cls._conexion}')
             return cls._conexion
         except Exception as e:
             log.error(f"Ocurrió una exception: {e}")
             sys.exit()
     else:
         return cls._conexion
예제 #20
0
 def getPool(cls):
     if cls._pool == None:
         try:
             cls._pool = pool.SimpleConnectionPool(cls._MIN_CON,
                                                   cls._MAX_CON,
                                                   host=cls._HOST,
                                                   user=cls._USERNAME,
                                                   password=cls._PASSWORD,
                                                   port=cls._DB_PORT,
                                                   database=cls._DATABASE)
             log.debug(f"Pool getted: {cls._pool}")
             return cls._pool
         except Exception as e:
             log.error(f"Error happenned while getting pool: {e}")
     else:
         return cls._pool
예제 #21
0
 def verifyQuantBusByTerminal(cls,terminal_id:int):
     try:
         with CursorPool() as cursor:
             values:tuple = (terminal_id,)
             cursor.execute(cls._COUNT_TERMINAL,values)
             result:tuple = cursor.fetchone()
             quantity:int = result[0]
             if quantity == 0:
                 log.debug(f"Don't exist bus on terminal with id:{terminal_id}")
             elif quantity == 1:
                 log.debug(f"Exist {quantity} terminal on terminal with id: {terminal_id}")
             else:
                 log.debug(f"Exist {quantity} terminals on terminal with id: {terminal_id}")
             return quantity
     except Exception as e:
         log.error(f"Error happened while verifying quantity bus by terminal: {e}")
예제 #22
0
 def getAllBuses(cls):
     try:
         with CursorPool() as cursor:
             buses:list = []
             cursor.execute(cls._SELECT)
             registers:tuple = cursor.fetchall()
             for register in registers:
                 bus:Bus = Bus(
                     bus_plate=register[0],
                     capacity=register[1],
                     terminal_id=register[2]
                 )
                 buses.append(bus)
                 log.debug(bus)
             return buses              
     except Exception as e:
         log.error(f"Error happened while getting all buses: {e}")
예제 #23
0
 def getAllPlaces(cls):
     try:
         with CursorPool() as cursor:
             places:list = []
             cursor.execute(cls._SELECT)
             registers = cursor.fetchall()
             for register in registers:
                 place:PlaceToTravel = PlaceToTravel(
                     idPlace=register[0],
                     namePlace=register[1]
                 )
                 places.append(place)
                 log.debug(place)
             log.debug("Getting Places Succesfully")
             return places
     except Exception as e:
         log.error(f"Error happened while getting all places to travel: {e}")
예제 #24
0
 def getAllUsers(cls):
     try:
         with CursorPool() as cursor:
             users = []
             cursor.execute(cls._SELECT)
             registers = cursor.fetchall()
             for register in registers:
                 user: User = User(idUser=register[0],
                                   username=register[1],
                                   password=register[2],
                                   name=register[3],
                                   lastName=register[4])
                 log.debug(user)
             log.debug("Getting Users Succesfully")
             return users
     except Exception as e:
         log.error(f"Error happened while getting all users")
예제 #25
0
 def getAllTerminals(cls):
     try:
         with CursorPool() as cursor:
             terminals:list = []
             cursor.execute(cls._SELECT)
             registers:tuple = cursor.fetchall()
             for register in registers:
                 terminal:Terminal = Terminal(
                     idTerminal=register[0],
                     name=register[1],
                     place=register[2],
                     number=register[3]
                 )
                 terminals.append(terminal)
                 log.debug(terminal)
             log.debug("Getting Terminals Succesfully")
             return terminals
     except Exception as e:
         log.error(f"Error while getting terminals: {e}")
예제 #26
0
 def asignNumberTerminal(cls,terminal:Terminal) -> int or bool:
     try:
         quantity:int = cls.terminalsByPlace(terminal)
         if quantity == 0:
             return quantity+1
         elif quantity == 1:
             with CursorPool() as cursor:
                 values:tuple = (terminal.place,)
                 cursor.execute(cls._SELECT_TERMINAL_BY_PLACE,values)
                 register:tuple = cursor.fetchone()
                 number:int = register[3]
                 if number == 1:
                     return number+1
                 elif number == 2:
                     return number-1
                 else:
                     return False                
     except Exception as e:
         log.error(f"Error happened while asigning number terminal: {e}")
예제 #27
0
 def validationUsername(self, result: tuple):
     if result != None:
         return 2
     else:
         log.error(f"Username is not correct")
         return -2
예제 #28
0
 busOption = None
 while busOption != 5:
     busOption: int = int(input(menuBus))
     if busOption == 1:
         BusDao.getAllBuses()
     if busOption == 2:
         status: bool = False
         capacity = None
         while status == False:
             capacity: int = int(
                 input("Enter bus's capacity: "))
             if capacity > 0 and capacity <= 36:
                 status = True
             else:
                 log.error(
                     f"Capacity should to be into 0 to 36"
                 )
         TerminalDao.getAllTerminals()
         terminal: int = int(
             input("Input terminal id: "))
         bus: Bus = Bus(capacity=capacity,
                        terminal_id=terminal)
         BusDao.insertBus(bus)
     if busOption == 3:
         BusDao.getAllBuses()
         bus_plate = input(
             "Write bus plate to update: ")
         capacity = None
         status: bool = False
         while status == False:
             capacity: int = int(
예제 #29
0
 def unlockConnection(cls, connection):
     try:
         cls.getPool().putconn(connection)
         log.debug(f"Unlocked Connection: {connection}")
     except Exception as e:
         log.error(f"Error happened while Unlocked Connection: {e}")
예제 #30
0
 def closeAllConnection(cls):
     try:
         cls.getPool().closeall()
         log.debug(f"Closed all Connections Susccesfully!")
     except Exception as e:
         log.error(f"Error happened while Close All Connections: {e}")