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

@ -3,7 +3,7 @@ from datetime import datetime
class CreditList(db.Model):
""" DataBase Object Credit List:
Attributes:
id: id in Database. Is the Primary Key
<month>_guthaben: Credit of the Month.
@ -150,7 +150,7 @@ class CreditList(db.Model):
def toJSON(self):
""" Create Dic to dump in JSON
Returns:
A Dic with static Attributes.
"""
@ -192,4 +192,4 @@ class CreditList(db.Model):
"credit": self.dez_guthaben,
"depts": self.dez_schulden},
}
return dic
return dic

View File

@ -5,7 +5,7 @@ from datetime import datetime
class User(db.Model):
""" Database Object for User
Table for all safed User
Attributes:
@ -42,7 +42,7 @@ class User(db.Model):
if geruecht.year == datetime.now().year - 1:
print('fonud last geruecht {}'.format(geruecht))
last = geruecht
if last:
amount = last.getSchulden()
return self.createCurrentGeruecht(amount=amount)
@ -60,7 +60,7 @@ class User(db.Model):
def toJSON(self):
""" Create Dic to dump in JSON
Returns:
A Dic with static Attributes.
"""
@ -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
@ -20,7 +15,7 @@ def verifyAccessToken(token, group):
token: Token to verify.
group: Group like 'moneymaster', 'gastro', 'user' or 'bar'
Returns:
An the AccesToken for this given Token or None.
An the AccesToken for this given Token or None.
"""
accToken = accesTokenController.findAccesToken(token)
print(accToken)
@ -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")
@ -91,7 +45,7 @@ def _valid():
@app.route("/login", methods=['POST'])
def _login():
""" Login User
Nothing to say.
Login in User and create an AccessToken for the User.
@ -111,55 +65,7 @@ def _login():
return jsonify(dic)
else:
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
return jsonify({"error": "wrong username"}), 402
@app.route("/getFinanzer")
def getFinanzer():