added decoratos for connected in database and login_requird in routes
This commit is contained in:
parent
29f20b2327
commit
f782be934d
|
@ -15,7 +15,7 @@ from flask_cors import CORS
|
||||||
LOGGER.info("Build APP")
|
LOGGER.info("Build APP")
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app)
|
CORS(app)
|
||||||
# app.config['SECRET_KEY'] = '0a657b97ef546da90b2db91862ad4e29'
|
app.config['SECRET_KEY'] = '0a657b97ef546da90b2db91862ad4e29'
|
||||||
|
|
||||||
from geruecht import routes
|
from geruecht import routes
|
||||||
from geruecht.baruser.routes import baruser
|
from geruecht.baruser.routes import baruser
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
from flask import Blueprint, request, jsonify
|
from flask import Blueprint, request, jsonify
|
||||||
import geruecht.controller as gc
|
import geruecht.controller as gc
|
||||||
import geruecht.controller.ldapController as lc
|
import geruecht.controller.ldapController as lc
|
||||||
import geruecht.controller.accesTokenController as ac
|
|
||||||
import geruecht.controller.userController as uc
|
import geruecht.controller.userController as uc
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from geruecht.model import BAR, MONEY
|
from geruecht.model import BAR, MONEY
|
||||||
|
from geruecht.decorator import login_required
|
||||||
|
|
||||||
baruser = Blueprint("baruser", __name__)
|
baruser = Blueprint("baruser", __name__)
|
||||||
|
|
||||||
ldap= lc.LDAPController(gc.ldapConfig['URL'], gc.ldapConfig['dn'])
|
ldap= lc.LDAPController(gc.ldapConfig['URL'], gc.ldapConfig['dn'])
|
||||||
accesTokenController = ac.AccesTokenController()
|
|
||||||
userController = uc.UserController()
|
userController = uc.UserController()
|
||||||
|
|
||||||
|
|
||||||
@baruser.route("/bar")
|
@baruser.route("/bar")
|
||||||
def _bar():
|
@login_required(groups=[BAR])
|
||||||
|
def _bar(**kwargs):
|
||||||
""" Main function for Baruser
|
""" Main function for Baruser
|
||||||
|
|
||||||
Returns JSON-file with all Users, who hast amounts in this month.
|
Returns JSON-file with all Users, who hast amounts in this month.
|
||||||
|
@ -22,38 +23,33 @@ def _bar():
|
||||||
JSON-File with Users, who has amounts in this month
|
JSON-File with Users, who has amounts in this month
|
||||||
or ERROR 401 Permission Denied
|
or ERROR 401 Permission Denied
|
||||||
"""
|
"""
|
||||||
print(request.headers)
|
|
||||||
token = request.headers.get("Token")
|
|
||||||
print(token)
|
|
||||||
accToken = accesTokenController.validateAccessToken(token, [BAR])
|
|
||||||
|
|
||||||
dic = {}
|
dic = {}
|
||||||
if accToken:
|
users = userController.getAllUsersfromDB()
|
||||||
users = userController.getAllUsersfromDB()
|
for user in users:
|
||||||
for user in users:
|
geruecht = None
|
||||||
geruecht = None
|
geruecht = user.getGeruecht(datetime.now().year)
|
||||||
geruecht = user.getGeruecht(datetime.now().year)
|
if geruecht is not None:
|
||||||
if geruecht is not None:
|
month = geruecht.getMonth(datetime.now().month)
|
||||||
month = geruecht.getMonth(datetime.now().month)
|
amount = month[0] - month[1]
|
||||||
amount = month[0] - month[1]
|
all = geruecht.getSchulden()
|
||||||
all = geruecht.getSchulden()
|
if all != 0:
|
||||||
if all != 0:
|
if all >= 0:
|
||||||
if all >= 0:
|
type = 'credit'
|
||||||
type = 'credit'
|
else:
|
||||||
else:
|
type = 'amount'
|
||||||
type = 'amount'
|
dic[user.uid] = {"username": user.uid,
|
||||||
dic[user.uid] = {"username": user.uid,
|
"firstname": user.firstname,
|
||||||
"firstname": user.firstname,
|
"lastname": user.lastname,
|
||||||
"lastname": user.lastname,
|
"amount": abs(all),
|
||||||
"amount": abs(all),
|
"locked": user.locked,
|
||||||
"locked": user.locked,
|
"type": type
|
||||||
"type": type
|
}
|
||||||
}
|
return jsonify(dic)
|
||||||
return jsonify(dic)
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
||||||
@baruser.route("/baradd", methods=['POST'])
|
@baruser.route("/baradd", methods=['POST'])
|
||||||
def _baradd():
|
@login_required(groups=[BAR])
|
||||||
|
def _baradd(**kwargs):
|
||||||
""" Function for Baruser to add amount
|
""" Function for Baruser to add amount
|
||||||
|
|
||||||
This function added to the user with the posted userID the posted amount.
|
This function added to the user with the posted userID the posted amount.
|
||||||
|
@ -62,35 +58,31 @@ def _baradd():
|
||||||
JSON-File with userID and the amount
|
JSON-File with userID and the amount
|
||||||
or ERROR 401 Permission Denied
|
or ERROR 401 Permission Denied
|
||||||
"""
|
"""
|
||||||
token = request.headers.get("Token")
|
data = request.get_json()
|
||||||
print(token)
|
userID = data['userId']
|
||||||
accToken = accesTokenController.validateAccessToken(token, [BAR])
|
amount = int(data['amount'])
|
||||||
|
|
||||||
if accToken:
|
date = datetime.now()
|
||||||
data = request.get_json()
|
userController.addAmount(userID, amount, year=date.year, month=date.month)
|
||||||
userID = data['userId']
|
user = userController.getUser(userID)
|
||||||
amount = int(data['amount'])
|
geruecht = user.getGeruecht(year=date.year)
|
||||||
|
month = geruecht.getMonth(month=date.month)
|
||||||
|
amount = abs(month[0] - month[1])
|
||||||
|
all = geruecht.getSchulden()
|
||||||
|
if all >= 0:
|
||||||
|
type = 'credit'
|
||||||
|
else:
|
||||||
|
type = 'amount'
|
||||||
|
dic = user.toJSON()
|
||||||
|
dic['amount'] = abs(all)
|
||||||
|
dic['type'] = type
|
||||||
|
|
||||||
date = datetime.now()
|
return jsonify(dic)
|
||||||
userController.addAmount(userID, amount, year=date.year, month=date.month)
|
|
||||||
user = userController.getUser(userID)
|
|
||||||
geruecht = user.getGeruecht(year=date.year)
|
|
||||||
month = geruecht.getMonth(month=date.month)
|
|
||||||
amount = abs(month[0] - month[1])
|
|
||||||
all = geruecht.getSchulden()
|
|
||||||
if all >= 0:
|
|
||||||
type = 'credit'
|
|
||||||
else:
|
|
||||||
type = 'amount'
|
|
||||||
dic = user.toJSON()
|
|
||||||
dic['amount'] = abs(all)
|
|
||||||
dic['type'] = type
|
|
||||||
|
|
||||||
return jsonify(dic)
|
|
||||||
return jsonify({"error", "permission denied"}), 401
|
|
||||||
|
|
||||||
@baruser.route("/barGetUsers")
|
@baruser.route("/barGetUsers")
|
||||||
def _getUsers():
|
@login_required(groups=[BAR, MONEY])
|
||||||
|
def _getUsers(**kwargs):
|
||||||
""" Get Users without amount
|
""" Get Users without amount
|
||||||
|
|
||||||
This Function returns all Users, who hasn't an amount in this month.
|
This Function returns all Users, who hasn't an amount in this month.
|
||||||
|
@ -99,48 +91,33 @@ def _getUsers():
|
||||||
JSON-File with Users
|
JSON-File with Users
|
||||||
or ERROR 401 Permission Denied
|
or ERROR 401 Permission Denied
|
||||||
"""
|
"""
|
||||||
token = request.headers.get("Token")
|
|
||||||
print(token)
|
|
||||||
accToken = accesTokenController.validateAccessToken(token, [BAR])
|
|
||||||
|
|
||||||
retVal = {}
|
retVal = {}
|
||||||
if accToken:
|
retVal = ldap.getAllUser()
|
||||||
retVal = ldap.getAllUser()
|
return jsonify(retVal)
|
||||||
return jsonify(retVal)
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
||||||
@baruser.route("/barGetUser", methods=['POST'])
|
@baruser.route("/barGetUser", methods=['POST'])
|
||||||
def _getUser():
|
@login_required(groups=[BAR])
|
||||||
token = request.headers.get("Token")
|
def _getUser(**kwargs):
|
||||||
accToken = accesTokenController.validateAccessToken(token, [BAR])
|
data = request.get_json()
|
||||||
if accToken:
|
username = data['userId']
|
||||||
data = request.get_json()
|
user = userController.getUser(username)
|
||||||
username = data['userId']
|
amount = user.getGeruecht(datetime.now().year).getSchulden()
|
||||||
user = userController.getUser(username)
|
if amount >= 0:
|
||||||
amount = user.getGeruecht(datetime.now().year).getSchulden()
|
type = 'credit'
|
||||||
if amount >= 0:
|
else:
|
||||||
type = 'credit'
|
type = 'amount'
|
||||||
else:
|
|
||||||
type = 'amount'
|
retVal = user.toJSON()
|
||||||
|
retVal['amount'] = amount
|
||||||
|
retVal['type'] = type
|
||||||
|
return jsonify(retVal)
|
||||||
|
|
||||||
retVal = user.toJSON()
|
|
||||||
retVal['amount'] = amount
|
|
||||||
retVal['type'] = type
|
|
||||||
return jsonify(retVal)
|
|
||||||
return jsonify("error", "permission denied"), 401
|
|
||||||
|
|
||||||
@baruser.route("/search", methods=['POST'])
|
@baruser.route("/search", methods=['POST'])
|
||||||
def _search():
|
@login_required(groups=[BAR, MONEY])
|
||||||
token = request.headers.get("Token")
|
def _search(**kwargs):
|
||||||
print(token)
|
data = request.get_json()
|
||||||
accToken = accesTokenController.validateAccessToken(token, [BAR, MONEY])
|
searchString = data['searchString']
|
||||||
|
retVal = ldap.searchUser(searchString)
|
||||||
if accToken:
|
return jsonify(retVal)
|
||||||
data = request.get_json()
|
|
||||||
|
|
||||||
searchString = data['searchString']
|
|
||||||
|
|
||||||
retVal = ldap.searchUser(searchString)
|
|
||||||
|
|
||||||
return jsonify(retVal)
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
|
@ -4,6 +4,14 @@ from geruecht.model.user import User
|
||||||
from geruecht.model.creditList import CreditList
|
from geruecht.model.creditList import CreditList
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
def connected(func):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
self = args[0]
|
||||||
|
if not self.db.open:
|
||||||
|
self.connect()
|
||||||
|
return func(*args,**kwargs)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
class DatabaseController(metaclass=Singleton):
|
class DatabaseController(metaclass=Singleton):
|
||||||
'''
|
'''
|
||||||
DatabaesController
|
DatabaesController
|
||||||
|
@ -24,16 +32,12 @@ class DatabaseController(metaclass=Singleton):
|
||||||
self.db = pymysql.connect(self.url, self.user, self.password, self.database, cursorclass=pymysql.cursors.DictCursor)
|
self.db = pymysql.connect(self.url, self.user, self.password, self.database, cursorclass=pymysql.cursors.DictCursor)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
raise err
|
raise err
|
||||||
|
@connected
|
||||||
def getAllUser(self):
|
def getAllUser(self):
|
||||||
self.connect()
|
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
try:
|
cursor.execute("select * from user")
|
||||||
cursor.execute("select * from user")
|
data = cursor.fetchall()
|
||||||
data = cursor.fetchall()
|
self.db.close()
|
||||||
self.db.close()
|
|
||||||
except Exception as err:
|
|
||||||
raise err
|
|
||||||
|
|
||||||
if data:
|
if data:
|
||||||
retVal = []
|
retVal = []
|
||||||
|
@ -43,34 +47,26 @@ class DatabaseController(metaclass=Singleton):
|
||||||
user.initGeruechte(creditLists)
|
user.initGeruechte(creditLists)
|
||||||
retVal.append(user)
|
retVal.append(user)
|
||||||
return retVal
|
return retVal
|
||||||
|
@connected
|
||||||
def getUser(self, username):
|
def getUser(self, username):
|
||||||
self.connect()
|
|
||||||
retVal = None
|
retVal = None
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
try:
|
cursor.execute("select * from user where uid='{}'".format(username))
|
||||||
cursor.execute("select * from user where uid='{}'".format(username))
|
data = cursor.fetchone()
|
||||||
data = cursor.fetchone()
|
self.db.close()
|
||||||
self.db.close()
|
|
||||||
except Exception as err:
|
|
||||||
raise err
|
|
||||||
if data:
|
if data:
|
||||||
retVal = User(data)
|
retVal = User(data)
|
||||||
creditLists = self.getCreditListFromUser(retVal)
|
creditLists = self.getCreditListFromUser(retVal)
|
||||||
retVal.initGeruechte(creditLists)
|
retVal.initGeruechte(creditLists)
|
||||||
|
|
||||||
return retVal
|
return retVal
|
||||||
|
@connected
|
||||||
def getUserById(self, id):
|
def getUserById(self, id):
|
||||||
self.connect()
|
|
||||||
retVal = None
|
retVal = None
|
||||||
try:
|
cursor = self.db.cursor()
|
||||||
cursor = self.db.cursor()
|
cursor.execute("select * from user where id={}".format(id))
|
||||||
cursor.execute("select * from user where id={}".format(id))
|
data = cursor.fetchone()
|
||||||
data = cursor.fetchone()
|
self.db.close()
|
||||||
self.db.close()
|
|
||||||
except Exception as err:
|
|
||||||
raise err
|
|
||||||
if data:
|
if data:
|
||||||
retVal = User(data)
|
retVal = User(data)
|
||||||
creditLists = self.getCreditListFromUser(retVal)
|
creditLists = self.getCreditListFromUser(retVal)
|
||||||
|
@ -85,8 +81,8 @@ class DatabaseController(metaclass=Singleton):
|
||||||
retVal += group
|
retVal += group
|
||||||
return retVal
|
return retVal
|
||||||
|
|
||||||
|
@connected
|
||||||
def insertUser(self, user):
|
def insertUser(self, user):
|
||||||
self.connect()
|
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
groups = self._convertGroupToString(user.group)
|
groups = self._convertGroupToString(user.group)
|
||||||
try:
|
try:
|
||||||
|
@ -99,8 +95,8 @@ class DatabaseController(metaclass=Singleton):
|
||||||
raise err
|
raise err
|
||||||
self.db.close()
|
self.db.close()
|
||||||
|
|
||||||
|
@connected
|
||||||
def updateUser(self, user):
|
def updateUser(self, user):
|
||||||
self.connect()
|
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
groups = self._convertGroupToString(user.group)
|
groups = self._convertGroupToString(user.group)
|
||||||
try:
|
try:
|
||||||
|
@ -117,38 +113,35 @@ class DatabaseController(metaclass=Singleton):
|
||||||
|
|
||||||
self.db.close()
|
self.db.close()
|
||||||
|
|
||||||
|
@connected
|
||||||
def getCreditListFromUser(self, user, **kwargs):
|
def getCreditListFromUser(self, user, **kwargs):
|
||||||
self.connect()
|
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
try:
|
if 'year' in kwargs:
|
||||||
if 'year' in kwargs:
|
sql = "select * from creditList where user_id={} and year_date={}".format(user.id, kwargs['year'])
|
||||||
sql = "select * from creditList where user_id={} and year_date={}".format(user.id, kwargs['year'])
|
else:
|
||||||
else:
|
sql = "select * from creditList where user_id={}".format(user.id)
|
||||||
sql = "select * from creditList where user_id={}".format(user.id)
|
cursor.execute(sql)
|
||||||
cursor.execute(sql)
|
data = cursor.fetchall()
|
||||||
data = cursor.fetchall()
|
self.db.close()
|
||||||
self.db.close()
|
|
||||||
except Exception as err:
|
|
||||||
self.db.close()
|
|
||||||
raise err
|
|
||||||
if len(data) == 1:
|
if len(data) == 1:
|
||||||
return [CreditList(data[0])]
|
return [CreditList(data[0])]
|
||||||
else:
|
else:
|
||||||
return [CreditList(value) for value in data]
|
return [CreditList(value) for value in data]
|
||||||
|
|
||||||
|
@connected
|
||||||
def createCreditList(self, user_id, year=datetime.now().year):
|
def createCreditList(self, user_id, year=datetime.now().year):
|
||||||
self.connect()
|
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
try:
|
try:
|
||||||
cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id))
|
cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id))
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
self.db.rollback()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
|
@connected
|
||||||
def updateCreditList(self, creditlist):
|
def updateCreditList(self, creditlist):
|
||||||
self.connect()
|
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
try:
|
try:
|
||||||
cursor.execute("select * from creditList where user_id={} and year_date={}".format(creditlist.user_id, creditlist.year))
|
cursor.execute("select * from creditList where user_id={} and year_date={}".format(creditlist.user_id, creditlist.year))
|
||||||
|
@ -179,32 +172,24 @@ class DatabaseController(metaclass=Singleton):
|
||||||
self.db.rollback()
|
self.db.rollback()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
raise err
|
raise err
|
||||||
|
@connected
|
||||||
def getWorker(self, user, date):
|
def getWorker(self, user, date):
|
||||||
self.connect()
|
cursor = self.db.cursor()
|
||||||
try:
|
cursor.execute("select * from bardienste where user_id={} and startdatetime='{}'".format(user.id, date))
|
||||||
cursor = self.db.cursor()
|
data = cursor.fetchone()
|
||||||
cursor.execute("select * from bardienste where user_id={} and startdatetime='{}'".format(user.id, date))
|
self.db.close()
|
||||||
data = cursor.fetchone()
|
|
||||||
self.db.close()
|
|
||||||
except Exception as err:
|
|
||||||
raise err
|
|
||||||
return {"user": user, "startdatetime": data['startdatetime'], "enddatetime": data['enddatetime']} if data else None
|
return {"user": user, "startdatetime": data['startdatetime'], "enddatetime": data['enddatetime']} if data else None
|
||||||
|
|
||||||
|
@connected
|
||||||
def getWorkers(self, date):
|
def getWorkers(self, date):
|
||||||
self.connect()
|
cursor = self.db.cursor()
|
||||||
try:
|
cursor.execute("select * from bardienste where startdatetime='{}'".format(date))
|
||||||
cursor = self.db.cursor()
|
data = cursor.fetchall()
|
||||||
cursor.execute("select * from bardienste where startdatetime='{}'".format(date))
|
self.db.close()
|
||||||
data = cursor.fetchall()
|
|
||||||
self.db.close()
|
|
||||||
except Exception as err:
|
|
||||||
raise err
|
|
||||||
|
|
||||||
return [{"user": self.getUserById(work['user_id']).toJSON(), "startdatetime": work['startdatetime'], "enddatetime": work['enddatetime']} for work in data]
|
return [{"user": self.getUserById(work['user_id']).toJSON(), "startdatetime": work['startdatetime'], "enddatetime": work['enddatetime']} for work in data]
|
||||||
|
|
||||||
|
@connected
|
||||||
def setWorker(self, user, date):
|
def setWorker(self, user, date):
|
||||||
self.connect()
|
|
||||||
try:
|
try:
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("insert into bardienste (user_id, startdatetime, enddatetime) values ({},'{}','{}')".format(user.id, date, date + timedelta(days=1)))
|
cursor.execute("insert into bardienste (user_id, startdatetime, enddatetime) values ({},'{}','{}')".format(user.id, date, date + timedelta(days=1)))
|
||||||
|
@ -215,8 +200,8 @@ class DatabaseController(metaclass=Singleton):
|
||||||
self.db.close()
|
self.db.close()
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
|
@connected
|
||||||
def deleteWorker(self, user, date):
|
def deleteWorker(self, user, date):
|
||||||
self.connect()
|
|
||||||
try:
|
try:
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("delete from bardienste where user_id={} and startdatetime='{}'".format(user.id, date))
|
cursor.execute("delete from bardienste where user_id={} and startdatetime='{}'".format(user.id, date))
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
from functools import wraps
|
||||||
|
def login_required(**kwargs):
|
||||||
|
import geruecht.controller.accesTokenController as ac
|
||||||
|
from geruecht.model import BAR, USER, MONEY, GASTRO
|
||||||
|
from flask import request, jsonify
|
||||||
|
accessController = ac.AccesTokenController()
|
||||||
|
groups = [USER, BAR, GASTRO, MONEY]
|
||||||
|
if "groups" in kwargs:
|
||||||
|
groups = kwargs["groups"]
|
||||||
|
def real_decorator(func):
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
token = request.headers.get('Token')
|
||||||
|
accToken = accessController.validateAccessToken(token, groups)
|
||||||
|
kwargs['accToken'] = accToken
|
||||||
|
if accToken:
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "error", "message": "permission denied"}), 401
|
||||||
|
return wrapper
|
||||||
|
return real_decorator
|
|
@ -2,16 +2,17 @@ from flask import Blueprint, request, jsonify
|
||||||
from geruecht.finanzer import LOGGER
|
from geruecht.finanzer import LOGGER
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import geruecht.controller.userController as uc
|
import geruecht.controller.userController as uc
|
||||||
import geruecht.controller.accesTokenController as ac
|
|
||||||
from geruecht.model import MONEY
|
from geruecht.model import MONEY
|
||||||
|
from geruecht.decorator import login_required
|
||||||
|
|
||||||
finanzer = Blueprint("finanzer", __name__)
|
finanzer = Blueprint("finanzer", __name__)
|
||||||
|
|
||||||
accesTokenController = ac.AccesTokenController()
|
|
||||||
userController = uc.UserController()
|
userController = uc.UserController()
|
||||||
|
|
||||||
|
|
||||||
@finanzer.route("/getFinanzerMain")
|
@finanzer.route("/getFinanzerMain")
|
||||||
def _getFinanzer():
|
@login_required(groups=[MONEY])
|
||||||
|
def _getFinanzer(**kwargs):
|
||||||
""" Function for /getFinanzerMain
|
""" Function for /getFinanzerMain
|
||||||
|
|
||||||
Retrieves all User for the groupe 'moneymaster'
|
Retrieves all User for the groupe 'moneymaster'
|
||||||
|
@ -20,26 +21,20 @@ def _getFinanzer():
|
||||||
A JSON-File with Users
|
A JSON-File with Users
|
||||||
or ERROR 401 Permission Denied.
|
or ERROR 401 Permission Denied.
|
||||||
"""
|
"""
|
||||||
LOGGER.info("Get main for Finanzer")
|
LOGGER.debug("Get all Useres")
|
||||||
token = request.headers.get("Token")
|
users = userController.getAllUsersfromDB()
|
||||||
LOGGER.debug("Verify AccessToken with Token {}".format(token))
|
dic = {}
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY])
|
for user in users:
|
||||||
if accToken:
|
LOGGER.debug("Add User {} to ReturnValue".format(user))
|
||||||
LOGGER.debug("Get all Useres")
|
dic[user.uid] = user.toJSON()
|
||||||
users = userController.getAllUsersfromDB()
|
dic[user.uid]['creditList'] = {credit.year: credit.toJSON() for credit in user.geruechte}
|
||||||
dic = {}
|
LOGGER.debug("ReturnValue is {}".format(dic))
|
||||||
for user in users:
|
LOGGER.info("Send main for Finanzer")
|
||||||
LOGGER.debug("Add User {} to ReturnValue".format(user))
|
return jsonify(dic)
|
||||||
dic[user.uid] = user.toJSON()
|
|
||||||
dic[user.uid]['creditList'] = {credit.year: credit.toJSON() for credit in user.geruechte}
|
|
||||||
LOGGER.debug("ReturnValue is {}".format(dic))
|
|
||||||
LOGGER.info("Send main for Finanzer")
|
|
||||||
return jsonify(dic)
|
|
||||||
LOGGER.info("Permission Denied")
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
||||||
@finanzer.route("/finanzerAddAmount", methods=['POST'])
|
@finanzer.route("/finanzerAddAmount", methods=['POST'])
|
||||||
def _addAmount():
|
@login_required(groups=[MONEY])
|
||||||
|
def _addAmount(**kwargs):
|
||||||
""" Add Amount to User
|
""" Add Amount to User
|
||||||
|
|
||||||
This Function add an amount to the user with posted userID.
|
This Function add an amount to the user with posted userID.
|
||||||
|
@ -50,39 +45,32 @@ def _addAmount():
|
||||||
JSON-File with geruecht of year
|
JSON-File with geruecht of year
|
||||||
or ERROR 401 Permission Denied
|
or ERROR 401 Permission Denied
|
||||||
"""
|
"""
|
||||||
LOGGER.info("Add Amount")
|
data = request.get_json()
|
||||||
token = request.headers.get("Token")
|
LOGGER.debug("Get data {}".format(data))
|
||||||
LOGGER.debug("Verify AccessToken with Token {}".format(token))
|
userID = data['userId']
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY])
|
amount = int(data['amount'])
|
||||||
|
LOGGER.debug("UserID is {} and amount is {}".format(userID, amount))
|
||||||
if accToken:
|
try:
|
||||||
data = request.get_json()
|
year = int(data['year'])
|
||||||
LOGGER.debug("Get data {}".format(data))
|
except KeyError as er:
|
||||||
userID = data['userId']
|
LOGGER.error("KeyError in year. Year is set to default.")
|
||||||
amount = int(data['amount'])
|
year = datetime.now().year
|
||||||
LOGGER.debug("UserID is {} and amount is {}".format(userID, amount))
|
try:
|
||||||
try:
|
month = int(data['month'])
|
||||||
year = int(data['year'])
|
except KeyError as er:
|
||||||
except KeyError as er:
|
LOGGER.error("KeyError in month. Month is set to default.")
|
||||||
LOGGER.error("KeyError in year. Year is set to default.")
|
month = datetime.now().month
|
||||||
year = datetime.now().year
|
LOGGER.debug("Year is {} and Month is {}".format(year, month))
|
||||||
try:
|
userController.addAmount(userID, amount, year=year, month=month, finanzer=True)
|
||||||
month = int(data['month'])
|
user = userController.getUser(userID)
|
||||||
except KeyError as er:
|
retVal = {str(geruecht.year): geruecht.toJSON() for geruecht in user.geruechte}
|
||||||
LOGGER.error("KeyError in month. Month is set to default.")
|
retVal['locked'] = user.locked
|
||||||
month = datetime.now().month
|
LOGGER.info("Send updated Geruecht")
|
||||||
LOGGER.debug("Year is {} and Month is {}".format(year, month))
|
return jsonify(retVal)
|
||||||
userController.addAmount(userID, amount, year=year, month=month, finanzer=True)
|
|
||||||
user = userController.getUser(userID)
|
|
||||||
retVal = {str(geruecht.year): geruecht.toJSON() for geruecht in user.geruechte}
|
|
||||||
retVal['locked'] = user.locked
|
|
||||||
LOGGER.info("Send updated Geruecht")
|
|
||||||
return jsonify(retVal)
|
|
||||||
LOGGER.info("Permission Denied")
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
||||||
@finanzer.route("/finanzerAddCredit", methods=['POST'])
|
@finanzer.route("/finanzerAddCredit", methods=['POST'])
|
||||||
def _addCredit():
|
@login_required(groups=[MONEY])
|
||||||
|
def _addCredit(**kwargs):
|
||||||
""" Add Credit to User
|
""" Add Credit to User
|
||||||
|
|
||||||
This Function add an credit to the user with posted userID.
|
This Function add an credit to the user with posted userID.
|
||||||
|
@ -93,106 +81,79 @@ def _addCredit():
|
||||||
JSON-File with geruecht of year
|
JSON-File with geruecht of year
|
||||||
or ERROR 401 Permission Denied
|
or ERROR 401 Permission Denied
|
||||||
"""
|
"""
|
||||||
LOGGER.info("Add Amount")
|
data = request.get_json()
|
||||||
token = request.headers.get("Token")
|
print(data)
|
||||||
LOGGER.debug("Verify AccessToken with Token {}".format(token))
|
LOGGER.debug("Get data {}".format(data))
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY])
|
userID = data['userId']
|
||||||
|
credit = int(data['credit'])
|
||||||
|
LOGGER.debug("UserID is {} and credit is {}".format(userID, credit))
|
||||||
|
|
||||||
if accToken:
|
try:
|
||||||
|
year = int(data['year'])
|
||||||
|
except KeyError as er:
|
||||||
|
LOGGER.error("KeyError in year. Year is set to default.")
|
||||||
|
year = datetime.now().year
|
||||||
|
try:
|
||||||
|
month = int(data['month'])
|
||||||
|
except KeyError as er:
|
||||||
|
LOGGER.error("KeyError in month. Month is set to default.")
|
||||||
|
month = datetime.now().month
|
||||||
|
|
||||||
data = request.get_json()
|
LOGGER.debug("Year is {} and Month is {}".format(year, month))
|
||||||
print(data)
|
userController.addCredit(userID, credit, year=year, month=month).toJSON()
|
||||||
LOGGER.debug("Get data {}".format(data))
|
user = userController.getUser(userID)
|
||||||
userID = data['userId']
|
retVal = {str(geruecht.year): geruecht.toJSON() for geruecht in user.geruechte}
|
||||||
credit = int(data['credit'])
|
retVal['locked'] = user.locked
|
||||||
LOGGER.debug("UserID is {} and credit is {}".format(userID, credit))
|
LOGGER.info("Send updated Geruecht")
|
||||||
|
return jsonify(retVal)
|
||||||
|
|
||||||
try:
|
|
||||||
year = int(data['year'])
|
|
||||||
except KeyError as er:
|
|
||||||
LOGGER.error("KeyError in year. Year is set to default.")
|
|
||||||
year = datetime.now().year
|
|
||||||
try:
|
|
||||||
month = int(data['month'])
|
|
||||||
except KeyError as er:
|
|
||||||
LOGGER.error("KeyError in month. Month is set to default.")
|
|
||||||
month = datetime.now().month
|
|
||||||
|
|
||||||
LOGGER.debug("Year is {} and Month is {}".format(year, month))
|
|
||||||
userController.addCredit(userID, credit, year=year, month=month).toJSON()
|
|
||||||
user = userController.getUser(userID)
|
|
||||||
retVal = {str(geruecht.year): geruecht.toJSON() for geruecht in user.geruechte}
|
|
||||||
retVal['locked'] = user.locked
|
|
||||||
LOGGER.info("Send updated Geruecht")
|
|
||||||
return jsonify(retVal)
|
|
||||||
LOGGER.info("Permission Denied")
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
||||||
@finanzer.route("/finanzerLock", methods=['POST'])
|
@finanzer.route("/finanzerLock", methods=['POST'])
|
||||||
def _finanzerLock():
|
@login_required(groups=[MONEY])
|
||||||
token = request.headers.get("Token")
|
def _finanzerLock(**kwargs):
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY])
|
data = request.get_json()
|
||||||
|
username = data['userId']
|
||||||
|
locked = bool(data['locked'])
|
||||||
|
retVal = userController.lockUser(username, locked).toJSON()
|
||||||
|
return jsonify(retVal)
|
||||||
|
|
||||||
if accToken:
|
|
||||||
data = request.get_json()
|
|
||||||
username = data['userId']
|
|
||||||
locked = bool(data['locked'])
|
|
||||||
retVal = userController.lockUser(username, locked).toJSON()
|
|
||||||
return jsonify(retVal)
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
||||||
@finanzer.route("/finanzerSetConfig", methods=['POST'])
|
@finanzer.route("/finanzerSetConfig", methods=['POST'])
|
||||||
def _finanzerSetConfig():
|
@login_required(groups=[MONEY])
|
||||||
token = request.headers.get("Token")
|
def _finanzerSetConfig(**kwargs):
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY])
|
data = request.get_json()
|
||||||
|
username = data['userId']
|
||||||
if accToken:
|
autoLock = bool(data['autoLock'])
|
||||||
data = request.get_json()
|
limit = int(data['limit'])
|
||||||
username = data['userId']
|
retVal = userController.updateConfig(username, {'lockLimit': limit, 'autoLock': autoLock}).toJSON()
|
||||||
autoLock = bool(data['autoLock'])
|
return jsonify(retVal)
|
||||||
limit = int(data['limit'])
|
|
||||||
retVal = userController.updateConfig(username, {'lockLimit': limit, 'autoLock': autoLock}).toJSON()
|
|
||||||
return jsonify(retVal)
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
||||||
@finanzer.route("/finanzerAddUser", methods=['POST'])
|
@finanzer.route("/finanzerAddUser", methods=['POST'])
|
||||||
def _finanzerAddUser():
|
@login_required(groups=[MONEY])
|
||||||
token = request.headers.get("Token")
|
def _finanzerAddUser(**kwargs):
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY])
|
data = request.get_json()
|
||||||
|
username = data['userId']
|
||||||
if accToken:
|
userController.getUser(username)
|
||||||
data = request.get_json()
|
LOGGER.debug("Get all Useres")
|
||||||
username = data['userId']
|
users = userController.getAllUsersfromDB()
|
||||||
userController.getUser(username)
|
dic = {}
|
||||||
LOGGER.debug("Get all Useres")
|
for user in users:
|
||||||
users = userController.getAllUsersfromDB()
|
LOGGER.debug("Add User {} to ReturnValue".format(user))
|
||||||
dic = {}
|
dic[user.uid] = user.toJSON()
|
||||||
for user in users:
|
dic[user.uid]['creditList'] = {credit.year: credit.toJSON() for credit in user.geruechte}
|
||||||
LOGGER.debug("Add User {} to ReturnValue".format(user))
|
LOGGER.debug("ReturnValue is {}".format(dic))
|
||||||
dic[user.uid] = user.toJSON()
|
return jsonify(dic), 200
|
||||||
dic[user.uid]['creditList'] = {credit.year: credit.toJSON() for credit in user.geruechte}
|
|
||||||
LOGGER.debug("ReturnValue is {}".format(dic))
|
|
||||||
return jsonify(dic), 200
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
||||||
|
|
||||||
@finanzer.route("/finanzerSendOneMail", methods=['POST'])
|
@finanzer.route("/finanzerSendOneMail", methods=['POST'])
|
||||||
def _finanzerSendOneMail():
|
@login_required(groups=[MONEY])
|
||||||
token = request.headers.get("Token")
|
def _finanzerSendOneMail(**kwargs):
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY])
|
data = request.get_json()
|
||||||
|
username = data['userId']
|
||||||
if accToken:
|
retVal = userController.sendMail(username)
|
||||||
data = request.get_json()
|
return jsonify(retVal)
|
||||||
username = data['userId']
|
|
||||||
retVal = userController.sendMail(username)
|
|
||||||
return jsonify(retVal)
|
|
||||||
return jsonify({"error:", "permission denied"}), 401
|
|
||||||
|
|
||||||
@finanzer.route("/finanzerSendAllMail", methods=['GET'])
|
@finanzer.route("/finanzerSendAllMail", methods=['GET'])
|
||||||
def _finanzerSendAllMail():
|
@login_required(groups=[MONEY])
|
||||||
token = request.headers.get("Token")
|
def _finanzerSendAllMail(**kwargs):
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY])
|
retVal = userController.sendAllMail()
|
||||||
|
return jsonify(retVal)
|
||||||
if accToken:
|
|
||||||
retVal = userController.sendAllMail()
|
|
||||||
return jsonify(retVal)
|
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
|
@ -1,33 +1,30 @@
|
||||||
from flask import Blueprint, request, jsonify
|
from flask import Blueprint, request, jsonify
|
||||||
import geruecht.controller as gc
|
from geruecht.decorator import login_required
|
||||||
import geruecht.controller.userController as uc
|
import geruecht.controller.userController as uc
|
||||||
import geruecht.controller.accesTokenController as ac
|
|
||||||
from geruecht.model import USER
|
from geruecht.model import USER
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
user = Blueprint("user", __name__)
|
user = Blueprint("user", __name__)
|
||||||
|
|
||||||
accesTokenController = ac.AccesTokenController()
|
|
||||||
userController = uc.UserController()
|
userController = uc.UserController()
|
||||||
|
|
||||||
@user.route("/user/main")
|
|
||||||
def _main():
|
|
||||||
|
|
||||||
token = request.headers.get("Token")
|
@user.route("/user/main")
|
||||||
accToken = accesTokenController.validateAccessToken(token, [USER])
|
@login_required(groups=[USER])
|
||||||
if accToken:
|
def _main(**kwargs):
|
||||||
|
if 'accToken' in kwargs:
|
||||||
|
accToken = kwargs['accToken']
|
||||||
accToken.user = userController.getUser(accToken.user.uid)
|
accToken.user = userController.getUser(accToken.user.uid)
|
||||||
retVal = accToken.user.toJSON()
|
retVal = accToken.user.toJSON()
|
||||||
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte}
|
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte}
|
||||||
return jsonify(retVal)
|
return jsonify(retVal)
|
||||||
return jsonify({"error": "permission denied"}), 401
|
return jsonify("error", "something went wrong"), 500
|
||||||
|
|
||||||
@user.route("/user/addAmount", methods=['POST'])
|
@user.route("/user/addAmount", methods=['POST'])
|
||||||
def _addAmount():
|
@login_required(groups=[USER])
|
||||||
|
def _addAmount(**kwargs):
|
||||||
token = request.headers.get("Token")
|
if 'accToken' in kwargs:
|
||||||
accToken = accesTokenController.validateAccessToken(token, [USER])
|
accToken = kwargs['accToken']
|
||||||
if accToken:
|
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
amount = int(data['amount'])
|
amount = int(data['amount'])
|
||||||
date = datetime.now()
|
date = datetime.now()
|
||||||
|
@ -36,4 +33,4 @@ def _addAmount():
|
||||||
retVal = accToken.user.toJSON()
|
retVal = accToken.user.toJSON()
|
||||||
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte}
|
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte}
|
||||||
return jsonify(retVal)
|
return jsonify(retVal)
|
||||||
return jsonify({"error": "permission denied"}), 401
|
return jsonify({"error": "something went wrong"}), 500
|
|
@ -1,24 +1,25 @@
|
||||||
from flask import Blueprint, request, jsonify
|
from flask import Blueprint, request, jsonify
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from geruecht.controller import accesTokenController, userController
|
import geruecht.controller.userController as uc
|
||||||
|
from geruecht.decorator import login_required
|
||||||
from geruecht.model import MONEY, GASTRO
|
from geruecht.model import MONEY, GASTRO
|
||||||
|
|
||||||
vorstand = Blueprint("vorstand", __name__)
|
vorstand = Blueprint("vorstand", __name__)
|
||||||
|
userController = uc.UserController()
|
||||||
|
|
||||||
|
|
||||||
@vorstand.route("/sm/addUser", methods=['POST', 'GET'])
|
@vorstand.route("/sm/addUser", methods=['POST', 'GET'])
|
||||||
|
|
||||||
|
@login_required(groups=[MONEY, GASTRO])
|
||||||
def _addUser():
|
def _addUser():
|
||||||
|
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
return "<h1>HEllo World</h1>"
|
return "<h1>HEllo World</h1>"
|
||||||
|
|
||||||
token = request.headers.get("Token")
|
data = request.get_json()
|
||||||
accToken = accesTokenController.validateAccessToken(token, [MONEY, GASTRO])
|
user = data['user']
|
||||||
if accToken:
|
date = datetime.utcfromtimestamp(int(data['date']))
|
||||||
data = request.get_json()
|
userController.addWorker(user['username'], date)
|
||||||
user = data['user']
|
|
||||||
date = datetime.utcfromtimestamp(int(data['date']))
|
|
||||||
userController.addWorker(user['username'], date)
|
|
||||||
|
|
||||||
print(data)
|
print(data)
|
||||||
return jsonify({"date": date})
|
return jsonify({"date": date})
|
||||||
return jsonify({"error": "permission denied"}), 401
|
|
Loading…
Reference in New Issue