diff --git a/.gitignore b/.gitignore index 037f03d..24662c6 100644 --- a/.gitignore +++ b/.gitignore @@ -119,4 +119,8 @@ dmypy.json .idea .vscode/ -*.log \ No newline at end of file +*.log + +# custom +test_pricelist/ +test_project/ diff --git a/geruecht/controller/databaseController.py b/geruecht/controller/databaseController.py index d2d1fdb..82d32b9 100644 --- a/geruecht/controller/databaseController.py +++ b/geruecht/controller/databaseController.py @@ -18,7 +18,7 @@ class DatabaseController(metaclass=Singleton): def __init__(self): self.db = db - def getAllUser(self, extern=False): + def getAllUser(self, extern=False, workgroups=True): try: cursor = self.db.connection.cursor() cursor.execute("select * from user") @@ -32,6 +32,8 @@ class DatabaseController(metaclass=Singleton): user = User(value) creditLists = self.getCreditListFromUser(user) user.initGeruechte(creditLists) + if workgroups: + user.workgroups = self.getWorkgroupsOfUser(user.id) retVal.append(user) return retVal except Exception as err: @@ -39,7 +41,7 @@ class DatabaseController(metaclass=Singleton): self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) - def getUser(self, username): + def getUser(self, username, workgroups=True): try: retVal = None cursor = self.db.connection.cursor() @@ -49,13 +51,15 @@ class DatabaseController(metaclass=Singleton): retVal = User(data) creditLists = self.getCreditListFromUser(retVal) retVal.initGeruechte(creditLists) + if workgroups: + retVal.workgroups = self.getWorkgroupsOfUser(retVal.id) return retVal except Exception as err: traceback.print_exc() self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) - def getUserById(self, id): + def getUserById(self, id, workgroups=True): try: retVal = None cursor = self.db.connection.cursor() @@ -65,6 +69,8 @@ class DatabaseController(metaclass=Singleton): retVal = User(data) creditLists = self.getCreditListFromUser(retVal) retVal.initGeruechte(creditLists) + if workgroups: + retVal.workgroups = self.getWorkgroupsOfUser(retVal.id) return retVal except Exception as err: traceback.print_exc() @@ -573,6 +579,127 @@ class DatabaseController(metaclass=Singleton): self.db.connection.rollback() raise DatabaseExecption("Something went worng with Databes: {}".format(err)) + 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)) + if __name__ == '__main__': db = DatabaseController() user = db.getUser('jhille') diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index 0b86736..ebdffde 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -23,6 +23,42 @@ class UserController(metaclass=Singleton): debug.debug("init UserController") pass + def updateWorkgroupsOfUser(self, user, workgroups): + debug.info("update workgroups {{ {} }} of user {{ {} }}".format(workgroups, user)) + db.deleteWorkgroupsOfUser(user) + for workgroup in workgroups: + db.setUserWorkgroup(user, workgroup) + return db.getWorkgroupsOfUser(user['id']) + + def getAllWorkgroups(self): + debug.info("get all workgroups") + retVal = db.getAllWorkgroups() + debug.debug("workgroups are {{ {} }}".format(retVal)) + return retVal + + def getWorkgroups(self, name): + debug.info("get Workgroup {{ {} }}".format(name)) + retVal = db.getWorkgroup(name) + debug.debug("workgroup is {{ {} }} is {{ {} }}".format(name, retVal)) + return retVal + + def setWorkgroup(self, name, boss): + debug.info("set workgroup {{ {} }} with boss {{ {} }}".format(name, boss)) + retVal = db.setWorkgroup(name, boss) + debug.debug( + "seted workgroup {{ {} }} is {{ {} }}".format(name, retVal)) + return retVal + + def deleteWorkgroup(self, workgroup): + debug.info("delete workgroup {{ {} }}".format(workgroup)) + db.deleteWorkgroup(workgroup) + + def updateWorkgroup(self, workgroup): + debug.info("update workgroup {{ {} }}".format(workgroup)) + retVal = db.updateWorkgroup(workgroup) + debug.debug("updated workgroup is {{ {} }}".format(retVal)) + return retVal + def getAllStatus(self): debug.info("get all status for user") retVal = db.getAllStatus() diff --git a/geruecht/model/user.py b/geruecht/model/user.py index d7d5d46..1af0ffe 100644 --- a/geruecht/model/user.py +++ b/geruecht/model/user.py @@ -58,6 +58,10 @@ class User(): self.group = data['gruppe'].split(',') if 'creditLists' in data: self.geruechte = data['creditLists'] + if 'workgroups' in data: + self.workgroups = data['workgroups'] + else: + self.workgroups = None self.password = '' debug.debug("user is {{ {} }}".format(self)) @@ -83,6 +87,10 @@ class User(): self.statusgroup = data['statusgroup'] if 'voting' in data: self.voting = data['voting'] + if 'workgroups' in data: + self.workgroups = data['workgroups'] + else: + self.workgroups = None def initGeruechte(self, creditLists): if type(creditLists) == list: @@ -223,7 +231,8 @@ class User(): "limit": self.limit, "mail": self.mail, "statusgroup": self.statusgroup, - "voting": self.voting + "voting": self.voting, + "workgroups": self.workgroups } return dic diff --git a/geruecht/vorstand/routes.py b/geruecht/vorstand/routes.py index 14035f3..56b8ff6 100644 --- a/geruecht/vorstand/routes.py +++ b/geruecht/vorstand/routes.py @@ -88,6 +88,18 @@ def _updateVoting(**kwargs): debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 +@vorstand.route('/um/updateWorkgroups', methods=['POST']) +@login_required(groups=[VORSTAND]) +def _updateWorkgroups(**kwargs): + debug.info("/um/updateWorkgroups") + try: + data = request.get_json() + retVal = userController.updateWorkgroupsOfUser({"id": data['id']}, data['workgroups']) + debug.debug("return {{ {} }}".format(retVal)) + return jsonify(retVal), 200 + except Exception as err: + debug.debug("exception", exc_info=True) + return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/addUser", methods=['POST', 'GET']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) @@ -197,6 +209,69 @@ def _deletUser(**kwargs): debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 +@vorstand.route("/wgm/getAllWorkgroups", methods=['GET']) +@login_required(bar=True) +def _getAllWorkgroups(**kwargs): + try: + debug.info("get all workgroups") + retVal = userController.getAllWorkgroups() + debug.info("return all workgroups {{ {} }}".format(retVal)) + return jsonify(retVal) + except Exception as err: + debug.warning("exception in get all workgroups.", exc_info=True) + return jsonify({"error": str(err)}), 500 + +@vorstand.route("/wgm/getWorkgroup", methods=['POST']) +@login_required(bar=True) +def _getWorkgroup(**kwargs): + try: + debug.info("get workgroup") + data = request.get_json() + name = data['name'] + debug.info("get workgroup {{ {} }}".format(name)) + retVal = userController.getWorkgroups(name) + debug.info( + "return workgroup {{ {} }} : {{ {} }}".format(name, retVal)) + return jsonify(retVal) + except Exception as err: + debug.warning("exception in get workgroup.", exc_info=True) + return jsonify({"error": str(err)}), 500 + +@vorstand.route("/wgm/workgroup", methods=['POST', 'PUT', 'DELETE']) +@login_required(groups=[MONEY, GASTRO, VORSTAND]) +def _workgroup(**kwargs): + debug.info("/wgm/workgroup") + try: + data = request.get_json() + if request.method == 'PUT': + name = data['name'] + boss = None + if 'boss' in data: + boss = data['boss'] + retVal = userController.setWorkgroup(name, boss) + debug.debug("return {{ {} }}".format(retVal)) + if request.method == 'POST': + retVal = userController.updateWorkgroup(data) + debug.debug("return {{ {} }}".format(retVal)) + return jsonify(retVal) + except Exception as err: + debug.debug("exception", exc_info=True) + return jsonify({"error": str(err)}), 500 + +@vorstand.route("/wgm/deleteWorkgroup", methods=['POST']) +@login_required(groups=[VORSTAND]) +def _deleteWorkgroup(**kwargs): + try: + data = request.get_json() + debug.info("/wgm/deleteWorkgroup") + userController.deleteWorkgroup(data) + retVal = {"ok": "ok"} + debug.debug("return ok") + return jsonify(retVal) + except Exception as err: + debug.debug("exception", exc_info=True) + return jsonify({"error": str(err)}), 500 + @vorstand.route("/sm/lockDay", methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND])