task #23 und #22 wurden bearbeitet.

This commit is contained in:
Tim Gröger 2019-04-24 00:08:25 +02:00
parent 7bec023d57
commit 360766bd35
5 changed files with 253 additions and 9 deletions

View File

@ -88,7 +88,7 @@ class AccesTokenController(Thread):
print("start allocate") print("start allocate")
for accToken in self.tokenList: for accToken in self.tokenList:
print("controle", accToken) print("controle", accToken)
if (datetime.now() - accToken.timestamp).seconds > 60: if (datetime.now() - accToken.timestamp).seconds > 7200:
print("delete", accToken) print("delete", accToken)
self.tokenList.remove(accToken) self.tokenList.remove(accToken)
else: else:

View File

@ -11,6 +11,7 @@ class CreditList(db.Model):
last_schulden: Debt or Credit of last Year. last_schulden: Debt or Credit of last Year.
year: Year of all Credits and Debts. year: Year of all Credits and Debts.
user_id: id from the User.
TODO: link to user??? TODO: link to user???
""" """
@ -57,3 +58,139 @@ class CreditList(db.Model):
year = db.Column(db.Integer, nullable=False, default=datetime.now().year) year = db.Column(db.Integer, nullable=False, default=datetime.now().year)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def getSchulden(self):
jan = self.jan_guthaben - self.jan_schulden
feb = self.feb_guthaben - self.feb_schulden
maer = self.maer_guthaben - self.maer_schulden
apr = self.apr_guthaben - self.apr_schulden
mai = self.mai_guthaben - self.mai_schulden
jun = self.jun_guthaben - self.jun_schulden
jul = self.jul_guthaben - self.jul_schulden
aug = self.aug_guthaben - self.aug_schulden
sep = self.sep_guthaben - self.sep_schulden
okt = self.okt_guthaben - self.okt_schulden
nov = self.nov_guthaben - self.nov_schulden
dez = self.dez_guthaben - self.dez_schulden
sum = jan + feb + maer + apr + mai + jun + jul + aug + sep + okt + nov + dez + self.last_schulden
return sum
def getMonth(self, month):
retValue = None
if month == 1:
retValue = (self.jan_guthaben, self.jan_schulden)
elif month == 2:
retValue = (self.feb_guthaben, self.feb_schulden)
elif month == 3:
retValue = (self.mear_guthaben, self.maer_schulden)
elif month == 4:
retValue = (self.apr_guthaben, self.apr_schulden)
elif month == 5:
retValue = (self.mai_guthaben, self.mai_schulden)
elif month == 6:
retValue = (self.jun_guthaben, self.jun_schulden)
elif month == 7:
retValue = (self.jul_guthaben, self.jul_schulden)
elif month == 8:
retValue = (self.aug_guthaben, self.aug_schulden)
elif month == 9:
retValue = (self.sep_guthaben, self.sep_schulden)
elif month == 10:
retValue = (self.okt_guthaben, self.okt_schulden)
elif month == 11:
retValue = (self.nov_guthaben, self.nov_schulden)
elif month == 12:
retValue = (self.dez_guthaben, self.dez_schulden)
return retValue
def addAmount(self, amount):
month = datetime.now().month
if month == 1:
self.jan_schulden += amount
retValue = (self.jan_guthaben, self.jan_schulden)
elif month == 2:
self.feb_schulden += amount
retValue = (self.feb_guthaben, self.feb_schulden)
elif month == 3:
self.maer_schulden += amount
retValue = (self.maer_guthaben, self.maer_schulden)
elif month == 4:
self.apr_schulden += amount
retValue = (self.apr_guthaben, self.apr_schulden)
elif month == 5:
self.mai_schulden += amount
retValue = (self.mai_guthaben, self.mai_schulden)
elif month == 6:
self.jun_schulden += amount
retValue = (self.jun_guthaben, self.jun_schulden)
elif month == 7:
self.jul_schulden += amount
retValue = (self.jul_guthaben, self.jul_schulden)
elif month == 8:
self.aug_schulden += amount
retValue = (self.aug_guthaben, self.aug_schulden)
elif month == 9:
self.sep_schulden += amount
retValue = (self.sep_guthaben, self.sep_schulden)
elif month == 10:
self.okt_schulden += amount
retValue = (self.okt_guthaben, self.okt_schulden)
elif month == 11:
self.nov_schulden += amount
retValue = (self.nov_guthaben, self.nov_schulden)
elif month == 12:
self.dez_schulden += amount
retValue = (self.dez_guthaben, self.dez_schulden)
return retValue
def toJSON(self):
""" Create Dic to dump in JSON
Returns:
A Dic with static Attributes.
"""
dic = {
"year": self.year,
"jan": {
"credit": self.jan_guthaben,
"depts": self.jan_schulden},
"feb": {
"credit": self.feb_guthaben,
"depts": self.feb_schulden},
"maer": {
"credit": self.maer_guthaben,
"depts": self.maer_schulden},
"apr": {
"credit": self.apr_guthaben,
"depts": self.apr_schulden},
"mai": {
"credit": self.mai_guthaben,
"depts": self.mai_schulden},
"jun": {
"credit": self.jun_guthaben,
"depts": self.jun_schulden},
"jul": {
"credit": self.jul_guthaben,
"depts": self.jul_schulden},
"aug": {
"credit": self.aug_guthaben,
"depts": self.aug_schulden},
"sep": {
"credit": self.sep_guthaben,
"depts": self.sep_schulden},
"okt": {
"credit": self.okt_guthaben,
"depts": self.okt_schulden},
"nov": {
"credit": self.nov_guthaben,
"depts": self.nov_schulden},
"dez": {
"credit": self.dez_guthaben,
"depts": self.dez_schulden},
}
return dic

