diff --git a/geruecht/controller/databaseController.py b/geruecht/controller/databaseController.py index d2d1fdb..e364e3b 100644 --- a/geruecht/controller/databaseController.py +++ b/geruecht/controller/databaseController.py @@ -573,6 +573,70 @@ 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']) + 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']) 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)) + if __name__ == '__main__': db = DatabaseController() user = db.getUser('jhille') diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index 0b86736..90778d9 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -23,6 +23,35 @@ class UserController(metaclass=Singleton): debug.debug("init UserController") pass + 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/vorstand/routes.py b/geruecht/vorstand/routes.py index 14035f3..7f35b9a 100644 --- a/geruecht/vorstand/routes.py +++ b/geruecht/vorstand/routes.py @@ -197,6 +197,74 @@ 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() + 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: + 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)) + 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) + 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) + 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: + 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])