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()
for user in users:
geruecht = None
geruecht = user.getCurrentGeruecht()
geruecht = user.getGeruecht()
if geruecht is not None:
month = geruecht.getMonth(datetime.now().month)
amount = abs(month[0] - month[1])
@ -43,12 +43,9 @@ def _baradd():
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])
month = user.addAmount(amount)
db.session.add(geruecht)
db.session.commit()
amount = abs(month[0] - month[1])
return jsonify({"userId": user.userID, "amount": amount})
return jsonify({"error", "permission denied"}), 401

View File

@ -1,4 +1,5 @@
from flask import Blueprint, request, jsonify
from datetime import datetime
from geruecht import MONEY
from geruecht.routes import verifyAccessToken
from geruecht.model.user import User
@ -45,4 +46,62 @@ def _getFinanzerYear():
for geruecht in user.geruechte:
dic[user.userID][geruecht.year] = geruecht.toJSON()
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
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
def getMonth(self, month):
@ -106,8 +106,7 @@ class CreditList(db.Model):
return retValue
def addAmount(self, amount):
month = datetime.now().month
def addAmount(self, amount, month=datetime.now().month):
if month == 1:
self.jan_schulden += amount
@ -146,6 +145,51 @@ class CreditList(db.Model):
self.dez_schulden += amount
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
def toJSON(self):

View File

@ -27,37 +27,66 @@ class User(db.Model):
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)
def createGeruecht(self, amount=0, year=datetime.now().year):
print('create geruecht for user {} in year {}'.format(self.userID, year))
credit = CreditList(user_id=self.id, last_schulden=amount, year=year)
db.session.add(credit)
db.session.commit()
credit = CreditList.query.filter_by(year=datetime.now().year).first()
print('reated currentgeruecht {}'.format(credit))
credit = CreditList.query.filter_by(year=year, user_id=self.id).first()
print('reated geruecht {}'.format(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):
""" Create Dic to dump in JSON

Binary file not shown.