2020-06-04 21:03:39 +00:00
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 ( )
2020-06-04 22:34:32 +00:00
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
2020-06-04 21:03:39 +00:00
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
2020-06-04 22:34:32 +00:00
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 ) )
2020-06-04 21:03:39 +00:00
def getAccessTokens ( self ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from session " )
sessions = cursor . fetchall ( )
2020-06-04 22:34:32 +00:00
retVal = [ AccessToken ( session [ ' id ' ] , self . getUserById ( session [ ' user ' ] ) , session [ ' token ' ] , session [ ' lifetime ' ] , session [ ' timestamp ' ] , browser = session [ ' browser ' ] , platform = session [ ' platform ' ] ) for session in sessions ]
2020-06-04 21:03:39 +00:00
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2020-06-04 22:34:32 +00:00
def createAccessToken ( self , user , token , lifetime , timestamp , lock_bar , user_agent = None ) :
2020-06-04 21:03:39 +00:00
try :
cursor = self . db . connection . cursor ( )
2020-06-04 22:34:32 +00:00
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 ' ) )
2020-06-04 21:03:39 +00:00
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 ) )