task #22 und task #26 abgeschlossen

es werden dynamisch neue geruechte erstellt, wenn nötig.
Finanzer kann sowohl Guthaben als auch Schulden hinzufügen, egal zu welcher Zeit
This commit is contained in:
Tim Gröger 2019-05-02 02:21:50 +02:00
parent 52ca1caa52
commit 2427d94626
5 changed files with 166 additions and 37 deletions

View File

@ -18,7 +18,7 @@ def _bar():
users = User.query.all() users = User.query.all()
for user in users: for user in users:
geruecht = None geruecht = None
geruecht = user.getCurrentGeruecht() geruecht = user.getGeruecht()
if geruecht is not None: if geruecht is not None:
month = geruecht.getMonth(datetime.now().month) month = geruecht.getMonth(datetime.now().month)
amount = abs(month[0] - month[1]) amount = abs(month[0] - month[1])
@ -43,12 +43,9 @@ def _baradd():
amount = int(data['amount']) amount = int(data['amount'])
user = User.query.filter_by(userID=userID).first() user = User.query.filter_by(userID=userID).first()
geruecht = user.getCurrentGeruecht() month = user.addAmount(amount)
month = geruecht.addAmount(amount)
amount = abs(month[0] - month[1])
db.session.add(geruecht) amount = abs(month[0] - month[1])
db.session.commit()
return jsonify({"userId": user.userID, "amount": amount}) return jsonify({"userId": user.userID, "amount": amount})
return jsonify({"error", "permission denied"}), 401 return jsonify({"error", "permission denied"}), 401

View File

@ -1,4 +1,5 @@
from flask import Blueprint, request, jsonify from flask import Blueprint, request, jsonify
from datetime import datetime
from geruecht import MONEY from geruecht import MONEY
from geruecht.routes import verifyAccessToken from geruecht.routes import verifyAccessToken
from geruecht.model.user import User from geruecht.model.user import User
@ -45,4 +46,62 @@ def _getFinanzerYear():
for geruecht in user.geruechte: for geruecht in user.geruechte:
dic[user.userID][geruecht.year] = geruecht.toJSON() dic[user.userID][geruecht.year] = geruecht.toJSON()
return jsonify(dic) return jsonify(dic)
return jsonify({"error": "permission denied"}), 401 return jsonify({"error": "permission denied"}), 401
@finanzer.route("/finanzerAddAmount", methods=['POST'])
def _addAmount():
print(request.headers)
token = request.headers.get("Token")
print(token)
accToken = verifyAccessToken(token, MONEY)
if accToken is not None:
data = request.get_json()
userID = data['userId']
amount = int(data['amount'])
try:
year = int(data['year'])
except KeyError as er:
print("Error: ", er)
year = datetime.now().year
try:
month = int(data['month'])
except KeyError as er:
print("Error: ", er)
month = datetime.now().month
user = User.query.filter_by(userID=userID).first()
user.addAmount(amount, year=year, month=month)
retVal = user.getGeruecht(year=year).toJSON()
return jsonify(retVal)
return jsonify({"error": "permission denied"}), 401
@finanzer.route("/finanzerAddCredit", methods=['POST'])
def _addCredit():
print(request.headers)
token = request.headers.get("Token")
print(token)
accToken = verifyAccessToken(token, MONEY)
if accToken is not None:
data = request.get_json()
userID = data['userId']
credit = int(data['credit'])
try:
year = int(data['year'])
except KeyError as er:
print("Error: ", er)
year = datetime.now().year
try:
month = int(data['month'])
except KeyError as er:
print("Error: ", er)
month = datetime.now().month
user = User.query.filter_by(userID=userID).first()
user.addCredit(credit, year=year, month=month)
retVal = user.getGeruecht(year=year).toJSON()
return jsonify(retVal)
return jsonify({"error": "permission denied"}), 401

View File

@ -73,7 +73,7 @@ class CreditList(db.Model):
nov = self.nov_guthaben - self.nov_schulden nov = self.nov_guthaben - self.nov_schulden
dez = self.dez_guthaben - self.dez_schulden dez = self.dez_guthaben - self.dez_schulden
sum = jan + feb + maer + apr + mai + jun + jul + aug + sep + okt + nov + dez + self.last_schulden sum = jan + feb + maer + apr + mai + jun + jul + aug + sep + okt + nov + dez - self.last_schulden
return sum return sum
def getMonth(self, month): def getMonth(self, month):
@ -106,8 +106,7 @@ class CreditList(db.Model):
return retValue return retValue
def addAmount(self, amount): def addAmount(self, amount, month=datetime.now().month):
month = datetime.now().month
if month == 1: if month == 1:
self.jan_schulden += amount self.jan_schulden += amount
@ -146,6 +145,51 @@ class CreditList(db.Model):
self.dez_schulden += amount self.dez_schulden += amount
retValue = (self.dez_guthaben, self.dez_schulden) retValue = (self.dez_guthaben, self.dez_schulden)
db.session.commit()
return retValue
def addCredit(self, credit, month=datetime.now().month):
if month == 1:
self.jan_guthaben += credit
retValue = (self.jan_guthaben, self.jan_schulden)
elif month == 2:
self.feb_guthaben += credit
retValue = (self.feb_guthaben, self.feb_schulden)
elif month == 3:
self.maer_guthaben += credit
retValue = (self.maer_guthaben, self.maer_schulden)
elif month == 4:
self.apr_guthaben += credit
retValue = (self.apr_guthaben, self.apr_schulden)
elif month == 5:
self.mai_guthaben += credit
retValue = (self.mai_guthaben, self.mai_schulden)
elif month == 6:
self.jun_guthaben += credit
retValue = (self.jun_guthaben, self.jun_schulden)
elif month == 7:
self.jul_guthaben += credit
retValue = (self.jul_guthaben, self.jul_schulden)
elif month == 8:
self.aug_guthaben += credit
retValue = (self.aug_guthaben, self.aug_schulden)
elif month == 9:
self.sep_guthaben += credit
retValue = (self.sep_guthaben, self.sep_schulden)
elif month == 10:
self.okt_guthaben += credit
retValue = (self.okt_guthaben, self.okt_schulden)
elif month == 11:
self.nov_guthaben += credit
retValue = (self.nov_guthaben, self.nov_schulden)
elif month == 12:
self.dez_guthaben += credit
retValue = (self.dez_guthaben, self.dez_schulden)
db.session.commit()
return retValue return retValue
def toJSON(self): def toJSON(self):

View File

@ -27,37 +27,66 @@ class User(db.Model):
geruechte = db.relationship('CreditList', backref='user', lazy=True) geruechte = db.relationship('CreditList', backref='user', lazy=True)
def getCurrentGeruecht(self): def createGeruecht(self, amount=0, year=datetime.now().year):
print('search currentGeruecht in user {}'.format(self.userID)) print('create geruecht for user {} in year {}'.format(self.userID, year))
if len(self.geruechte) == 0: credit = CreditList(user_id=self.id, last_schulden=amount, year=year)
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.add(credit)
db.session.commit() db.session.commit()
credit = CreditList.query.filter_by(year=datetime.now().year).first() credit = CreditList.query.filter_by(year=year, user_id=self.id).first()
print('reated currentgeruecht {}'.format(credit)) print('reated geruecht {}'.format(credit))
return credit return credit
def getGeruecht(self, year=datetime.now().year):
for geruecht in self.geruechte:
if geruecht.year == year:
print("find geruecht {} for user {}".format(geruecht, self.id))
return geruecht
print("no geruecht found for user {}. Will create one".format(self.id))
geruecht = self.createGeruecht(year=year)
self.updateGeruecht()
return geruecht
def addAmount(self, amount, year=datetime.now().year, month=datetime.now().month):
geruecht = self.getGeruecht(year=year)
retVal = geruecht.addAmount(amount, month=month)
db.session.add(geruecht)
db.session.commit()
self.updateGeruecht()
return retVal
def addCredit(self, credit, year=datetime.now().year, month=datetime.now().month):
geruecht = self.getGeruecht(year=year)
retVal = geruecht.addCredit(credit, month=month)
db.session.add(geruecht)
db.session.commit()
self.updateGeruecht()
return retVal
def updateGeruecht(self):
""" Update list of geruechte
"""
self.geruechte.sort(key=self.sortYear)
for index, geruecht in enumerate(self.geruechte):
if index == 0 or index == len(self.geruechte) - 1:
geruecht.last_schulden = 0
if index != 0:
geruecht.last_schulden = (self.geruechte[index - 1].getSchulden() * -1)
db.session.commit()
def sortYear(self, geruecht):
return geruecht.year
def toJSON(self): def toJSON(self):
""" Create Dic to dump in JSON """ Create Dic to dump in JSON

Binary file not shown.