clean up code, extra routes for finanzer and baruser

This commit is contained in:
Tim Gröger 2019-05-01 22:43:28 +02:00
parent cb58f12698
commit 52ca1caa52
8 changed files with 122 additions and 105 deletions

View File

@ -24,5 +24,15 @@ accesTokenController.start()
# login_manager.login_view = 'login'
# login_manager.login_message_category = 'info'
MONEY = "moneymaster"
GASTRO = "gastro"
USER = "user"
BAR = "bar"
from geruecht import routes
from geruecht.baruser.routes import baruser
from geruecht.finanzer.routes import finanzer
app.register_blueprint(baruser)
app.register_blueprint(finanzer)

View File

View File

@ -0,0 +1,54 @@
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():
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.getCurrentGeruecht()
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():
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()
geruecht = user.getCurrentGeruecht()
month = geruecht.addAmount(amount)
amount = abs(month[0] - month[1])
db.session.add(geruecht)
db.session.commit()
return jsonify({"userId": user.userID, "amount": amount})
return jsonify({"error", "permission denied"}), 401

View File

View File

@ -0,0 +1,48 @@
from flask import Blueprint, request, jsonify
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

View File

@ -82,4 +82,3 @@ class User(db.Model):
A Bool. True if the password is correct and False if it isn't.
"""
return True if bcrypt.check_password_hash(self.password, password) else False

View File

@ -1,15 +1,10 @@
from geruecht import app, db, accesTokenController
from geruecht import app, db, accesTokenController, MONEY, BAR, USER, GASTRO
from geruecht.model.user import User
from geruecht.model.creditList import CreditList
from geruecht.model.priceList import PriceList
from datetime import datetime
from flask import request, jsonify
MONEY = "moneymaster"
GASTRO = "gastro"
USER = "user"
BAR = "bar"
def verifyAccessToken(token, group):
""" Verify Accestoken
@ -30,47 +25,6 @@ def verifyAccessToken(token, group):
return accToken
return None
@app.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
@app.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
@app.route("/valid")
def _valid():
token = request.headers.get("Token")
@ -113,54 +67,6 @@ def _login():
return jsonify({"error": "wrong password"}), 401
return jsonify({"error": "wrong username"}), 402
@app.route("/bar")
def _bar():
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.getCurrentGeruecht()
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
@app.route("/baradd", methods=['POST'])
def _baradd():
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()
geruecht = user.getCurrentGeruecht()
month = geruecht.addAmount(amount)
amount = abs(month[0] - month[1])
db.session.add(geruecht)
db.session.commit()
return jsonify({"userId": user.userID, "amount": amount})
return jsonify({"error", "permission denied"}), 401
@app.route("/getFinanzer")
def getFinanzer():
users = User.query.all()