68 lines
3.2 KiB
Python
68 lines
3.2 KiB
Python
|
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']) 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 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']) 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):
|
||
|
try:
|
||
|
cursor = self.db.connection.cursor()
|
||
|
cursor.execute("insert into session (user, timestamp, lock_bar, token, lifetime) VALUES ({}, '{}', {}, '{}', {})".format(user.id, timestamp, lock_bar, token, lifetime))
|
||
|
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))
|