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 = [] # for work in data: # 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 # retVal.append({'user': user, 'startdatetime': startdatetime, 'enddatetime': enddatetime, 'start': start, 'job_kind': job_kind}) # return retVal 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)) 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)) 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))