flaschengeist/geruecht/baruser/routes.py

195 lines
6.5 KiB
Python
Raw Normal View History

from flask import Blueprint, request, jsonify
import geruecht.controller.ldapController as lc
import geruecht.controller.userController as uc
from datetime import datetime
2020-03-04 20:11:41 +00:00
from geruecht.model import BAR, MONEY, USER, VORSTAND
from geruecht.decorator import login_required
from geruecht.logger import getDebugLogger, getCreditLogger
debug = getDebugLogger()
creditL = getCreditLogger()
baruser = Blueprint("baruser", __name__)
ldap= lc.LDAPController()
userController = uc.UserController()
@baruser.route("/bar")
@login_required(groups=[BAR])
def _bar(**kwargs):
2019-05-02 16:50:59 +00:00
""" Main function for Baruser
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
"""
debug.info("/bar")
try:
dic = {}
users = userController.getAllUsersfromDB()
for user in users:
geruecht = None
geruecht = user.getGeruecht(datetime.now().year)
if geruecht is not None:
month = geruecht.getMonth(datetime.now().month)
amount = month[0] - month[1]
all = geruecht.getSchulden()
if all != 0:
if all >= 0:
type = 'credit'
else:
type = 'amount'
dic[user.uid] = {"username": user.uid,
"firstname": user.firstname,
"lastname": user.lastname,
"amount": all,
"locked": user.locked,
"type": type
}
debug.debug("return {{}}".format(dic))
return jsonify(dic)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@baruser.route("/baradd", methods=['POST'])
@login_required(groups=[BAR])
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
"""
debug.info("/baradd")
try:
data = request.get_json()
userID = data['userId']
amount = int(data['amount'])
date = datetime.now()
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
debug.debug("return {{}}".format(dic))
creditL.info("{} Baruser {} {} fügt {} {} {} € Schulden hinzu.".format(date, kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, user.firstname, user.lastname, amount/100))
return jsonify(dic)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@baruser.route("/barGetUsers")
@login_required(groups=[BAR, MONEY])
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
"""
debug.info("/barGetUsers")
try:
retVal = {}
retVal = ldap.getAllUser()
debug.debug("return {{}}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
2020-02-19 22:11:24 +00:00
@baruser.route("/bar/storno", methods=['POST'])
@login_required(groups=[BAR])
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
"""
debug.info("/bar/storno")
try:
data = request.get_json()
userID = data['userId']
amount = int(data['amount'])
date = datetime.now()
userController.addCredit(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
debug.debug("return {{}}".format(dic))
creditL.info("{} Baruser {} {} storniert {} € von {} {}".format(date, kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, amount/100, user.firstname, user.lastname))
return jsonify(dic)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@baruser.route("/barGetUser", methods=['POST'])
@login_required(groups=[BAR])
def _getUser(**kwargs):
debug.info("/barGetUser")
try:
data = request.get_json()
username = data['userId']
user = userController.getUser(username)
amount = user.getGeruecht(datetime.now().year).getSchulden()
if amount >= 0:
type = 'credit'
else:
type = 'amount'
retVal = user.toJSON()
retVal['amount'] = amount
retVal['type'] = type
debug.debug("return {{}}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
2020-03-04 20:11:41 +00:00
@baruser.route("/search", methods=['GET'])
@login_required(groups=[BAR, MONEY, USER,VORSTAND])
def _search(**kwargs):
debug.info("/search")
try:
retVal = ldap.getAllUser()
for user in retVal:
if user['username'] == 'extern':
retVal.remove(user)
break
debug.debug("return {{}}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500