85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
from geruecht import db
|
|
from geruecht import bcrypt
|
|
from geruecht.model.creditList import CreditList
|
|
from datetime import datetime
|
|
|
|
class User(db.Model):
|
|
""" Database Object for User
|
|
|
|
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.
|
|
"""
|
|
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)
|
|
|
|
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):
|
|
""" Create Dic to dump in JSON
|
|
|
|
Returns:
|
|
A Dic with static Attributes.
|
|
"""
|
|
dic = {
|
|
"userId": self.userID,
|
|
"username": self.username,
|
|
"firstname": self.firstname,
|
|
"lastname": self.lastname,
|
|
"group": self.group,
|
|
}
|
|
return dic
|
|
|
|
def login(self, password):
|
|
""" Login for the User
|
|
|
|
Only check the given Password:
|
|
|
|
Returns:
|
|
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
|