flaschengeist/geruecht/finanzer/routes.py

135 lines
4.0 KiB
Python

from flask import Blueprint, request, jsonify
from datetime import datetime
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:
A JSON-File with Users
or ERROR 401 Permission Denied.
"""
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():
""" 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
"""
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)
return jsonify({"error": "permission denied"}), 401
@finanzer.route("/finanzerAddAmount", methods=['POST'])
def _addAmount():
""" 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
"""
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():
""" 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
"""
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