flaschengeist/geruecht/finanzer/routes.py

207 lines
7.0 KiB
Python

from flask import Blueprint, request, jsonify
from datetime import datetime
import geruecht.controller.userController as uc
from geruecht.model import MONEY
from geruecht.decorator import login_required
from geruecht.logger import getDebugLogger, getCreditLogger
debug = getDebugLogger()
creditL = getCreditLogger()
finanzer = Blueprint("finanzer", __name__)
userController = uc.UserController()
@finanzer.route("/getFinanzerMain")
@login_required(groups=[MONEY])
def _getFinanzer(**kwargs):
""" Function for /getFinanzerMain
Retrieves all User for the groupe 'moneymaster'
Returns:
A JSON-File with Users
or ERROR 401 Permission Denied.
"""
debug.info("/getFinanzerMain")
try:
users = userController.getAllUsersfromDB()
dic = {}
for user in users:
dic[user.uid] = user.toJSON()
dic[user.uid]['creditList'] = {
credit.year: credit.toJSON() for credit in user.geruechte}
debug.debug("return {{ {} }}".format(dic))
return jsonify(dic)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@finanzer.route("/finanzerAddAmount", methods=['POST'])
@login_required(groups=[MONEY])
def _addAmount(**kwargs):
""" 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
"""
debug.info("/finanzerAddAmount")
try:
data = request.get_json()
userID = data['userId']
amount = int(data['amount'])
try:
year = int(data['year'])
except KeyError:
year = datetime.now().year
try:
month = int(data['month'])
except KeyError:
month = datetime.now().month
userController.addAmount(
userID, amount, year=year, month=month, finanzer=True)
user = userController.getUser(userID)
retVal = {str(geruecht.year): geruecht.toJSON()
for geruecht in user.geruechte}
retVal['locked'] = user.locked
debug.debug("return {{ {} }}".format(retVal))
creditL.info("{} Finanzer {} {} fügt {} {} {} € Schulden hinzu.".format(datetime(year, month, 1).date(
), kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, user.firstname, user.lastname, amount/100))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@finanzer.route("/finanzerAddCredit", methods=['POST'])
@login_required(groups=[MONEY])
def _addCredit(**kwargs):
""" 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
"""
debug.info("/finanzerAddCredit")
try:
data = request.get_json()
userID = data['userId']
credit = int(data['credit'])
try:
year = int(data['year'])
except KeyError:
year = datetime.now().year
try:
month = int(data['month'])
except KeyError:
month = datetime.now().month
userController.addCredit(
userID, credit, year=year, month=month).toJSON()
user = userController.getUser(userID)
retVal = {str(geruecht.year): geruecht.toJSON()
for geruecht in user.geruechte}
retVal['locked'] = user.locked
debug.debug("return {{ {} }}".format(retVal))
creditL.info("{} Finanzer {} {} fügt {} {} {} € Guthaben hinzu.".format(datetime(year, month, 1).date(
), kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, user.firstname, user.lastname, credit / 100))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@finanzer.route("/finanzerLock", methods=['POST'])
@login_required(groups=[MONEY])
def _finanzerLock(**kwargs):
debug.info("/finanzerLock")
try:
data = request.get_json()
username = data['userId']
locked = bool(data['locked'])
retVal = userController.lockUser(username, locked).toJSON()
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@finanzer.route("/finanzerSetConfig", methods=['POST'])
@login_required(groups=[MONEY])
def _finanzerSetConfig(**kwargs):
debug.info("/finanzerSetConfig")
try:
data = request.get_json()
username = data['userId']
autoLock = bool(data['autoLock'])
limit = int(data['limit'])
retVal = userController.updateConfig(
username, {'lockLimit': limit, 'autoLock': autoLock}).toJSON()
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@finanzer.route("/finanzerAddUser", methods=['POST'])
@login_required(groups=[MONEY])
def _finanzerAddUser(**kwargs):
debug.info("/finanzerAddUser")
try:
data = request.get_json()
username = data['userId']
userController.getUser(username)
users = userController.getAllUsersfromDB()
dic = {}
for user in users:
dic[user.uid] = user.toJSON()
dic[user.uid]['creditList'] = {
credit.year: credit.toJSON() for credit in user.geruechte}
debug.debug("return {{ {} }}".format(dic))
return jsonify(dic), 200
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@finanzer.route("/finanzerSendOneMail", methods=['POST'])
@login_required(groups=[MONEY])
def _finanzerSendOneMail(**kwargs):
debug.info("/finanzerSendOneMail")
try:
data = request.get_json()
username = data['userId']
retVal = userController.sendMail(username)
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@finanzer.route("/finanzerSendAllMail", methods=['GET'])
@login_required(groups=[MONEY])
def _finanzerSendAllMail(**kwargs):
debug.info("/finanzerSendAllMail")
try:
retVal = userController.sendAllMail()
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500