import traceback from geruecht.exceptions import DatabaseExecption class Base: 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: item['boss']=self.getUserById(item['boss'], workgroups=False).toJSON() 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() retVal['boss'] = self.getUserById(retVal['boss'], workgroups=False).toJSON() if retVal['boss'] != 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 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)) 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))