from flask import Blueprint, request, jsonify from geruecht import BAR, db from geruecht.routes import verifyAccessToken from geruecht.model.user import User from datetime import datetime baruser = Blueprint("baruser", __name__) @baruser.route("/bar") def _bar(): """ Main function for Baruser Returns JSON-file with all Users, who hast amounts in this month. Returns: JSON-File with Users, who has amounts in this month or ERROR 401 Permission Denied """ print(request.headers) token = request.headers.get("Token") print(token) accToken = verifyAccessToken(token, BAR) dic = {} if accToken is not None: users = User.query.all() for user in users: geruecht = None geruecht = user.getGeruecht() if geruecht is not None: month = geruecht.getMonth(datetime.now().month) amount = abs(month[0] - month[1]) if amount != 0: dic[user.userID] = {"username": user.username, "firstname": user.firstname, "lastname": user.lastname, "amount": abs(month[0] - month[1]) } return jsonify(dic) return jsonify({"error": "permission denied"}), 401 @baruser.route("/baradd", methods=['POST']) def _baradd(): """ Function for Baruser to add amount This function added to the user with the posted userID the posted amount. Returns: JSON-File with userID and the amount or ERROR 401 Permission Denied """ token = request.headers.get("Token") print(token) accToken = verifyAccessToken(token, BAR) if accToken is not None: data = request.get_json() userID = data['userId'] amount = int(data['amount']) user = User.query.filter_by(userID=userID).first() month = user.addAmount(amount) amount = abs(month[0] - month[1]) return jsonify({"userId": user.userID, "amount": amount}) return jsonify({"error", "permission denied"}), 401 @baruser.route("/barGetUsers") def _getUsers(): """ Get Users without amount This Function returns all Users, who hasn't an amount in this month. Returns: JSON-File with Users or ERROR 401 Permission Denied """ token = request.headers.get("Token") print(token) accToken = verifyAccessToken(token, BAR) retVal = {} if accToken is not None: users = User.query.all() for user in users: month = user.getGeruecht().getMonth() if month == 0: retVal[user.userID] = {user.toJSON()} return jsonify(retVal) return jsonify({"error": "permission denied"}), 401 @baruser.route("/barGetUser", methods=['POST']) def _getUser(): """ Get specified User This function returns the user with posted userID and them amount and credit. Returns: JSON-File with userID, amount and credit or ERROR 401 Permission Denied """ token = request.headers.get("Token") print(token) accToken = verifyAccessToken(token, BAR) if accToken is not None: data = request.get_json() userID = data['userId'] user = User.query.filter_by(userID=userID) month = user.getGeruecht().getMonth() return jsonify({"userId": user.userID, "amount": month[1], "credit": month[0]}) return jsonify({"error": "permission denied"}), 401