2019-05-01 20:43:28 +00:00
|
|
|
from flask import Blueprint, request, jsonify
|
2019-05-02 23:40:13 +00:00
|
|
|
from geruecht.finanzer import LOGGER
|
2019-05-02 00:21:50 +00:00
|
|
|
from datetime import datetime
|
2019-05-01 20:43:28 +00:00
|
|
|
from geruecht import MONEY
|
|
|
|
from geruecht.routes import verifyAccessToken
|
|
|
|
from geruecht.model.user import User
|
|
|
|
|
|
|
|
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.
|
2019-05-01 20:43:28 +00:00
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Get main for Finanzer")
|
2019-05-01 20:43:28 +00:00
|
|
|
token = request.headers.get("Token")
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Verify AccessToken with Token {}".format(token))
|
2019-05-01 20:43:28 +00:00
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
|
|
|
if accToken is not None:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Get all Useres")
|
2019-05-01 20:43:28 +00:00
|
|
|
users = User.query.all()
|
|
|
|
dic = {}
|
|
|
|
for user in users:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Add User {} to ReturnValue".format(user))
|
2019-05-01 20:43:28 +00:00
|
|
|
dic[user.userID] = user.toJSON()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("ReturnValue is {}".format(dic))
|
|
|
|
LOGGER.info("Send main for Finanzer")
|
2019-05-01 20:43:28 +00:00
|
|
|
return jsonify(dic)
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Permission Denied")
|
2019-05-01 20:43:28 +00:00
|
|
|
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
|
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Get all Geruechte from User.")
|
2019-05-01 20:43:28 +00:00
|
|
|
token = request.headers.get("Token")
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Verify AccessToken with Token {}".format(token))
|
2019-05-01 20:43:28 +00:00
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
|
|
|
|
|
|
|
dic = {}
|
|
|
|
if accToken is not None:
|
|
|
|
data = request.get_json()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Get data {}".format(data))
|
2019-05-01 20:43:28 +00:00
|
|
|
userID = data['userId']
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("UserID is {}".format(userID))
|
2019-05-01 20:43:28 +00:00
|
|
|
user = User.query.filter_by(userID=userID).first()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("User is {}".format(user))
|
2019-05-01 20:43:28 +00:00
|
|
|
dic[user.userID] = {}
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Build ReturnValue")
|
2019-05-01 20:43:28 +00:00
|
|
|
for geruecht in user.geruechte:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Add Geruecht {} to ReturnValue".format(geruecht))
|
2019-05-01 20:43:28 +00:00
|
|
|
dic[user.userID][geruecht.year] = geruecht.toJSON()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("ReturnValue is {}".format(dic))
|
|
|
|
LOGGER.info("Send Geruechte from User {}".format(user))
|
2019-05-01 20:43:28 +00:00
|
|
|
return jsonify(dic)
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Permission Denied")
|
2019-05-02 00:21:50 +00:00
|
|
|
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
|
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Add Amount")
|
2019-05-02 00:21:50 +00:00
|
|
|
token = request.headers.get("Token")
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Verify AccessToken with Token {}".format(token))
|
2019-05-02 00:21:50 +00:00
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
|
|
|
|
|
|
|
if accToken is not None:
|
|
|
|
data = request.get_json()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Get data {}".format(data))
|
2019-05-02 00:21:50 +00:00
|
|
|
userID = data['userId']
|
|
|
|
amount = int(data['amount'])
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("UserID is {} and amount is {}".format(userID, amount))
|
2019-05-02 00:21:50 +00:00
|
|
|
try:
|
|
|
|
year = int(data['year'])
|
|
|
|
except KeyError as er:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.error("KeyError in year. Year is set to default.")
|
2019-05-02 00:21:50 +00:00
|
|
|
year = datetime.now().year
|
|
|
|
try:
|
|
|
|
month = int(data['month'])
|
|
|
|
except KeyError as er:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.error("KeyError in month. Month is set to default.")
|
2019-05-02 00:21:50 +00:00
|
|
|
month = datetime.now().month
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Year is {} and Month is {}".format(year, month))
|
2019-05-02 00:21:50 +00:00
|
|
|
user = User.query.filter_by(userID=userID).first()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("User is {}".format(user))
|
|
|
|
LOGGER.debug("Add amount to User {} in year {} and month {}".format(user, year, month))
|
2019-05-02 00:21:50 +00:00
|
|
|
user.addAmount(amount, year=year, month=month)
|
|
|
|
retVal = user.getGeruecht(year=year).toJSON()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Send updated Geruecht")
|
2019-05-02 00:21:50 +00:00
|
|
|
return jsonify(retVal)
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Permission Denied")
|
2019-05-02 00:21:50 +00:00
|
|
|
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
|
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Add Amount")
|
2019-05-02 00:21:50 +00:00
|
|
|
token = request.headers.get("Token")
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Verify AccessToken with Token {}".format(token))
|
2019-05-02 00:21:50 +00:00
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
|
|
|
|
|
|
|
if accToken is not None:
|
|
|
|
data = request.get_json()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Get data {}".format(data))
|
2019-05-02 00:21:50 +00:00
|
|
|
userID = data['userId']
|
|
|
|
credit = int(data['credit'])
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("UserID is {} and credit is {}".format(userID, credit))
|
2019-05-02 00:21:50 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
year = int(data['year'])
|
|
|
|
except KeyError as er:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.error("KeyError in year. Year is set to default.")
|
2019-05-02 00:21:50 +00:00
|
|
|
year = datetime.now().year
|
|
|
|
try:
|
|
|
|
month = int(data['month'])
|
|
|
|
except KeyError as er:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.error("KeyError in month. Month is set to default.")
|
2019-05-02 00:21:50 +00:00
|
|
|
month = datetime.now().month
|
|
|
|
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Year is {} and Month is {}".format(year, month))
|
2019-05-02 00:21:50 +00:00
|
|
|
user = User.query.filter_by(userID=userID).first()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("User is {}".format(user))
|
|
|
|
LOGGER.debug("Add credit to User {} in year {} and month {}".format(user, year, month))
|
2019-05-02 00:21:50 +00:00
|
|
|
user.addCredit(credit, year=year, month=month)
|
|
|
|
retVal = user.getGeruecht(year=year).toJSON()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Send updated Geruecht")
|
2019-05-02 00:21:50 +00:00
|
|
|
return jsonify(retVal)
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Permission Denied")
|
2019-05-02 00:21:50 +00:00
|
|
|
return jsonify({"error": "permission denied"}), 401
|