From 3e61893baf0bf2e906f0c9d7aa8ee12efbbe2f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Thu, 27 Feb 2020 21:55:00 +0100 Subject: [PATCH 1/2] finished ##203 --- geruecht/controller/databaseController.py | 25 +++++++++++++++++++++++ geruecht/controller/userController.py | 7 +++++++ geruecht/routes.py | 10 +++++++++ 3 files changed, 42 insertions(+) diff --git a/geruecht/controller/databaseController.py b/geruecht/controller/databaseController.py index b9c2ba3..124acbd 100644 --- a/geruecht/controller/databaseController.py +++ b/geruecht/controller/databaseController.py @@ -368,6 +368,31 @@ class DatabaseController(metaclass=Singleton): self.db.connection.rollback() raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + def getPriceList(self): + try: + cursor = self.db.connection.cursor() + cursor.execute("select * from pricelist") + return cursor.fetchall() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + + def getDrinkType(self, name): + try: + cursor = self.db.connection.cursor() + if type(name) == str: + sql = 'select * from drink_type where name={}'.format(name) + if type(name) == int: + sql = 'select * from drink_type where id={}'.format(name) + else: + raise DatabaseExecption("name as no type int or str. name={}, type={}".format(name, type(name))) + cursor.execute(sql) + return cursor.fetchone() + 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() diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index 852662f..f0a0d0e 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -17,6 +17,13 @@ class UserController(metaclass=Singleton): def __init__(self): pass + def getPricelist(self): + list = db.getPriceList() + for element in list: + type = db.getDrinkType(element['type']) + element['type'] = type['name'] + return list + def setTransactJob(self, from_user, to_user, date): jobtransact = db.setTransactJob(from_user, to_user, date.date()) emailController.sendMail(jobtransact['to_user'], 'jobtransact', jobtransact) diff --git a/geruecht/routes.py b/geruecht/routes.py index bd71ea5..2cc7c4d 100644 --- a/geruecht/routes.py +++ b/geruecht/routes.py @@ -29,6 +29,16 @@ def _valid(): return jsonify(accToken.user.toJSON()) return jsonify({"error": "permission denied"}), 401 +@app.route("/pricelist", methods=['GET']) +def _getPricelist(): + try: + retVal = userController.getPricelist() + print(retVal) + return jsonify(retVal) + except Exception as err: + return jsonify({"error": str(err)}) + + @app.route("/login", methods=['POST']) def _login(): From 71c850c8c63f71f64978b96631fa3613a5633558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Sun, 1 Mar 2020 19:20:47 +0100 Subject: [PATCH 2/2] finished ##172 add, edit, delete drinks in pricelist add, edit, delete drinkTypes --- geruecht/__init__.py | 2 + geruecht/baruser/routes.py | 3 +- geruecht/controller/databaseController.py | 101 +++++++++++++++++++++- geruecht/controller/ldapController.py | 5 +- geruecht/controller/userController.py | 28 +++++- geruecht/gastro/__init__.py | 0 geruecht/gastro/routes.py | 71 +++++++++++++++ geruecht/routes.py | 8 ++ 8 files changed, 208 insertions(+), 10 deletions(-) create mode 100644 geruecht/gastro/__init__.py create mode 100644 geruecht/gastro/routes.py diff --git a/geruecht/__init__.py b/geruecht/__init__.py index ed985cc..2815956 100644 --- a/geruecht/__init__.py +++ b/geruecht/__init__.py @@ -38,9 +38,11 @@ from geruecht.baruser.routes import baruser from geruecht.finanzer.routes import finanzer from geruecht.user.routes import user from geruecht.vorstand.routes import vorstand +from geruecht.gastro.routes import gastrouser LOGGER.info("Registrate bluebrints") app.register_blueprint(baruser) app.register_blueprint(finanzer) app.register_blueprint(user) app.register_blueprint(vorstand) +app.register_blueprint(gastrouser) diff --git a/geruecht/baruser/routes.py b/geruecht/baruser/routes.py index 58dbc52..0e2c85f 100644 --- a/geruecht/baruser/routes.py +++ b/geruecht/baruser/routes.py @@ -1,5 +1,4 @@ from flask import Blueprint, request, jsonify -import geruecht.controller as gc import geruecht.controller.ldapController as lc import geruecht.controller.userController as uc from datetime import datetime @@ -8,7 +7,7 @@ from geruecht.decorator import login_required baruser = Blueprint("baruser", __name__) -ldap= lc.LDAPController(gc.ldapConfig['URL'], gc.ldapConfig['dn']) +ldap= lc.LDAPController() userController = uc.UserController() diff --git a/geruecht/controller/databaseController.py b/geruecht/controller/databaseController.py index 124acbd..3976ece 100644 --- a/geruecht/controller/databaseController.py +++ b/geruecht/controller/databaseController.py @@ -378,12 +378,67 @@ class DatabaseController(metaclass=Singleton): self.db.connection.rollback() raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + def getDrinkPrice(self, name): + try: + cursor = self.db.connection.cursor() + if type(name) == str: + sql = "select * from pricelist where name='{}'".format(name) + elif type(name) == int: + sql = 'select * from pricelist where id={}'.format(name) + else: + raise DatabaseExecption("name as no type int or str. name={}, type={}".format(name, type(name))) + cursor.execute(sql) + return cursor.fetchone() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + + def setDrinkPrice(self, drink): + try: + cursor = self.db.connection.cursor() + cursor.execute( + "insert into pricelist (name, price, price_big, price_club, price_club_big, premium, premium_club, price_extern_club, type) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", + ( + drink['name'], drink['price'], drink['price_big'], drink['price_club'], drink['price_club_big'], + drink['premium'], drink['premium_club'], drink['price_extern_club'], drink['type'])) + self.db.connection.commit() + return self.getDrinkPrice(str(drink['name'])) + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + + def updateDrinkPrice(self, drink): + try: + cursor = self.db.connection.cursor() + cursor.execute("update pricelist set name=%s, price=%s, price_big=%s, price_club=%s, price_club_big=%s, premium=%s, premium_club=%s, price_extern_club=%s, type=%s where id=%s", + ( + drink['name'], drink['price'], drink['price_big'], drink['price_club'], drink['price_club_big'], drink['premium'], drink['premium_club'], drink['price_extern_club'], drink['type'], drink['id'] + )) + self.db.connection.commit() + return self.getDrinkPrice(drink['id']) + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + + def deleteDrink(self, drink): + try: + cursor = self.db.connection.cursor() + cursor.execute("delete from pricelist where id={}".format(drink['id'])) + self.db.connection.commit() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Database: {}".format(err)) + def getDrinkType(self, name): try: cursor = self.db.connection.cursor() if type(name) == str: - sql = 'select * from drink_type where name={}'.format(name) - if type(name) == int: + sql = "select * from drink_type where name='{}'".format(name) + elif type(name) == int: sql = 'select * from drink_type where id={}'.format(name) else: raise DatabaseExecption("name as no type int or str. name={}, type={}".format(name, type(name))) @@ -394,6 +449,48 @@ class DatabaseController(metaclass=Singleton): self.db.connection.rollback() raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + def setDrinkType(self, name): + try: + cursor = self.db.connection.cursor() + cursor.execute("insert into drink_type (name) values ('{}')".format(name)) + self.db.connection.commit() + return self.getDrinkType(name) + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Database: {}".format(err)) + + def updateDrinkType(self, type): + try: + cursor = self.db.connection.cursor() + cursor.execute("update drink_type set name='{}' where id={}".format(type['name'], type['id'])) + self.db.connection.commit() + return self.getDrinkType(type['id']) + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Database: {}".format(err)) + + def deleteDrinkType(self, type): + try: + cursor = self.db.connection.cursor() + cursor.execute("delete from drink_type where id={}".format(type['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)) + + def getAllDrinkTypes(self): + try: + cursor = self.db.connection.cursor() + cursor.execute('select * from drink_type') + return cursor.fetchall() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Database: {}".format(err)) + if __name__ == '__main__': db = DatabaseController() user = db.getUser('jhille') diff --git a/geruecht/controller/ldapController.py b/geruecht/controller/ldapController.py index 9fa3d5b..2e09d9b 100644 --- a/geruecht/controller/ldapController.py +++ b/geruecht/controller/ldapController.py @@ -5,6 +5,7 @@ from geruecht.model import MONEY, USER, GASTRO, BAR from geruecht.exceptions import PermissionDenied from . import Singleton from geruecht.exceptions import UsernameExistLDAP, LDAPExcetpion +from geruecht import ldapConfig import traceback class LDAPController(metaclass=Singleton): @@ -12,8 +13,8 @@ class LDAPController(metaclass=Singleton): Authentification over LDAP. Create Account on-the-fly ''' - def __init__(self, dn='dc=ldap,dc=example,dc=local'): - self.dn = dn + def __init__(self): + self.dn = ldapConfig['dn'] self.ldap = ldap diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index f0a0d0e..5d912c4 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -9,7 +9,7 @@ from datetime import datetime, timedelta from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExecption, LDAPExcetpion, DayLocked, TansactJobIsAnswerdException db = dc.DatabaseController() -ldap = lc.LDAPController(ldapConfig['dn']) +ldap = lc.LDAPController() emailController = ec.EmailController(mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['crypt'], mailConfig['port'], mailConfig['email']) class UserController(metaclass=Singleton): @@ -17,11 +17,31 @@ class UserController(metaclass=Singleton): def __init__(self): pass + def deleteDrinkType(self, type): + db.deleteDrinkType(type) + + def updateDrinkType(self, type): + return db.updateDrinkType(type) + + def setDrinkType(self, type): + return db.setDrinkType(type) + + def deletDrinkPrice(self, drink): + db.deleteDrink(drink) + + def setDrinkPrice(self, drink): + retVal = db.setDrinkPrice(drink) + return retVal + + def updateDrinkPrice(self, drink): + retVal = db.updateDrinkPrice(drink) + return retVal + + def getAllDrinkTypes(self): + return db.getAllDrinkTypes() + def getPricelist(self): list = db.getPriceList() - for element in list: - type = db.getDrinkType(element['type']) - element['type'] = type['name'] return list def setTransactJob(self, from_user, to_user, date): diff --git a/geruecht/gastro/__init__.py b/geruecht/gastro/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/geruecht/gastro/routes.py b/geruecht/gastro/routes.py new file mode 100644 index 0000000..9deca0a --- /dev/null +++ b/geruecht/gastro/routes.py @@ -0,0 +1,71 @@ +from flask import request, jsonify, Blueprint +from geruecht.decorator import login_required +import geruecht.controller.userController as uc +from geruecht.model import GASTRO + +gastrouser = Blueprint('gastrouser', __name__) + +userController = uc.UserController() + +@gastrouser.route('/gastro/setDrink', methods=['POST']) +@login_required(groups=[GASTRO]) +def setDrink(**kwargs): + try: + data = request.get_json() + retVal = userController.setDrinkPrice(data) + return jsonify(retVal) + except Exception as err: + return jsonify({"error": str(err)}), 500 + +@gastrouser.route('/gastro/updateDrink', methods=['POST']) +@login_required(groups=[GASTRO]) +def updateDrink(**kwargs): + try: + data = request.get_json() + retVal = userController.updateDrinkPrice(data) + return jsonify(retVal) + except Exception as err: + return jsonify({"error": str(err)}), 500 + +@gastrouser.route('/gastro/deleteDrink', methods=['POST']) +@login_required(groups=[GASTRO]) +def deleteDrink(**kwargs): + try: + data = request.get_json() + id = data['id'] + retVal = userController.deletDrinkPrice({"id": id}) + return jsonify({"ok": "ok"}) + except Exception as err: + return jsonify({"error": str(err)}), 500 + +@gastrouser.route('/gastro/setDrinkType', methods=['POST']) +@login_required(groups=[GASTRO]) +def setType(**kwark): + try: + data = request.get_json() + name = data['name'] + retVal = userController.setDrinkType(name) + return jsonify(retVal) + except Exception as err: + return jsonify({"error": str(err)}), 500 + +@gastrouser.route('/gastro/updateDrinkType', methods=['POST']) +@login_required(groups=[GASTRO]) +def updateType(**kwargs): + try: + data = request.get_json() + retVal = userController.updateDrinkType(data) + return jsonify(retVal) + except Exception as err: + return jsonify({"error": str(err)}), 500 + +@gastrouser.route('/gastro/deleteDrinkType', methods=['POST']) +@login_required(groups=[GASTRO]) +def deleteType(**kwargs): + try: + data = request.get_json() + userController.deleteDrinkType(data) + return jsonify({"ok": "ok"}) + except Exception as err: + return jsonify({"error": str(err)}), 500 + diff --git a/geruecht/routes.py b/geruecht/routes.py index 2cc7c4d..6801fb4 100644 --- a/geruecht/routes.py +++ b/geruecht/routes.py @@ -38,6 +38,14 @@ def _getPricelist(): except Exception as err: return jsonify({"error": str(err)}) +@app.route('/drinkTypes', methods=['GET']) +def getTypes(): + try: + retVal = userController.getAllDrinkTypes() + return jsonify(retVal) + except Exception as err: + return jsonify({"error": str(err)}), 500 + @app.route("/login", methods=['POST'])