finished ##247 and ##253

fixed bug that user can't add negative amount.
This commit is contained in:
Tim Gröger 2020-05-17 13:35:33 +02:00
parent c9fe61b1dc
commit 27981efedf
4 changed files with 97 additions and 11 deletions

View File

@ -18,7 +18,7 @@ class DatabaseController(metaclass=Singleton):
def __init__(self): def __init__(self):
self.db = db self.db = db
def getAllUser(self, extern=False): def getAllUser(self, extern=False, workgroups=True):
try: try:
cursor = self.db.connection.cursor() cursor = self.db.connection.cursor()
cursor.execute("select * from user") cursor.execute("select * from user")
@ -32,6 +32,8 @@ class DatabaseController(metaclass=Singleton):
user = User(value) user = User(value)
creditLists = self.getCreditListFromUser(user) creditLists = self.getCreditListFromUser(user)
user.initGeruechte(creditLists) user.initGeruechte(creditLists)
if workgroups:
user.workgroups = self.getWorkgroupsOfUser(user.id)
retVal.append(user) retVal.append(user)
return retVal return retVal
except Exception as err: except Exception as err:
@ -39,7 +41,7 @@ class DatabaseController(metaclass=Singleton):
self.db.connection.rollback() self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
def getUser(self, username): def getUser(self, username, workgroups=True):
try: try:
retVal = None retVal = None
cursor = self.db.connection.cursor() cursor = self.db.connection.cursor()
@ -49,13 +51,15 @@ class DatabaseController(metaclass=Singleton):
retVal = User(data) retVal = User(data)
creditLists = self.getCreditListFromUser(retVal) creditLists = self.getCreditListFromUser(retVal)
retVal.initGeruechte(creditLists) retVal.initGeruechte(creditLists)
if workgroups:
retVal.workgroups = self.getWorkgroupsOfUser(retVal.id)
return retVal return retVal
except Exception as err: except Exception as err:
traceback.print_exc() traceback.print_exc()
self.db.connection.rollback() self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
def getUserById(self, id): def getUserById(self, id, workgroups=True):
try: try:
retVal = None retVal = None
cursor = self.db.connection.cursor() cursor = self.db.connection.cursor()
@ -65,6 +69,8 @@ class DatabaseController(metaclass=Singleton):
retVal = User(data) retVal = User(data)
creditLists = self.getCreditListFromUser(retVal) creditLists = self.getCreditListFromUser(retVal)
retVal.initGeruechte(creditLists) retVal.initGeruechte(creditLists)
if workgroups:
retVal.workgroups = self.getWorkgroupsOfUser(retVal.id)
return retVal return retVal
except Exception as err: except Exception as err:
traceback.print_exc() traceback.print_exc()
@ -580,7 +586,7 @@ class DatabaseController(metaclass=Singleton):
list = cursor.fetchall() list = cursor.fetchall()
for item in list: for item in list:
if item['boss'] != None: if item['boss'] != None:
item['boss']=self.getUserById(item['boss']) item['boss']=self.getUserById(item['boss'], workgroups=False).toJSON()
return list return list
except Exception as err: except Exception as err:
traceback.print_exc() 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))) raise DatabaseExecption("name as no type int or str. name={}, type={}".format(name, type(name)))
cursor.execute(sql) cursor.execute(sql)
retVal = cursor.fetchone() 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 return retVal
except Exception as err: except Exception as err:
traceback.print_exc() traceback.print_exc()
@ -637,6 +643,63 @@ class DatabaseController(metaclass=Singleton):
self.db.connection.rollback() self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Databes: {}".format(err)) 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__': if __name__ == '__main__':
db = DatabaseController() db = DatabaseController()
user = db.getUser('jhille') user = db.getUser('jhille')

View File

@ -23,6 +23,13 @@ class UserController(metaclass=Singleton):
debug.debug("init UserController") debug.debug("init UserController")
pass 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): def getAllWorkgroups(self):
debug.info("get all workgroups") debug.info("get all workgroups")
retVal = db.getAllWorkgroups() retVal = db.getAllWorkgroups()

View File

@ -58,6 +58,10 @@ class User():
self.group = data['gruppe'].split(',') self.group = data['gruppe'].split(',')
if 'creditLists' in data: if 'creditLists' in data:
self.geruechte = data['creditLists'] self.geruechte = data['creditLists']
if 'workgroups' in data:
self.workgroups = data['workgroups']
else:
self.workgroups = None
self.password = '' self.password = ''
debug.debug("user is {{ {} }}".format(self)) debug.debug("user is {{ {} }}".format(self))
@ -83,6 +87,10 @@ class User():
self.statusgroup = data['statusgroup'] self.statusgroup = data['statusgroup']
if 'voting' in data: if 'voting' in data:
self.voting = data['voting'] self.voting = data['voting']
if 'workgroups' in data:
self.workgroups = data['workgroups']
else:
self.workgroups = None
def initGeruechte(self, creditLists): def initGeruechte(self, creditLists):
if type(creditLists) == list: if type(creditLists) == list:
@ -223,7 +231,8 @@ class User():
"limit": self.limit, "limit": self.limit,
"mail": self.mail, "mail": self.mail,
"statusgroup": self.statusgroup, "statusgroup": self.statusgroup,
"voting": self.voting "voting": self.voting,
"workgroups": self.workgroups
} }
return dic return dic

View File

@ -88,6 +88,18 @@ def _updateVoting(**kwargs):
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500 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']) @vorstand.route("/sm/addUser", methods=['POST', 'GET'])
@login_required(groups=[MONEY, GASTRO, VORSTAND]) @login_required(groups=[MONEY, GASTRO, VORSTAND])
@ -203,8 +215,6 @@ def _getAllWorkgroups(**kwargs):
try: try:
debug.info("get all workgroups") debug.info("get all workgroups")
retVal = userController.getAllWorkgroups() 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)) debug.info("return all workgroups {{ {} }}".format(retVal))
return jsonify(retVal) return jsonify(retVal)
except Exception as err: except Exception as err:
@ -222,7 +232,6 @@ def _getWorkgroup(**kwargs):
retVal = userController.getWorkgroups(name) retVal = userController.getWorkgroups(name)
debug.info( debug.info(
"return workgroup {{ {} }} : {{ {} }}".format(name, retVal)) "return workgroup {{ {} }} : {{ {} }}".format(name, retVal))
retVal['boss'] = retVal['boss'].toJSON() if retVal['boss'] != None else None
return jsonify(retVal) return jsonify(retVal)
except Exception as err: except Exception as err:
debug.warning("exception in get workgroup.", exc_info=True) debug.warning("exception in get workgroup.", exc_info=True)
@ -240,11 +249,9 @@ def _workgroup(**kwargs):
if 'boss' in data: if 'boss' in data:
boss = data['boss'] boss = data['boss']
retVal = userController.setWorkgroup(name, boss) retVal = userController.setWorkgroup(name, boss)
retVal['boss'] = retVal['boss'].toJSON() if retVal['boss'] != None else None
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
if request.method == 'POST': if request.method == 'POST':
retVal = userController.updateWorkgroup(data) retVal = userController.updateWorkgroup(data)
retVal['boss'] = retVal['boss'].toJSON() if retVal['boss'] != None else None
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal) return jsonify(retVal)
except Exception as err: except Exception as err: