flaschengeist/geruecht/finanzer/routes.py

160 lines
5.9 KiB
Python
Raw Normal View History

from flask import Blueprint, request, jsonify
from geruecht.finanzer import LOGGER
from datetime import datetime
from geruecht import MONEY, db
from geruecht.routes import verifyAccessToken
finanzer = Blueprint("finanzer", __name__)
@finanzer.route("/getFinanzerMain")
def _getFinanzer():
""" Function for /getFinanzerMain
Retrieves all User for the groupe 'moneymaster'
Returns:
2019-05-02 16:50:59 +00:00
A JSON-File with Users
or ERROR 401 Permission Denied.
"""
LOGGER.info("Get main for Finanzer")
token = request.headers.get("Token")
LOGGER.debug("Verify AccessToken with Token {}".format(token))
accToken = verifyAccessToken(token, MONEY)
if accToken is not None:
LOGGER.debug("Get all Useres")
users = db.getAllUser()
dic = {}
for user in users:
LOGGER.debug("Add User {} to ReturnValue".format(user))
dic[user.cn] = user.toJSON()
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("/getFinanzerYears", methods=['POST'])
def _getFinanzerYear():
2019-05-02 16:50:59 +00:00
""" Get all geruechte from User
This function returns all geruechte from user with posted userID
Returns:
JSON-File with geruechte of special user
or ERROR 401 Permission Denied
"""
LOGGER.info("Get all Geruechte from User.")
token = request.headers.get("Token")
LOGGER.debug("Verify AccessToken with Token {}".format(token))
accToken = verifyAccessToken(token, MONEY)
dic = {}
if accToken is not None:
data = request.get_json()
LOGGER.debug("Get data {}".format(data))
userID = data['userId']
LOGGER.debug("UserID is {}".format(userID))
user = db.getUser(userID)
LOGGER.debug("User is {}".format(user))
dic[user.cn] = {}
LOGGER.debug("Build ReturnValue")
for geruecht in user.geruechte:
LOGGER.debug("Add Geruecht {} to ReturnValue".format(geruecht))
dic[user.cn][geruecht.year] = geruecht.toJSON()
LOGGER.debug("ReturnValue is {}".format(dic))
LOGGER.info("Send Geruechte from User {}".format(user))
return jsonify(dic)
LOGGER.info("Permission Denied")
return jsonify({"error": "permission denied"}), 401
@finanzer.route("/finanzerAddAmount", methods=['POST'])
def _addAmount():
2019-05-02 16:50:59 +00:00
""" Add Amount to User
This Function add an amount to the user with posted userID.
If year is not posted the default is the actual Year.
If month is not posted the default is the actual Month.
Returns:
JSON-File with geruecht of year
or ERROR 401 Permission Denied
"""
LOGGER.info("Add Amount")
token = request.headers.get("Token")
LOGGER.debug("Verify AccessToken with Token {}".format(token))
accToken = verifyAccessToken(token, MONEY)
if accToken is not None:
data = request.get_json()
LOGGER.debug("Get data {}".format(data))
userID = data['userId']
amount = int(data['amount'])
LOGGER.debug("UserID is {} and amount is {}".format(userID, amount))
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))
user = db.getUser(userID)
LOGGER.debug("User is {}".format(user))
LOGGER.debug("Add amount to User {} in year {} and month {}".format(user, year, month))
user.addAmount(amount, year=year, month=month)
retVal = user.getGeruecht(year=year).toJSON()
LOGGER.info("Send updated Geruecht")
return jsonify(retVal)
LOGGER.info("Permission Denied")
return jsonify({"error": "permission denied"}), 401
@finanzer.route("/finanzerAddCredit", methods=['POST'])
def _addCredit():
2019-05-02 16:50:59 +00:00
""" Add Credit to User
This Function add an credit to the user with posted userID.
If year is not posted the default is the actual Year.
If month is not posted the default is the actual Month.
Returns:
JSON-File with geruecht of year
or ERROR 401 Permission Denied
"""
LOGGER.info("Add Amount")
token = request.headers.get("Token")
LOGGER.debug("Verify AccessToken with Token {}".format(token))
accToken = verifyAccessToken(token, MONEY)
if accToken is not None:
data = request.get_json()
LOGGER.debug("Get data {}".format(data))
userID = data['userId']
credit = int(data['credit'])
LOGGER.debug("UserID is {} and credit is {}".format(userID, credit))
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))
user = db.getUser(userID)
LOGGER.debug("User is {}".format(user))
LOGGER.debug("Add credit to User {} in year {} and month {}".format(user, year, month))
user.addCredit(credit, year=year, month=month)
retVal = user.getGeruecht(year=year).toJSON()
LOGGER.info("Send updated Geruecht")
return jsonify(retVal)
LOGGER.info("Permission Denied")
return jsonify({"error": "permission denied"}), 401