2019-05-01 20:43:28 +00:00
|
|
|
from flask import Blueprint, request, jsonify
|
2020-01-18 23:37:40 +00:00
|
|
|
import geruecht.controller.ldapController as lc
|
2020-05-22 19:55:14 +00:00
|
|
|
import geruecht.controller.mainController as mc
|
2020-06-06 11:17:18 +00:00
|
|
|
import geruecht.controller.accesTokenController as ac
|
2019-05-01 20:43:28 +00:00
|
|
|
from datetime import datetime
|
2020-06-06 11:17:18 +00:00
|
|
|
from geruecht.model import BAR, MONEY, USER, VORSTAND, EXTERN
|
2020-01-19 08:07:45 +00:00
|
|
|
from geruecht.decorator import login_required
|
2020-03-10 08:19:11 +00:00
|
|
|
from geruecht.logger import getDebugLogger, getCreditLogger
|
|
|
|
|
|
|
|
debug = getDebugLogger()
|
|
|
|
creditL = getCreditLogger()
|
2019-05-01 20:43:28 +00:00
|
|
|
|
|
|
|
baruser = Blueprint("baruser", __name__)
|
|
|
|
|
2020-03-10 10:08:24 +00:00
|
|
|
ldap = lc.LDAPController()
|
2020-05-22 19:55:14 +00:00
|
|
|
mainController = mc.MainController()
|
2020-06-06 11:17:18 +00:00
|
|
|
accesTokenController = ac.AccesTokenController()
|
2020-01-18 23:37:40 +00:00
|
|
|
|
2020-01-19 08:07:45 +00:00
|
|
|
|
2019-05-01 20:43:28 +00:00
|
|
|
@baruser.route("/bar")
|
2020-03-17 19:37:01 +00:00
|
|
|
@login_required(groups=[BAR], bar=True)
|
2020-01-19 08:07:45 +00:00
|
|
|
def _bar(**kwargs):
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Main function for Baruser
|
2019-05-02 23:40:13 +00:00
|
|
|
|
2019-05-02 16:50:59 +00:00
|
|
|
Returns JSON-file with all Users, who hast amounts in this month.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
JSON-File with Users, who has amounts in this month
|
|
|
|
or ERROR 401 Permission Denied
|
|
|
|
"""
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/bar")
|
|
|
|
try:
|
|
|
|
dic = {}
|
2020-05-22 19:55:14 +00:00
|
|
|
users = mainController.getAllUsersfromDB()
|
2020-03-10 08:19:11 +00:00
|
|
|
for user in users:
|
|
|
|
geruecht = None
|
|
|
|
geruecht = user.getGeruecht(datetime.now().year)
|
|
|
|
if geruecht is not None:
|
|
|
|
all = geruecht.getSchulden()
|
|
|
|
if all != 0:
|
|
|
|
if all >= 0:
|
|
|
|
type = 'credit'
|
|
|
|
else:
|
|
|
|
type = 'amount'
|
|
|
|
dic[user.uid] = {"username": user.uid,
|
2020-03-10 10:08:24 +00:00
|
|
|
"firstname": user.firstname,
|
|
|
|
"lastname": user.lastname,
|
|
|
|
"amount": all,
|
|
|
|
"locked": user.locked,
|
2020-06-04 18:56:20 +00:00
|
|
|
"type": type,
|
|
|
|
"limit": user.limit
|
2020-03-10 10:08:24 +00:00
|
|
|
}
|
|
|
|
debug.debug("return {{ {} }}".format(dic))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(dic)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
2020-01-19 08:07:45 +00:00
|
|
|
|
2019-05-01 20:43:28 +00:00
|
|
|
|
|
|
|
@baruser.route("/baradd", methods=['POST'])
|
2020-03-17 19:37:01 +00:00
|
|
|
@login_required(groups=[BAR], bar=True)
|
2020-01-19 08:07:45 +00:00
|
|
|
def _baradd(**kwargs):
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Function for Baruser to add amount
|
|
|
|
|
|
|
|
This function added to the user with the posted userID the posted amount.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
JSON-File with userID and the amount
|
|
|
|
or ERROR 401 Permission Denied
|
|
|
|
"""
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/baradd")
|
|
|
|
try:
|
|
|
|
data = request.get_json()
|
|
|
|
userID = data['userId']
|
|
|
|
amount = int(data['amount'])
|
2020-03-10 18:23:52 +00:00
|
|
|
amountl = amount
|
2020-03-10 08:19:11 +00:00
|
|
|
date = datetime.now()
|
2020-05-22 19:55:14 +00:00
|
|
|
mainController.addAmount(
|
2020-03-10 10:08:24 +00:00
|
|
|
userID, amount, year=date.year, month=date.month)
|
2020-05-22 19:55:14 +00:00
|
|
|
user = mainController.getUser(userID)
|
2020-03-10 08:19:11 +00:00
|
|
|
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()
|
2020-05-11 21:07:26 +00:00
|
|
|
dic['amount'] = all
|
2020-03-10 08:19:11 +00:00
|
|
|
dic['type'] = type
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(dic))
|
|
|
|
creditL.info("{} Baruser {} {} fügt {} {} {} € Schulden hinzu.".format(
|
2020-03-10 18:23:52 +00:00
|
|
|
date, kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, user.firstname, user.lastname, amountl/100))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(dic)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
2020-01-19 08:07:45 +00:00
|
|
|
|
2019-05-02 13:39:53 +00:00
|
|
|
|
|
|
|
@baruser.route("/barGetUsers")
|
2020-03-17 19:37:01 +00:00
|
|
|
@login_required(groups=[BAR, MONEY], bar=True)
|
2020-01-19 08:07:45 +00:00
|
|
|
def _getUsers(**kwargs):
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Get Users without amount
|
|
|
|
|
|
|
|
This Function returns all Users, who hasn't an amount in this month.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
JSON-File with Users
|
|
|
|
or ERROR 401 Permission Denied
|
|
|
|
"""
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/barGetUsers")
|
|
|
|
try:
|
|
|
|
retVal = {}
|
|
|
|
retVal = ldap.getAllUser()
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
2019-05-02 13:39:53 +00:00
|
|
|
|
2020-03-10 10:08:24 +00:00
|
|
|
|
2020-02-19 22:11:24 +00:00
|
|
|
@baruser.route("/bar/storno", methods=['POST'])
|
2020-03-17 19:37:01 +00:00
|
|
|
@login_required(groups=[BAR], bar=True)
|
2020-02-19 22:11:24 +00:00
|
|
|
def _storno(**kwargs):
|
|
|
|
""" Function for Baruser to storno amount
|
|
|
|
|
|
|
|
This function added to the user with the posted userID the posted amount.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
JSON-File with userID and the amount
|
|
|
|
or ERROR 401 Permission Denied
|
|
|
|
"""
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/bar/storno")
|
|
|
|
try:
|
|
|
|
data = request.get_json()
|
|
|
|
userID = data['userId']
|
|
|
|
amount = int(data['amount'])
|
2020-03-10 18:23:52 +00:00
|
|
|
amountl = amount
|
2020-03-10 08:19:11 +00:00
|
|
|
date = datetime.now()
|
2020-05-22 19:55:14 +00:00
|
|
|
mainController.addCredit(
|
2020-03-10 10:08:24 +00:00
|
|
|
userID, amount, year=date.year, month=date.month)
|
2020-05-22 19:55:14 +00:00
|
|
|
user = mainController.getUser(userID)
|
2020-03-10 08:19:11 +00:00
|
|
|
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()
|
2020-05-11 21:07:26 +00:00
|
|
|
dic['amount'] = all
|
2020-03-10 08:19:11 +00:00
|
|
|
dic['type'] = type
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(dic))
|
|
|
|
creditL.info("{} Baruser {} {} storniert {} € von {} {}".format(
|
2020-03-10 18:23:52 +00:00
|
|
|
date, kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, amountl/100, user.firstname, user.lastname))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(dic)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
2019-12-29 16:55:21 +00:00
|
|
|
|
2020-03-10 10:08:24 +00:00
|
|
|
|
2020-01-19 08:07:45 +00:00
|
|
|
@baruser.route("/barGetUser", methods=['POST'])
|
2020-03-17 19:37:01 +00:00
|
|
|
@login_required(groups=[BAR], bar=True)
|
2020-01-19 08:07:45 +00:00
|
|
|
def _getUser(**kwargs):
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/barGetUser")
|
|
|
|
try:
|
|
|
|
data = request.get_json()
|
|
|
|
username = data['userId']
|
2020-05-22 19:55:14 +00:00
|
|
|
user = mainController.getUser(username)
|
2020-03-10 08:19:11 +00:00
|
|
|
amount = user.getGeruecht(datetime.now().year).getSchulden()
|
|
|
|
if amount >= 0:
|
|
|
|
type = 'credit'
|
|
|
|
else:
|
|
|
|
type = 'amount'
|
|
|
|
|
|
|
|
retVal = user.toJSON()
|
|
|
|
retVal['amount'] = amount
|
|
|
|
retVal['type'] = type
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
2019-05-02 13:39:53 +00:00
|
|
|
|
2020-03-10 10:08:24 +00:00
|
|
|
|
2020-03-04 20:11:41 +00:00
|
|
|
@baruser.route("/search", methods=['GET'])
|
2020-05-11 21:07:26 +00:00
|
|
|
@login_required(groups=[BAR, MONEY, USER, VORSTAND], bar=True)
|
2020-01-19 08:07:45 +00:00
|
|
|
def _search(**kwargs):
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/search")
|
|
|
|
try:
|
|
|
|
retVal = ldap.getAllUser()
|
|
|
|
for user in retVal:
|
|
|
|
if user['username'] == 'extern':
|
|
|
|
retVal.remove(user)
|
|
|
|
break
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
2020-03-17 19:37:01 +00:00
|
|
|
|
2020-05-11 21:07:26 +00:00
|
|
|
@baruser.route("/bar/lock", methods=['GET', 'POST'])
|
2020-03-17 19:37:01 +00:00
|
|
|
@login_required(groups=[BAR], bar=True)
|
|
|
|
def _lockbar(**kwargs):
|
2020-05-11 21:07:26 +00:00
|
|
|
|
2020-03-17 19:37:01 +00:00
|
|
|
debug.info('/bar/lock')
|
|
|
|
accToken = kwargs['accToken']
|
2020-05-11 21:07:26 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
data = request.get_json()
|
|
|
|
accToken.lock_bar = data['value']
|
2020-06-06 11:17:18 +00:00
|
|
|
accToken = accesTokenController.updateAccessToken(accToken)
|
|
|
|
accToken = accesTokenController.validateAccessToken(accToken.token, [USER, EXTERN])
|
2020-03-17 19:37:01 +00:00
|
|
|
debug.debug('return {{ "value": {} }}'.format(accToken.lock_bar))
|
|
|
|
return jsonify({'value': accToken.lock_bar})
|
2020-06-05 20:53:27 +00:00
|
|
|
|