2019-12-19 07:12:29 +00:00
import pymysql
2019-12-28 20:52:49 +00:00
from . import Singleton
2020-01-19 20:32:58 +00:00
from geruecht import db
2019-12-19 07:12:29 +00:00
from geruecht . model . user import User
2019-12-19 17:26:41 +00:00
from geruecht . model . creditList import CreditList
2020-01-18 22:31:49 +00:00
from datetime import datetime , timedelta
2020-01-26 22:31:22 +00:00
from geruecht . exceptions import UsernameExistDB , DatabaseExecption
2020-01-23 22:27:26 +00:00
import traceback
2020-02-23 20:27:03 +00:00
from MySQLdb . _exceptions import IntegrityError
2019-12-19 07:12:29 +00:00
class DatabaseController ( metaclass = Singleton ) :
'''
DatabaesController
Connect to the Database and execute sql - executions
'''
2020-01-19 20:32:58 +00:00
def __init__ ( self ) :
self . db = db
2019-12-19 07:12:29 +00:00
2020-05-17 11:35:33 +00:00
def getAllUser ( self , extern = False , workgroups = True ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from user " )
data = cursor . fetchall ( )
2020-01-19 20:32:58 +00:00
2020-02-23 20:27:03 +00:00
if data :
retVal = [ ]
for value in data :
2020-03-04 20:11:41 +00:00
if extern and value [ ' uid ' ] == ' extern ' :
continue
2020-02-23 20:27:03 +00:00
user = User ( value )
creditLists = self . getCreditListFromUser ( user )
user . initGeruechte ( creditLists )
2020-05-17 11:35:33 +00:00
if workgroups :
user . workgroups = self . getWorkgroupsOfUser ( user . id )
2020-02-23 20:27:03 +00:00
retVal . append ( user )
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2019-12-19 17:26:41 +00:00
2020-05-17 11:35:33 +00:00
def getUser ( self , username , workgroups = True ) :
2020-02-23 20:27:03 +00:00
try :
retVal = None
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from user where uid= ' {} ' " . format ( username ) )
data = cursor . fetchone ( )
if data :
retVal = User ( data )
creditLists = self . getCreditListFromUser ( retVal )
retVal . initGeruechte ( creditLists )
2020-05-17 11:35:33 +00:00
if workgroups :
retVal . workgroups = self . getWorkgroupsOfUser ( retVal . id )
2020-02-23 20:27:03 +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-01-19 20:32:58 +00:00
2020-05-17 11:35:33 +00:00
def getUserById ( self , id , workgroups = True ) :
2020-02-23 20:27:03 +00:00
try :
retVal = None
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from user where id= {} " . format ( id ) )
data = cursor . fetchone ( )
if data :
retVal = User ( data )
creditLists = self . getCreditListFromUser ( retVal )
retVal . initGeruechte ( creditLists )
2020-05-17 11:35:33 +00:00
if workgroups :
retVal . workgroups = self . getWorkgroupsOfUser ( retVal . id )
2020-02-23 20:27:03 +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-01-18 22:31:49 +00:00
2019-12-22 21:27:39 +00:00
def _convertGroupToString ( self , groups ) :
retVal = ' '
2020-01-27 19:16:04 +00:00
print ( ' groups: {} ' . format ( groups ) )
if groups :
for group in groups :
if len ( retVal ) != 0 :
retVal + = ' , '
retVal + = group
2019-12-22 21:27:39 +00:00
return retVal
2019-12-19 07:12:29 +00:00
2020-01-19 20:32:58 +00:00
2019-12-28 22:34:09 +00:00
def insertUser ( self , user ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
groups = self . _convertGroupToString ( user . group )
cursor . execute ( " insert into user (uid, dn, firstname, lastname, gruppe, lockLimit, locked, autoLock, mail) VALUES ( ' {} ' , ' {} ' , ' {} ' , ' {} ' , ' {} ' , {} , {} , {} , ' {} ' ) " . format (
user . uid , user . dn , user . firstname , user . lastname , groups , user . limit , user . locked , user . autoLock , user . mail ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2020-01-19 20:32:58 +00:00
2019-12-28 22:34:09 +00:00
def updateUser ( self , user ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
print ( ' uid: {} ; group: {} ' . format ( user . uid , user . group ) )
groups = self . _convertGroupToString ( user . group )
sql = " update user set dn= ' {} ' , firstname= ' {} ' , lastname= ' {} ' , gruppe= ' {} ' , lockLimit= {} , locked= {} , autoLock= {} , mail= ' {} ' where uid= ' {} ' " . format (
user . dn , user . firstname , user . lastname , groups , user . limit , user . locked , user . autoLock , user . mail , user . uid )
print ( sql )
cursor . execute ( sql )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2020-01-19 20:32:58 +00:00
2019-12-19 17:26:41 +00:00
def getCreditListFromUser ( self , user , * * kwargs ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
if ' year ' in kwargs :
sql = " select * from creditList where user_id= {} and year_date= {} " . format ( user . id , kwargs [ ' year ' ] )
else :
sql = " select * from creditList where user_id= {} " . format ( user . id )
cursor . execute ( sql )
data = cursor . fetchall ( )
if len ( data ) == 1 :
return [ CreditList ( data [ 0 ] ) ]
else :
return [ CreditList ( value ) for value in data ]
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2019-12-19 17:26:41 +00:00
2020-01-19 20:32:58 +00:00
2019-12-19 17:26:41 +00:00
def createCreditList ( self , user_id , year = datetime . now ( ) . year ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " insert into creditList (year_date, user_id) values ( {} , {} ) " . format ( year , user_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 ) )
2020-01-19 20:32:58 +00:00
2019-12-19 17:26:41 +00:00
def updateCreditList ( self , creditlist ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from creditList where user_id= {} and year_date= {} " . format ( creditlist . user_id , creditlist . year ) )
data = cursor . fetchall ( )
if len ( data ) == 0 :
self . createCreditList ( creditlist . user_id , creditlist . year )
sql = " update creditList set jan_guthaben= {} , jan_schulden= {} ,feb_guthaben= {} , feb_schulden= {} , maer_guthaben= {} , maer_schulden= {} , apr_guthaben= {} , apr_schulden= {} , mai_guthaben= {} , mai_schulden= {} , jun_guthaben= {} , jun_schulden= {} , jul_guthaben= {} , jul_schulden= {} , aug_guthaben= {} , aug_schulden= {} ,sep_guthaben= {} , sep_schulden= {} ,okt_guthaben= {} , okt_schulden= {} , nov_guthaben= {} , nov_schulden= {} , dez_guthaben= {} , dez_schulden= {} , last_schulden= {} where year_date= {} and user_id= {} " . format ( creditlist . jan_guthaben , creditlist . jan_schulden ,
creditlist . feb_guthaben , creditlist . feb_schulden ,
creditlist . maer_guthaben , creditlist . maer_schulden ,
creditlist . apr_guthaben , creditlist . apr_schulden ,
creditlist . mai_guthaben , creditlist . mai_schulden ,
creditlist . jun_guthaben , creditlist . jun_schulden ,
creditlist . jul_guthaben , creditlist . jul_schulden ,
creditlist . aug_guthaben , creditlist . aug_schulden ,
creditlist . sep_guthaben , creditlist . sep_schulden ,
creditlist . okt_guthaben , creditlist . okt_schulden ,
creditlist . nov_guthaben , creditlist . nov_schulden ,
creditlist . dez_guthaben , creditlist . dez_schulden ,
creditlist . last_schulden , creditlist . year , creditlist . user_id )
print ( sql )
cursor = self . db . connection . cursor ( )
cursor . execute ( sql )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2020-01-19 20:32:58 +00:00
2020-01-18 22:31:49 +00:00
def getWorker ( self , user , date ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from bardienste where user_id= {} and startdatetime= ' {} ' " . format ( user . id , date ) )
data = cursor . fetchone ( )
return { " user " : user . toJSON ( ) , " startdatetime " : data [ ' startdatetime ' ] , " enddatetime " : data [ ' enddatetime ' ] , " start " : { " year " : data [ ' startdatetime ' ] . year , " month " : data [ ' startdatetime ' ] . month , " day " : data [ ' startdatetime ' ] . day } } if data else None
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2020-01-18 22:31:49 +00:00
def getWorkers ( self , date ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from bardienste where startdatetime= ' {} ' " . format ( date ) )
data = cursor . fetchall ( )
return [ { " user " : self . getUserById ( work [ ' user_id ' ] ) . toJSON ( ) , " startdatetime " : work [ ' startdatetime ' ] , " enddatetime " : work [ ' enddatetime ' ] , " start " : { " year " : work [ ' startdatetime ' ] . year , " month " : work [ ' startdatetime ' ] . month , " day " : work [ ' startdatetime ' ] . day } } for work in data ]
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2020-01-19 20:32:58 +00:00
2020-01-18 22:31:49 +00:00
def setWorker ( self , user , date ) :
2020-02-23 20:27:03 +00:00
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " insert into bardienste (user_id, startdatetime, enddatetime) values ( {} , ' {} ' , ' {} ' ) " . format ( user . id , date , date + timedelta ( days = 1 ) ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2020-01-19 20:32:58 +00:00
2020-01-18 22:31:49 +00:00
def deleteWorker ( self , user , date ) :
2020-01-23 22:27:26 +00:00
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from bardienste where user_id= {} and startdatetime= ' {} ' " . format ( user . id , date ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
2020-02-23 20:27:03 +00:00
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2019-12-19 17:26:41 +00:00
2020-01-26 22:31:22 +00:00
def changeUsername ( self , user , newUsername ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from user where uid= ' {} ' " . format ( newUsername ) )
data = cursor . fetchall ( )
if data :
raise UsernameExistDB ( " Username already exists " )
else :
cursor . execute ( " update user set uid= ' {} ' where id= {} " . format ( newUsername , user . id ) )
2020-02-23 20:27:03 +00:00
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
def getLockedDay ( self , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from locked_days where daydate= ' {} ' " . format ( date ) )
data = cursor . fetchone ( )
return data
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
def setLockedDay ( self , date , locked , hard = False ) :
try :
cursor = self . db . connection . cursor ( )
sql = " insert into locked_days (daydate, locked) VALUES ( ' {} ' , {} ) " . format ( date , locked )
cursor . execute ( sql )
self . db . connection . commit ( )
return self . getLockedDay ( date )
except IntegrityError as err :
self . db . connection . rollback ( )
try :
exists = self . getLockedDay ( date )
if hard :
sql = " update locked_days set locked= {} where id= {} " . format ( locked , exists [ ' id ' ] )
else :
sql = False
if sql :
cursor . execute ( sql )
self . db . connection . commit ( )
return self . getLockedDay ( date )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
2020-02-25 21:50:32 +00:00
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
2020-01-26 22:31:22 +00:00
except Exception as err :
traceback . print_exc ( )
2020-02-23 20:27:03 +00:00
self . db . connection . rollback ( )
2020-01-26 22:31:22 +00:00
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
2020-02-25 21:50:32 +00:00
def setTransactJob ( self , from_user , to_user , date ) :
try :
exists = self . getTransactJob ( from_user , to_user , date )
if exists :
raise IntegrityError ( " job_transact already exists " )
cursor = self . db . connection . cursor ( )
cursor . execute ( " insert into job_transact (jobdate, from_user_id, to_user_id) VALUES ( ' {} ' , {} , {} ) " . format ( date , from_user . id , to_user . id ) )
self . db . connection . commit ( )
return self . getTransactJob ( from_user , to_user , date )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Somethin went wrong with Database: {} " . format ( err ) )
def getTransactJob ( self , from_user , to_user , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from job_transact where from_user_id= {} and to_user_id= {} and jobdate= ' {} ' " . format ( from_user . id , to_user . id , date ) )
data = cursor . fetchone ( )
if data :
return { " from_user " : from_user , " to_user " : to_user , " date " : data [ ' jobdate ' ] , " answerd " : data [ ' answerd ' ] , " accepted " : data [ ' accepted ' ] }
return None
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Database: {} " . format ( err ) )
def getAllTransactJobFromUser ( self , from_user , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from job_transact where from_user_id= {} " . format ( from_user . id ) )
data = cursor . fetchall ( )
retVal = [ ]
for transact in data :
if date < = transact [ ' jobdate ' ] :
retVal . append ( { " from_user " : from_user , " to_user " : self . getUserById ( transact [ ' to_user_id ' ] ) , " date " : transact [ ' jobdate ' ] , " accepted " : transact [ ' accepted ' ] , " answerd " : transact [ ' answerd ' ] } )
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Somethin went wrong with Database: {} " . format ( err ) )
def getAllTransactJobToUser ( self , to_user , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from job_transact where to_user_id= {} " . format ( to_user . id ) )
data = cursor . fetchall ( )
retVal = [ ]
for transact in data :
if date < = transact [ ' jobdate ' ] :
retVal . append ( { " to_user " : to_user , " from_user " : self . getUserById ( transact [ ' from_user_id ' ] ) , " date " : transact [ ' jobdate ' ] , " accepted " : transact [ ' accepted ' ] , " answerd " : transact [ ' answerd ' ] } )
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Somethin went wrong with Database: {} " . format ( err ) )
def getTransactJobToUser ( self , to_user , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from job_transact where to_user_id= {} and jobdate= ' {} ' " . format ( to_user . id , date ) )
data = cursor . fetchone ( )
if data :
return { " from_user " : self . getUserById ( data [ ' from_user_id ' ] ) , " to_user " : to_user , " date " : data [ ' jobdate ' ] , " accepted " : data [ ' accepted ' ] , " answerd " : data [ ' answerd ' ] }
else :
return None
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Somethin went wrong with Database: {} " . format ( err ) )
def updateTransactJob ( self , from_user , to_user , date , accepted ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " update job_transact set accepted= {} , answerd=true where to_user_id= {} and jobdate= ' {} ' " . format ( accepted , to_user . id , date ) )
self . db . connection . commit ( )
return self . getTransactJob ( from_user , to_user , date )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Somethin went wrong with Database: {} " . format ( err ) )
def getTransactJobFromUser ( self , user , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from job_transact where from_user_id= {} and jobdate= ' {} ' " . format ( user . id , date ) )
data = cursor . fetchall ( )
return [ { " from_user " : user , " to_user " : self . getUserById ( transact [ ' to_user_id ' ] ) , " date " : transact [ ' jobdate ' ] , " accepted " : transact [ ' accepted ' ] , " answerd " : transact [ ' answerd ' ] } for transact in data ]
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Somethin went wrong with Database: {} " . format ( err ) )
def deleteTransactJob ( self , from_user , to_user , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from job_transact where from_user_id= {} and to_user_id= {} and jobdate= ' {} ' " . format ( from_user . id , to_user . id , date ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
2020-02-27 20:55:00 +00:00
def getPriceList ( self ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from pricelist " )
return cursor . fetchall ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
2020-03-01 18:20:47 +00:00
def getDrinkPrice ( self , name ) :
try :
cursor = self . db . connection . cursor ( )
if type ( name ) == str :
sql = " select * from pricelist where name= ' {} ' " . format ( name )
elif type ( name ) == int :
sql = ' select * from pricelist where id= {} ' . format ( name )
else :
raise DatabaseExecption ( " name as no type int or str. name= {} , type= {} " . format ( name , type ( name ) ) )
cursor . execute ( sql )
return cursor . fetchone ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
def setDrinkPrice ( self , drink ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute (
" insert into pricelist (name, price, price_big, price_club, price_club_big, premium, premium_club, price_extern_club, type) VALUES ( %s , %s , %s , %s , %s , %s , %s , %s , %s ) " ,
(
drink [ ' name ' ] , drink [ ' price ' ] , drink [ ' price_big ' ] , drink [ ' price_club ' ] , drink [ ' price_club_big ' ] ,
drink [ ' premium ' ] , drink [ ' premium_club ' ] , drink [ ' price_extern_club ' ] , drink [ ' type ' ] ) )
self . db . connection . commit ( )
return self . getDrinkPrice ( str ( drink [ ' name ' ] ) )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
def updateDrinkPrice ( self , drink ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " update pricelist set name= %s , price= %s , price_big= %s , price_club= %s , price_club_big= %s , premium= %s , premium_club= %s , price_extern_club= %s , type= %s where id= %s " ,
(
drink [ ' name ' ] , drink [ ' price ' ] , drink [ ' price_big ' ] , drink [ ' price_club ' ] , drink [ ' price_club_big ' ] , drink [ ' premium ' ] , drink [ ' premium_club ' ] , drink [ ' price_extern_club ' ] , drink [ ' type ' ] , drink [ ' id ' ]
) )
self . db . connection . commit ( )
return self . getDrinkPrice ( drink [ ' id ' ] )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
def deleteDrink ( self , drink ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from pricelist where id= {} " . format ( drink [ ' id ' ] ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Database: {} " . format ( err ) )
2020-02-27 20:55:00 +00:00
def getDrinkType ( self , name ) :
try :
cursor = self . db . connection . cursor ( )
if type ( name ) == str :
2020-03-01 18:20:47 +00:00
sql = " select * from drink_type where name= ' {} ' " . format ( name )
elif type ( name ) == int :
2020-02-27 20:55:00 +00:00
sql = ' select * from drink_type where id= {} ' . format ( name )
else :
raise DatabaseExecption ( " name as no type int or str. name= {} , type= {} " . format ( name , type ( name ) ) )
cursor . execute ( sql )
return cursor . fetchone ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
2020-02-25 21:50:32 +00:00
2020-03-01 18:20:47 +00:00
def setDrinkType ( self , name ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " insert into drink_type (name) values ( ' {} ' ) " . format ( name ) )
self . db . connection . commit ( )
return self . getDrinkType ( name )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Database: {} " . format ( err ) )
def updateDrinkType ( self , type ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " update drink_type set name= ' {} ' where id= {} " . format ( type [ ' name ' ] , type [ ' id ' ] ) )
self . db . connection . commit ( )
return self . getDrinkType ( type [ ' id ' ] )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Database: {} " . format ( err ) )
def deleteDrinkType ( self , type ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from drink_type where id= {} " . format ( type [ ' id ' ] ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
def getAllDrinkTypes ( self ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( ' select * from drink_type ' )
return cursor . fetchall ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Database: {} " . format ( err ) )
2020-03-03 21:33:47 +00:00
def getAllStatus ( self ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( ' select * from statusgroup ' )
return cursor . fetchall ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def getStatus ( self , name ) :
try :
cursor = self . db . connection . cursor ( )
if type ( name ) == str :
sql = " select * from statusgroup where name= ' {} ' " . format ( name )
elif type ( name ) == int :
sql = ' select * from statusgroup where id= {} ' . format ( name )
else :
raise DatabaseExecption ( " name as no type int or str. name= {} , type= {} " . format ( name , type ( name ) ) )
cursor . execute ( sql )
return cursor . fetchone ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def setStatus ( self , name ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " insert into statusgroup (name) values ( ' {} ' ) " . format ( name ) )
self . db . connection . commit ( )
return self . getStatus ( name )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def updateStatus ( self , status ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " update statusgroup set name= ' {} ' where id= {} " . format ( status [ ' name ' ] , status [ ' id ' ] ) )
self . db . connection . commit ( )
return self . getStatus ( status [ ' id ' ] )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def deleteStatus ( self , status ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from statusgroup where id= {} " . format ( status [ ' id ' ] ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def updateStatusOfUser ( self , username , status ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " update user set statusgroup= {} where uid= ' {} ' " . format ( status [ ' id ' ] , username ) )
self . db . connection . commit ( )
return self . getUser ( username )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def updateVotingOfUser ( self , username , voting ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " update user set voting= {} where uid= ' {} ' " . format ( voting , username ) )
self . db . connection . commit ( )
return self . getUser ( username )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
2020-05-16 21:25:44 +00:00
def getAllWorkgroups ( self ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( ' select * from workgroup ' )
list = cursor . fetchall ( )
for item in list :
if item [ ' boss ' ] != None :
2020-05-17 11:35:33 +00:00
item [ ' boss ' ] = self . getUserById ( item [ ' boss ' ] , workgroups = False ) . toJSON ( )
2020-05-16 21:25:44 +00:00
return list
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def getWorkgroup ( self , name ) :
try :
cursor = self . db . connection . cursor ( )
if type ( name ) == str :
sql = " select * from workgroup where name= ' {} ' " . format ( name )
elif type ( name ) == int :
sql = ' select * from workgroup where id= {} ' . format ( name )
else :
raise DatabaseExecption ( " name as no type int or str. name= {} , type= {} " . format ( name , type ( name ) ) )
cursor . execute ( sql )
retVal = cursor . fetchone ( )
2020-05-17 11:35:33 +00:00
retVal [ ' boss ' ] = self . getUserById ( retVal [ ' boss ' ] , workgroups = False ) . toJSON ( ) if retVal [ ' boss ' ] != None else None
2020-05-16 21:25:44 +00:00
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def setWorkgroup ( self , name , boss ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " insert into workgroup (name, boss) values ( ' {} ' , {} ) " . format ( name , boss [ ' id ' ] ) )
self . db . connection . commit ( )
return self . getWorkgroup ( name )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def updateWorkgroup ( self , workgroup ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " update workgroup set name= ' {} ' , boss= {} where id= {} " . format ( workgroup [ ' name ' ] , workgroup [ ' boss ' ] [ ' id ' ] , workgroup [ ' id ' ] ) )
self . db . connection . commit ( )
return self . getWorkgroup ( workgroup [ ' id ' ] )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def deleteWorkgroup ( self , workgroup ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from workgroup where id= {} " . format ( workgroup [ ' id ' ] ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
2020-05-17 11:35:33 +00:00
def getWorkgroupsOfUser ( self , userid ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from user_workgroup where user_id= {} " . format ( userid ) )
knots = cursor . fetchall ( )
retVal = [ self . getWorkgroup ( knot [ ' workgroup_id ' ] ) for knot in knots ]
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
def getUsersOfWorkgroups ( self , workgroupid ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from user_workgroup where workgroup_id= {} " . format ( workgroupid ) )
knots = cursor . fetchall ( )
retVal = [ self . getUserById ( knot [ ' user_id ' ] , workgroups = False ) . toJSON ( ) for knot in knots ]
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
def getUserWorkgroup ( self , user , workgroup ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from user_workgroup where workgroup_id= {} and user_id= {} " . format ( workgroup [ ' id ' ] , user [ ' id ' ] ) )
knot = cursor . fetchone ( )
retVal = { " workgroup " : self . getWorkgroup ( workgroup [ ' id ' ] ) , " user " : self . getUserById ( user [ ' id ' ] , workgroups = False ) . toJSON ( ) }
return retVal
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
def setUserWorkgroup ( self , user , workgroup ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " insert into user_workgroup (user_id, workgroup_id) VALUES ( {} , {} ) " . format ( user [ ' id ' ] , workgroup [ ' id ' ] ) )
self . db . connection . commit ( )
return self . getUserWorkgroup ( user , workgroup )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
def deleteWorkgroupsOfUser ( self , user ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from user_workgroup where user_id= {} " . format ( user [ ' id ' ] ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went wrong with Database: {} " . format ( err ) )
2020-05-17 18:21:22 +00:00
def getAllJobKinds ( self ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( ' select * from job_kind ' )
list = cursor . fetchall ( )
2020-05-17 19:18:56 +00:00
for item in list :
item [ ' workgroup ' ] = self . getWorkgroup ( item [ ' workgroup ' ] ) if item [ ' workgroup ' ] != None else None
2020-05-17 18:21:22 +00:00
return list
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def getJobKind ( self , name ) :
try :
cursor = self . db . connection . cursor ( )
if type ( name ) == str :
sql = " select * from job_kind where name= ' {} ' " . format ( name )
elif type ( name ) == int :
sql = ' select * from job_kind where id= {} ' . format ( name )
else :
raise DatabaseExecption ( " name as no type int or str. name= {} , type= {} " . format ( name , type ( name ) ) )
cursor . execute ( sql )
retVal = cursor . fetchone ( )
2020-05-17 19:18:56 +00:00
retVal [ ' workgroup ' ] = self . getWorkgroup ( retVal [ ' workgroup ' ] ) if retVal [ ' workgroup ' ] != None else None
2020-05-17 18:21:22 +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-05-17 19:18:56 +00:00
def setJobKind ( self , name , workgroup_id ) :
2020-05-17 18:21:22 +00:00
try :
cursor = self . db . connection . cursor ( )
2020-05-17 19:18:56 +00:00
cursor . execute ( " insert into job_kind (name, workgroup) values ( ' {} ' , {} ) " . format ( name , workgroup_id if workgroup_id != None else ' NULL ' ) )
2020-05-17 18:21:22 +00:00
self . db . connection . commit ( )
return self . getJobKind ( name )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def updateJobKind ( self , jobkind ) :
try :
cursor = self . db . connection . cursor ( )
2020-05-17 19:18:56 +00:00
cursor . execute ( " update job_kind set name= ' {} ' , workgroup= {} where id= {} " . format ( jobkind [ ' name ' ] , jobkind [ ' workgroup ' ] [ ' id ' ] if jobkind [ ' workgroup ' ] != None else ' NULL ' , jobkind [ ' id ' ] ) )
2020-05-17 18:21:22 +00:00
self . db . connection . commit ( )
return self . getJobKind ( jobkind [ ' id ' ] )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
def deleteJobKind ( self , jobkind ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from job_kind where id= {} " . format ( jobkind [ ' id ' ] ) )
self . db . connection . commit ( )
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Databes: {} " . format ( err ) )
2019-12-19 07:12:29 +00:00
if __name__ == ' __main__ ' :
2019-12-19 17:26:41 +00:00
db = DatabaseController ( )
user = db . getUser ( ' jhille ' )
db . getCreditListFromUser ( user , year = 2018 )