2019-05-01 20:43:28 +00:00
|
|
|
from flask import Blueprint, request, jsonify
|
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
|
|
|
"""
|
|
|
|
token = request.headers.get("Token")
|
|
|
|
|
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
|
|
|
if accToken is not None:
|
|
|
|
users = User.query.all()
|
|
|
|
dic = {}
|
|
|
|
for user in users:
|
|
|
|
dic[user.userID] = user.toJSON()
|
|
|
|
return jsonify(dic)
|
|
|
|
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-01 20:43:28 +00:00
|
|
|
print(request.headers)
|
|
|
|
token = request.headers.get("Token")
|
|
|
|
print(token)
|
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
|
|
|
|
|
|
|
dic = {}
|
|
|
|
if accToken is not None:
|
|
|
|
data = request.get_json()
|
|
|
|
userID = data['userId']
|
|
|
|
|
|
|
|
user = User.query.filter_by(userID=userID).first()
|
|
|
|
dic[user.userID] = {}
|
|
|
|
for geruecht in user.geruechte:
|
|
|
|
dic[user.userID][geruecht.year] = geruecht.toJSON()
|
|
|
|
return jsonify(dic)
|
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 00:21:50 +00:00
|
|
|
print(request.headers)
|
|
|
|
token = request.headers.get("Token")
|
|
|
|
print(token)
|
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
|
|
|
|
|
|
|
if accToken is not None:
|
|
|
|
data = request.get_json()
|
|
|
|
userID = data['userId']
|
|
|
|
amount = int(data['amount'])
|
|
|
|
|
|
|
|
try:
|
|
|
|
year = int(data['year'])
|
|
|
|
except KeyError as er:
|
|
|
|
print("Error: ", er)
|
|
|
|
year = datetime.now().year
|
|
|
|
try:
|
|
|
|
month = int(data['month'])
|
|
|
|
except KeyError as er:
|
|
|
|
print("Error: ", er)
|
|
|
|
month = datetime.now().month
|
|
|
|
|
|
|
|
user = User.query.filter_by(userID=userID).first()
|
|
|
|
user.addAmount(amount, year=year, month=month)
|
|
|
|
retVal = user.getGeruecht(year=year).toJSON()
|
|
|
|
return jsonify(retVal)
|
|
|
|
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 00:21:50 +00:00
|
|
|
print(request.headers)
|
|
|
|
token = request.headers.get("Token")
|
|
|
|
print(token)
|
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
|
|
|
|
|
|
|
if accToken is not None:
|
|
|
|
data = request.get_json()
|
|
|
|
userID = data['userId']
|
|
|
|
credit = int(data['credit'])
|
|
|
|
|
|
|
|
try:
|
|
|
|
year = int(data['year'])
|
|
|
|
except KeyError as er:
|
|
|
|
print("Error: ", er)
|
|
|
|
year = datetime.now().year
|
|
|
|
try:
|
|
|
|
month = int(data['month'])
|
|
|
|
except KeyError as er:
|
|
|
|
print("Error: ", er)
|
|
|
|
month = datetime.now().month
|
|
|
|
|
|
|
|
user = User.query.filter_by(userID=userID).first()
|
|
|
|
user.addCredit(credit, year=year, month=month)
|
|
|
|
retVal = user.getGeruecht(year=year).toJSON()
|
|
|
|
return jsonify(retVal)
|
|
|
|
return jsonify({"error": "permission denied"}), 401
|