2019-04-11 21:56:55 +00:00
|
|
|
from geruecht import db
|
|
|
|
from geruecht import bcrypt
|
2019-04-23 22:08:25 +00:00
|
|
|
from geruecht.model.creditList import CreditList
|
|
|
|
from datetime import datetime
|
2019-04-11 21:56:55 +00:00
|
|
|
|
|
|
|
class User(db.Model):
|
2019-04-17 12:46:46 +00:00
|
|
|
""" Database Object for User
|
2019-05-01 20:43:28 +00:00
|
|
|
|
2019-04-17 12:46:46 +00:00
|
|
|
Table for all safed User
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
id: Id in Database as Primary Key.
|
|
|
|
userID: ID for the User maybe to Link?
|
|
|
|
username: Username of the User to Login
|
|
|
|
firstname: Firstname of the User
|
|
|
|
Lastname: Lastname of the User
|
|
|
|
group: Which group is the User? moneymaster, gastro, user or bar?
|
|
|
|
password: salted hashed password for the User.
|
|
|
|
"""
|
2019-04-11 21:56:55 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
userID = db.Column(db.String, nullable=False, unique=True)
|
|
|
|
username = db.Column(db.String, nullable=False, unique=True)
|
|
|
|
firstname = db.Column(db.String, nullable=False)
|
|
|
|
lastname = db.Column(db.String, nullable=False)
|
|
|
|
group = db.Column(db.String, nullable=False)
|
|
|
|
password = db.Column(db.String, nullable=False)
|
|
|
|
|
2019-04-23 18:26:20 +00:00
|
|
|
geruechte = db.relationship('CreditList', backref='user', lazy=True)
|
|
|
|
|
2019-05-02 00:21:50 +00:00
|
|
|
def createGeruecht(self, amount=0, year=datetime.now().year):
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Create Geruecht
|
|
|
|
|
|
|
|
This function create a geruecht for the user for an year.
|
|
|
|
By default is amount zero and year the actual year.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
amount: is the last_schulden of the geruecht
|
|
|
|
year: is the year of the geruecht
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
the created geruecht
|
|
|
|
"""
|
2019-05-02 00:21:50 +00:00
|
|
|
print('create geruecht for user {} in year {}'.format(self.userID, year))
|
|
|
|
credit = CreditList(user_id=self.id, last_schulden=amount, year=year)
|
2019-04-23 22:08:25 +00:00
|
|
|
db.session.add(credit)
|
|
|
|
db.session.commit()
|
2019-05-02 00:21:50 +00:00
|
|
|
credit = CreditList.query.filter_by(year=year, user_id=self.id).first()
|
|
|
|
print('reated geruecht {}'.format(credit))
|
2019-04-23 22:08:25 +00:00
|
|
|
return credit
|
|
|
|
|
2019-05-02 00:21:50 +00:00
|
|
|
def getGeruecht(self, year=datetime.now().year):
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Get Geruecht
|
|
|
|
|
|
|
|
This function returns the geruecht of an year.
|
|
|
|
By default is the year the actual year.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
year: the year of the geruecht
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
the geruecht of the year
|
|
|
|
"""
|
2019-05-02 00:21:50 +00:00
|
|
|
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):
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Add Amount
|
|
|
|
|
|
|
|
This function add an amount to a geruecht with an spezified year and month to the user.
|
|
|
|
By default the year is the actual year.
|
|
|
|
By default the month is the actual month.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
year: year of the geruecht
|
|
|
|
month: month for the amount
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
double (credit, amount)
|
|
|
|
"""
|
2019-05-02 00:21:50 +00:00
|
|
|
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):
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Add Credit
|
|
|
|
|
|
|
|
This function add an credit to a geruecht with an spezified year and month to the user.
|
|
|
|
By default the year is the actual year.
|
|
|
|
By default the month is the actual month.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
year: year of the geruecht
|
|
|
|
month: month for the amount
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
double (credit, amount)
|
|
|
|
"""
|
2019-05-02 00:21:50 +00:00
|
|
|
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
|
|
|
|
|
2019-05-02 16:50:59 +00:00
|
|
|
This function iterate through the geruechte, which sorted by year and update the last_schulden of the geruecht.
|
2019-05-02 00:21:50 +00:00
|
|
|
"""
|
|
|
|
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):
|
2019-05-02 16:50:59 +00:00
|
|
|
""" Sort Year
|
|
|
|
|
|
|
|
This function is only an helperfunction to sort the list of geruechte by years.
|
|
|
|
It only returns the year of the geruecht.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
geruecht: geruecht which year you want
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int year of the geruecht
|
|
|
|
"""
|
2019-05-02 00:21:50 +00:00
|
|
|
return geruecht.year
|
|
|
|
|
2019-04-11 21:56:55 +00:00
|
|
|
def toJSON(self):
|
2019-04-17 12:46:46 +00:00
|
|
|
""" Create Dic to dump in JSON
|
2019-05-01 20:43:28 +00:00
|
|
|
|
2019-04-17 12:46:46 +00:00
|
|
|
Returns:
|
|
|
|
A Dic with static Attributes.
|
|
|
|
"""
|
2019-04-11 21:56:55 +00:00
|
|
|
dic = {
|
2019-04-12 12:51:37 +00:00
|
|
|
"userId": self.userID,
|
2019-04-11 21:56:55 +00:00
|
|
|
"username": self.username,
|
|
|
|
"firstname": self.firstname,
|
|
|
|
"lastname": self.lastname,
|
|
|
|
"group": self.group,
|
|
|
|
}
|
|
|
|
return dic
|
|
|
|
|
|
|
|
def login(self, password):
|
2019-04-17 12:46:46 +00:00
|
|
|
""" Login for the User
|
|
|
|
|
|
|
|
Only check the given Password:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A Bool. True if the password is correct and False if it isn't.
|
|
|
|
"""
|
2019-04-11 21:56:55 +00:00
|
|
|
return True if bcrypt.check_password_hash(self.password, password) else False
|