flaschengeist/geruecht/model/user.py

198 lines
6.2 KiB
Python

from geruecht import getLogger
from geruecht import db
#from geruecht.model.creditList import CreditList
from datetime import datetime
LOGGER = getLogger(__name__)
class User():
""" 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.
"""
def __init__(self, data):
self.id = int(data['id'])
self.cn = data['cn']
self.dn = data['dn']
self.firstname = data['firstname']
self.lastname = data['lastname']
self.group = data['group']
#geruechte = db.relationship('CreditList', backref='user', lazy=True)
'''
def createGeruecht(self, amount=0, year=datetime.now().year):
""" 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
"""
LOGGER.debug("Create Geruecht for user {} in year {}".format(self, 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=year, user_id=self.id).first()
LOGGER.debug("Created Geruecht {}".format(credit))
return credit
def getGeruecht(self, year=datetime.now().year):
""" 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
"""
LOGGER.debug("Iterate through Geruechte of User {}".format(self))
for geruecht in self.geruechte:
LOGGER.debug("Check if Geruecht {} has year {}".format(geruecht, year))
if geruecht.year == year:
LOGGER.debug("Find Geruecht {} for User {}".format(geruecht, self))
return geruecht
LOGGER.debug("No Geruecht found for User {}. Will create one".format(self))
geruecht = self.createGeruecht(year=year)
self.updateGeruecht()
return geruecht
def addAmount(self, amount, year=datetime.now().year, month=datetime.now().month):
""" 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)
"""
LOGGER.debug("Add amount to User {} in year {} and month {}".format(self, year, 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):
""" 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)
"""
LOGGER.debug("Add credit to User {} in year {} and month {}".format(self, year, 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
This function iterate through the geruechte, which sorted by year and update the last_schulden of the geruecht.
"""
LOGGER.debug("Update all 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):
""" 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
"""
return geruecht.year
'''
def toJSON(self):
""" Create Dic to dump in JSON
Returns:
A Dic with static Attributes.
"""
dic = {
"cn": self.cn,
"dn": self.dn,
"firstname": self.firstname,
"lastname": self.lastname,
"group": self.group,
}
return dic
def update(self):
data = ldap.getUserData(self.cn)
data['group'] = ldap.getGroup(self.cn)
db.updateUser(data)
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.
"""
LOGGER.debug("Login User {}".format(self))
try:
from geruecht import ldapController as ldap
ldap.login(self.cn, password)
self.update()
return True
except:
return False
def __repr__(self):
return "User({}, {}, {})".format(self.cn, self.dn, self.group)