flaschengeist/geruecht/finanzer/routes.py

108 lines
3.1 KiB
Python
Raw Normal View History

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 an Error.
example:
"""
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():
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():
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():
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