From 27981efedf9545ab404b4d131c12bfaaa17c0249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Sun, 17 May 2020 13:35:33 +0200 Subject: [PATCH] finished ##247 and ##253 fixed bug that user can't add negative amount. --- geruecht/controller/databaseController.py | 73 +++++++++++++++++++++-- geruecht/controller/userController.py | 7 +++ geruecht/model/user.py | 11 +++- geruecht/vorstand/routes.py | 17 ++++-- 4 files changed, 97 insertions(+), 11 deletions(-) diff --git a/geruecht/controller/databaseController.py b/geruecht/controller/databaseController.py index e364e3b..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() @@ -580,7 +586,7 @@ class DatabaseController(metaclass=Singleton): list = cursor.fetchall() for item in list: if item['boss'] != None: - item['boss']=self.getUserById(item['boss']) + item['boss']=self.getUserById(item['boss'], workgroups=False).toJSON() return list except Exception as err: traceback.print_exc() @@ -598,7 +604,7 @@ class DatabaseController(metaclass=Singleton): 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']) if retVal['boss'] != None else None + retVal['boss'] = self.getUserById(retVal['boss'], workgroups=False).toJSON() if retVal['boss'] != None else None return retVal except Exception as err: traceback.print_exc() @@ -637,6 +643,63 @@ class DatabaseController(metaclass=Singleton): 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 90778d9..ebdffde 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -23,6 +23,13 @@ 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() 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 7f35b9a..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]) @@ -203,8 +215,6 @@ def _getAllWorkgroups(**kwargs): try: debug.info("get all workgroups") retVal = userController.getAllWorkgroups() - for item in retVal: - item['boss'] = item['boss'].toJSON() if item['boss'] != None else None debug.info("return all workgroups {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: @@ -222,7 +232,6 @@ def _getWorkgroup(**kwargs): retVal = userController.getWorkgroups(name) debug.info( "return workgroup {{ {} }} : {{ {} }}".format(name, retVal)) - retVal['boss'] = retVal['boss'].toJSON() if retVal['boss'] != None else None return jsonify(retVal) except Exception as err: debug.warning("exception in get workgroup.", exc_info=True) @@ -240,11 +249,9 @@ def _workgroup(**kwargs): if 'boss' in data: boss = data['boss'] retVal = userController.setWorkgroup(name, boss) - retVal['boss'] = retVal['boss'].toJSON() if retVal['boss'] != None else None debug.debug("return {{ {} }}".format(retVal)) if request.method == 'POST': retVal = userController.updateWorkgroup(data) - retVal['boss'] = retVal['boss'].toJSON() if retVal['boss'] != None else None debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: