2019-05-01 20:43:28 +00:00
|
|
|
from flask import Blueprint, request, jsonify
|
2019-12-28 20:52:49 +00:00
|
|
|
from geruecht.controller import ldapController as ldap, accesTokenController, userController
|
2019-05-01 20:43:28 +00:00
|
|
|
from datetime import datetime
|
2019-12-29 20:36:42 +00:00
|
|
|
from geruecht.model import BAR, MONEY
|
2019-05-01 20:43:28 +00:00
|
|
|
|
|
|
|
baruser = Blueprint("baruser", __name__)
|
|
|
|
|
|
|
|
@baruser.route("/bar")
|
|
|
|
def _bar():
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Main function for Baruser
|
2019-05-02 23:40:13 +00:00
|
|
|
|
2019-05-02 16:50:59 +00:00
|
|
|
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
|
|
|
|
"""
|
2019-05-01 20:43:28 +00:00
|
|
|
print(request.headers)
|
|
|
|
token = request.headers.get("Token")
|
|
|
|
print(token)
|
2019-12-28 10:31:45 +00:00
|
|
|
accToken = accesTokenController.validateAccessToken(token, BAR)
|
2019-05-01 20:43:28 +00:00
|
|
|
|
|
|
|
dic = {}
|
2019-12-28 10:31:45 +00:00
|
|
|
if accToken:
|
2019-12-28 20:52:49 +00:00
|
|
|
users = userController.getAllUsersfromDB()
|
2019-05-01 20:43:28 +00:00
|
|
|
for user in users:
|
|
|
|
geruecht = None
|
2019-12-28 20:52:49 +00:00
|
|
|
geruecht = user.getGeruecht(datetime.now().year)
|
2019-05-01 20:43:28 +00:00
|
|
|
if geruecht is not None:
|
|
|
|
month = geruecht.getMonth(datetime.now().month)
|
2019-12-22 21:27:39 +00:00
|
|
|
amount = month[0] - month[1]
|
2019-12-29 20:57:59 +00:00
|
|
|
all = geruecht.getSchulden()
|
2020-01-05 13:15:02 +00:00
|
|
|
if all != 0:
|
2019-12-29 20:57:59 +00:00
|
|
|
if all >= 0:
|
2019-12-22 21:27:39 +00:00
|
|
|
type = 'credit'
|
|
|
|
else:
|
|
|
|
type = 'amount'
|
2019-12-28 20:52:49 +00:00
|
|
|
dic[user.uid] = {"username": user.uid,
|
2019-05-01 20:43:28 +00:00
|
|
|
"firstname": user.firstname,
|
|
|
|
"lastname": user.lastname,
|
2019-12-29 20:57:59 +00:00
|
|
|
"amount": abs(all),
|
2019-12-29 16:55:21 +00:00
|
|
|
"locked": user.locked,
|
2019-12-22 21:27:39 +00:00
|
|
|
"type": type
|
2019-05-01 20:43:28 +00:00
|
|
|
}
|
|
|
|
return jsonify(dic)
|
|
|
|
return jsonify({"error": "permission denied"}), 401
|
|
|
|
|
|
|
|
@baruser.route("/baradd", methods=['POST'])
|
|
|
|
def _baradd():
|
2019-05-02 16:50:59 +00:00
|
|
|
""" 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
|
|
|
|
"""
|
2019-05-01 20:43:28 +00:00
|
|
|
token = request.headers.get("Token")
|
|
|
|
print(token)
|
2019-12-28 10:31:45 +00:00
|
|
|
accToken = accesTokenController.validateAccessToken(token, BAR)
|
2019-05-01 20:43:28 +00:00
|
|
|
|
2019-12-28 10:31:45 +00:00
|
|
|
if accToken:
|
2019-05-01 20:43:28 +00:00
|
|
|
data = request.get_json()
|
|
|
|
userID = data['userId']
|
|
|
|
amount = int(data['amount'])
|
|
|
|
|
2019-12-28 20:52:49 +00:00
|
|
|
date = datetime.now()
|
|
|
|
userController.addAmount(userID, amount, year=date.year, month=date.month)
|
|
|
|
user = userController.getUser(userID)
|
2019-12-29 20:57:59 +00:00
|
|
|
geruecht = user.getGeruecht(year=date.year)
|
|
|
|
month = geruecht.getMonth(month=date.month)
|
2019-05-02 00:21:50 +00:00
|
|
|
amount = abs(month[0] - month[1])
|
2019-12-29 20:57:59 +00:00
|
|
|
all = geruecht.getSchulden()
|
|
|
|
if all >= 0:
|
|
|
|
type = 'credit'
|
|
|
|
else:
|
|
|
|
type = 'amount'
|
2020-01-14 19:00:37 +00:00
|
|
|
dic = user.toJSON()
|
|
|
|
dic['amount'] = abs(all)
|
2019-05-01 20:43:28 +00:00
|
|
|
|
2020-01-14 19:00:37 +00:00
|
|
|
return jsonify(dic)
|
2019-05-01 20:43:28 +00:00
|
|
|
return jsonify({"error", "permission denied"}), 401
|
2019-05-02 13:39:53 +00:00
|
|
|
|
|
|
|
@baruser.route("/barGetUsers")
|
|
|
|
def _getUsers():
|
2019-05-02 16:50:59 +00:00
|
|
|
""" 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
|
|
|
|
"""
|
2019-05-02 13:39:53 +00:00
|
|
|
token = request.headers.get("Token")
|
|
|
|
print(token)
|
2019-12-28 10:31:45 +00:00
|
|
|
accToken = accesTokenController.validateAccessToken(token, BAR)
|
2019-05-02 13:39:53 +00:00
|
|
|
|
|
|
|
retVal = {}
|
2019-12-28 10:31:45 +00:00
|
|
|
if accToken:
|
2019-12-26 09:28:30 +00:00
|
|
|
retVal = ldap.getAllUser()
|
2019-05-02 13:39:53 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
return jsonify({"error": "permission denied"}), 401
|
|
|
|
|
2019-12-29 16:55:21 +00:00
|
|
|
@baruser.route("/barGetUser", methods=['POST'])
|
|
|
|
def _getUser():
|
|
|
|
token = request.headers.get("Token")
|
|
|
|
accToken = accesTokenController.validateAccessToken(token, BAR)
|
|
|
|
if accToken:
|
|
|
|
data = request.get_json()
|
|
|
|
username = data['userId']
|
2020-01-05 13:15:02 +00:00
|
|
|
user = userController.getUser(username)
|
|
|
|
amount = user.getGeruecht(datetime.now().year).getSchulden()
|
|
|
|
if amount >= 0:
|
|
|
|
type = 'credit'
|
|
|
|
else:
|
|
|
|
type = 'amount'
|
|
|
|
|
|
|
|
retVal = user.toJSON()
|
|
|
|
retVal['amount'] = amount
|
|
|
|
retVal['type'] = type
|
2019-12-29 16:55:21 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
return jsonify("error", "permission denied"), 401
|
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
@baruser.route("/search", methods=['POST'])
|
|
|
|
def _search():
|
2019-05-02 13:39:53 +00:00
|
|
|
token = request.headers.get("Token")
|
|
|
|
print(token)
|
2019-12-28 10:31:45 +00:00
|
|
|
accToken = accesTokenController.validateAccessToken(token, BAR)
|
2019-12-29 20:36:42 +00:00
|
|
|
accToken2 = accesTokenController.validateAccessToken(token, MONEY)
|
2019-05-02 13:39:53 +00:00
|
|
|
|
2019-12-29 20:36:42 +00:00
|
|
|
if accToken or accToken2:
|
2019-05-02 13:39:53 +00:00
|
|
|
data = request.get_json()
|
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
searchString = data['searchString']
|
2019-05-02 13:39:53 +00:00
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
retVal = ldap.searchUser(searchString)
|
|
|
|
|
|
|
|
return jsonify(retVal)
|
2019-05-02 13:39:53 +00:00
|
|
|
return jsonify({"error": "permission denied"}), 401
|