import traceback from geruecht.exceptions import DatabaseExecption from geruecht.model.accessToken import AccessToken class Base: def getAccessToken(self, item): try: cursor = self.db.connection.cursor() if type(item) == str: sql = "select * from session where token='{}'".format(item) elif type(item) == int: sql = 'select * from session where id={}'.format(item) else: raise DatabaseExecption("item as no type int or str. name={}, type={}".format(item, type(item))) cursor.execute(sql) session = cursor.fetchone() retVal = AccessToken(session['id'], self.getUserById(session['user']), session['token'], session['lifetime'], session['timestamp'], browser=session['browser'], platform=session['platform']) if session != None else None return retVal except Exception as err: traceback.print_exc() self.db.connection.rollback() raise DatabaseExecption("Something went worng with Databes: {}".format(err)) def getAccessTokensFromUser(self, user): try: cursor = self.db.connection.cursor() cursor.execute("select * from session where user={}".format(user.id)) sessions = cursor.fetchall() retVal = [ AccessToken(session['id'], self.getUserById(session['user']), session['token'], session['lifetime'], session['timestamp'], browser=session['browser'], platform=session['platform']) for session in sessions] return retVal except Exception as err: traceback.print_exc() self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) def getAccessTokens(self): try: cursor = self.db.connection.cursor() cursor.execute("select * from session") sessions = cursor.fetchall() retVal = [AccessToken(session['id'], self.getUserById(session['user']), session['token'], session['lifetime'], session['timestamp'], browser=session['browser'], platform=session['platform']) for session in sessions] return retVal except Exception as err: traceback.print_exc() self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) def createAccessToken(self, user, token, lifetime, timestamp, lock_bar, user_agent=None): try: cursor = self.db.connection.cursor() cursor.execute("insert into session (user, timestamp, lock_bar, token, lifetime, browser, platform) VALUES ({}, '{}', {}, '{}', {}, '{}', '{}')".format(user.id, timestamp, lock_bar, token, lifetime, user_agent.browser if user_agent else 'NULL', user_agent.platform if user_agent else 'NULL')) self.db.connection.commit() return self.getAccessToken(token) except Exception as err: traceback.print_exc() self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) def updateAccessToken(self, accToken): try: cursor = self.db.connection.cursor() cursor.execute("update session set timestamp='{}', lock_bar={}, lifetime={} where id={}".format(accToken.timestamp, accToken.lock_bar, accToken.lifetime, accToken.id)) self.db.connection.commit() return self.getAccessToken(accToken.id) except Exception as err: traceback.print_exc() self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) def deleteAccessToken(self, accToken): try: cursor = self.db.connection.cursor() cursor.execute("delete from session where id={}".format(accToken.id)) self.db.connection.commit() except Exception as err: traceback.print_exc() self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))