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-12-28 20:52:49 +00:00
|
|
|
from geruecht.controller import accesTokenController, userController
|
|
|
|
from geruecht.model import MONEY
|
2019-05-01 20:43:28 +00:00
|
|
|
|
|
|
|
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-12-28 10:31:45 +00:00
|
|
|
accToken = accesTokenController.validateAccessToken(token, MONEY)
|
|
|
|
if accToken:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Get all Useres")
|
2019-12-28 20:52:49 +00:00
|
|
|
users = userController.getAllUsersfromDB()
|
2019-05-01 20:43:28 +00:00
|
|
|
dic = {}
|
|
|
|
for user in users:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Add User {} to ReturnValue".format(user))
|
2019-12-28 20:52:49 +00:00
|
|
|
dic[user.uid] = user.toJSON()
|
|
|
|
dic[user.uid]['creditList'] = {credit.year: credit.toJSON() for credit in user.geruechte}
|
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
|
|
|
|
|
2019-05-02 00:21:50 +00:00
|
|
|
@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-12-28 10:31:45 +00:00
|
|
|
accToken = accesTokenController.validateAccessToken(token, MONEY)
|
2019-05-02 00:21:50 +00:00
|
|
|
|
2019-12-28 10:31:45 +00:00
|
|
|
if accToken:
|
2019-05-02 00:21:50 +00:00
|
|
|
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-12-29 16:55:21 +00:00
|
|
|
userController.addAmount(userID, amount, year=year, month=month, finanzer=True)
|
2019-12-28 21:40:33 +00:00
|
|
|
retVal = {geruecht.year: geruecht.toJSON() for geruecht in userController.getUser(userID).geruechte}
|
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-12-28 10:31:45 +00:00
|
|
|
accToken = accesTokenController.validateAccessToken(token, MONEY)
|
2019-05-02 00:21:50 +00:00
|
|
|
|
2019-12-28 10:31:45 +00:00
|
|
|
if accToken:
|
2019-12-26 09:28:30 +00:00
|
|
|
|
2019-05-02 00:21:50 +00:00
|
|
|
data = request.get_json()
|
2019-12-26 09:28:30 +00:00
|
|
|
print(data)
|
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-12-28 20:52:49 +00:00
|
|
|
retVal = userController.addCredit(userID, credit, year=year, month=month).toJSON()
|
2019-12-28 21:40:33 +00:00
|
|
|
retVal = {geruecht.year: geruecht.toJSON() for geruecht in userController.getUser(userID).geruechte}
|
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
|
2019-12-29 16:55:21 +00:00
|
|
|
|
|
|
|
@finanzer.route("/finanzerLock", methods=['POST'])
|
|
|
|
def _finanzerLock():
|
|
|
|
token = request.headers.get("Token")
|
|
|
|
accToken = accesTokenController.validateAccessToken(token, MONEY)
|
|
|
|
|
|
|
|
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'])
|
|
|
|
def _finanzerSetConfig():
|
|
|
|
token = request.headers.get("Token")
|
|
|
|
accToken = accesTokenController.validateAccessToken(token, MONEY)
|
|
|
|
|
|
|
|
if accToken:
|
|
|
|
data = request.get_json()
|
|
|
|
username = data['userId']
|
|
|
|
autoLock = bool(data['autoLock'])
|
|
|
|
limit = int(data['limit'])
|
|
|
|
retVal = userController.updateConfig(username, {'lockLimit': limit, 'autoLock': autoLock}).toJSON()
|
|
|
|
return jsonify(retVal)
|
|
|
|
return jsonify({"error": "permission denied"}), 401
|