2020-05-22 19:55:14 +00:00
import traceback
from datetime import timedelta
from geruecht . exceptions import DatabaseExecption
class Base :
def getWorker ( self , user , date ) :
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 } , " job_kind " : self . getJobKind ( data [ ' job_kind ' ] ) if data [ ' job_kind ' ] != None else None } if data else None
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
def getWorkers ( self , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from bardienste where startdatetime= ' {} ' " . format ( date ) )
data = cursor . fetchall ( )
retVal = [ ]
2020-06-04 19:20:38 +00:00
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 } , " job_kind " : self . getJobKind ( work [ ' job_kind ' ] ) if work [ ' job_kind ' ] != None else None } for work in data ]
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
def getWorkersWithJobKind ( self , date , job_kind ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " select * from bardienste where startdatetime= ' {} ' and job_kind= {} {} " . format ( date , job_kind [ ' id ' ] , " or job_kind= ' null ' " if job_kind [ ' id ' ] is 1 else ' ' ) )
data = cursor . fetchall ( )
retVal = [ ]
2020-05-22 19:55:14 +00:00
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 } , " job_kind " : self . getJobKind ( work [ ' job_kind ' ] ) if work [ ' job_kind ' ] != None else None } for work in data ]
except Exception as err :
traceback . print_exc ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )
def setWorker ( self , user , date , job_kind = None ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " insert into bardienste (user_id, startdatetime, enddatetime, job_kind) values ( {} , ' {} ' , ' {} ' , {} ) " . format ( user . id , date , date + timedelta ( days = 1 ) , job_kind [ ' id ' ] if job_kind != None else ' NULL ' ) )
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-06-02 21:24:17 +00:00
def changeWorker ( self , from_user , to_user , date ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " update bardienste set user_id= {} where user_id= {} and startdatetime= ' {} ' " . format ( to_user [ ' id ' ] , from_user [ ' id ' ] , date ) )
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-05-22 19:55:14 +00:00
def deleteAllWorkerWithJobKind ( self , date , job_kind ) :
try :
cursor = self . db . connection . cursor ( )
cursor . execute ( " delete from bardienste where startdatetime= ' {} ' and job_kind= {} " . format ( date , job_kind [ ' 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 deleteWorker ( self , user , date ) :
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 ( )
self . db . connection . rollback ( )
raise DatabaseExecption ( " Something went worng with Datatabase: {} " . format ( err ) )