View File

@ -1,5 +1,7 @@
from geruecht import db from geruecht import db
from geruecht import bcrypt from geruecht import bcrypt
from geruecht.model.creditList import CreditList
from datetime import datetime
class User(db.Model): class User(db.Model):
""" Database Object for User """ Database Object for User
@ -25,6 +27,37 @@ class User(db.Model):
geruechte = db.relationship('CreditList', backref='user', lazy=True) geruechte = db.relationship('CreditList', backref='user', lazy=True)
def getCurrentGeruecht(self):
print('search currentGeruecht in user {}'.format(self.userID))
if len(self.geruechte) == 0:
print('user {} has no geruechte'.format(self.userID))
return self.createCurrentGeruecht()
print('iterate throw geruechte')
last = None
for geruecht in self.geruechte:
print('geruecht {}'.format(geruecht))
if geruecht.year == datetime.now().year:
print('found geruecht {}'.format(geruecht))
return geruecht
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)
else:
print('error, no geruecht found and no geruecht created')
def createCurrentGeruecht(self, amount=0):
print('create currentgeruecht for user {} in year {}'.format(self.userID, datetime.now().year))
credit = CreditList(user_id=self.id, last_schulden=amount)
db.session.add(credit)
db.session.commit()
credit = CreditList.query.filter_by(year=datetime.now().year).first()
print('reated currentgeruecht {}'.format(credit))
return credit
def toJSON(self): def toJSON(self):
""" Create Dic to dump in JSON """ Create Dic to dump in JSON

View File

@ -2,6 +2,7 @@ from geruecht import app, db, accesTokenController
from geruecht.model.user import User from geruecht.model.user import User
from geruecht.model.creditList import CreditList from geruecht.model.creditList import CreditList
from geruecht.model.priceList import PriceList from geruecht.model.priceList import PriceList
from datetime import datetime
from flask import request, jsonify from flask import request, jsonify
MONEY = "moneymaster" MONEY = "moneymaster"
@ -29,7 +30,7 @@ def verifyAccessToken(token, group):
return accToken return accToken
return None return None
@app.route("/getFinanzerMain", methods=['POST']) @app.route("/getFinanzerMain")
def _getFinanzer(): def _getFinanzer():
""" Function for /getFinanzerMain """ Function for /getFinanzerMain
@ -40,8 +41,7 @@ def _getFinanzer():
example: example:
""" """
data = request.get_json() token = request.headers.get("Token")
token = data["token"]
accToken = verifyAccessToken(token, MONEY) accToken = verifyAccessToken(token, MONEY)
if accToken is not None: if accToken is not None:
@ -52,11 +52,38 @@ def _getFinanzer():
return jsonify(dic) return jsonify(dic)
return jsonify({"error": "permission denied"}), 401 return jsonify({"error": "permission denied"}), 401
@app.route("/valid", methods=['POST']) @app.route("/getFinanzerYears", methods=['POST'])
def _valid(): def _getFinanzerYear():
data = request.get_json() print(request.headers)
token = data["token"] token = request.headers.get("Token")
print(token)
accToken = verifyAccessToken(token, MONEY) 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")
accToken = verifyAccessToken(token, MONEY)
if accToken is not None:
return jsonify(accToken.user.toJSON())
accToken = verifyAccessToken(token, BAR)
if accToken is not None:
return jsonify(accToken.user.toJSON())
accToken = verifyAccessToken(token, GASTRO)
if accToken is not None:
return jsonify(accToken.user.toJSON())
accToken = verifyAccessToken(token, USER)
if accToken is not None: if accToken is not None:
return jsonify(accToken.user.toJSON()) return jsonify(accToken.user.toJSON())
return jsonify({"error": "permission denied"}), 401 return jsonify({"error": "permission denied"}), 401
@ -86,6 +113,53 @@ def _login():
return jsonify({"error": "wrong password"}), 401 return jsonify({"error": "wrong password"}), 401
return jsonify({"error": "wrong username"}), 402 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") @app.route("/getFinanzer")
def getFinanzer(): def getFinanzer():

Binary file not shown.