2020-03-10 08:19:11 +00:00
|
|
|
from geruecht.logger import getDebugLogger
|
2019-12-19 17:26:41 +00:00
|
|
|
from geruecht.model.creditList import CreditList, create_empty_data
|
2019-04-23 22:08:25 +00:00
|
|
|
from datetime import datetime
|
2019-04-11 21:56:55 +00:00
|
|
|
|
2020-03-10 18:23:52 +00:00
|
|
|
debug = getDebugLogger()
|
2019-05-02 23:40:13 +00:00
|
|
|
|
2019-12-19 17:26:41 +00:00
|
|
|
|
2019-12-19 07:12:29 +00:00
|
|
|
class User():
|
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-12-19 07:12:29 +00:00
|
|
|
def __init__(self, data):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("init user")
|
2019-12-29 16:55:21 +00:00
|
|
|
if 'id' in data:
|
|
|
|
self.id = int(data['id'])
|
2019-12-28 20:52:49 +00:00
|
|
|
self.uid = data['uid']
|
2019-12-19 07:12:29 +00:00
|
|
|
self.dn = data['dn']
|
|
|
|
self.firstname = data['firstname']
|
|
|
|
self.lastname = data['lastname']
|
2019-12-19 17:26:41 +00:00
|
|
|
self.group = data['gruppe']
|
2020-06-07 21:28:27 +00:00
|
|
|
self.last_seen = None
|
|
|
|
if 'last_seen' in data:
|
|
|
|
self.last_seen = data['last_seen']
|
2020-03-03 21:33:47 +00:00
|
|
|
if 'statusgroup' in data:
|
|
|
|
self.statusgroup = data['statusgroup']
|
|
|
|
else:
|
|
|
|
self.statusgroup = None
|
|
|
|
if 'voting' in data:
|
|
|
|
self.voting = data['voting']
|
|
|
|
else:
|
|
|
|
self.voting = None
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'mail' in data:
|
|
|
|
self.mail = data['mail']
|
|
|
|
else:
|
|
|
|
self.mail = ''
|
2019-12-29 16:55:21 +00:00
|
|
|
if 'lockLimit' in data:
|
|
|
|
self.limit = int(data['lockLimit'])
|
2019-12-28 22:34:09 +00:00
|
|
|
else:
|
|
|
|
self.limit = 4200
|
|
|
|
if 'locked' in data:
|
|
|
|
self.locked = bool(data['locked'])
|
|
|
|
else:
|
|
|
|
self.locked = False
|
|
|
|
if 'autoLock' in data:
|
|
|
|
self.autoLock = bool(data['autoLock'])
|
|
|
|
else:
|
|
|
|
self.autoLock = True
|
2019-12-22 21:27:39 +00:00
|
|
|
if type(data['gruppe']) == list:
|
|
|
|
self.group = data['gruppe']
|
|
|
|
elif type(data['gruppe']) == str:
|
|
|
|
self.group = data['gruppe'].split(',')
|
2019-12-28 20:52:49 +00:00
|
|
|
if 'creditLists' in data:
|
|
|
|
self.geruechte = data['creditLists']
|
2020-05-17 11:35:33 +00:00
|
|
|
if 'workgroups' in data:
|
|
|
|
self.workgroups = data['workgroups']
|
|
|
|
else:
|
|
|
|
self.workgroups = None
|
2020-01-26 22:31:22 +00:00
|
|
|
self.password = ''
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("user is {{ {} }}".format(self))
|
2019-12-22 21:27:39 +00:00
|
|
|
|
2019-12-28 22:34:09 +00:00
|
|
|
def updateData(self, data):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("update data of user")
|
2019-12-28 22:34:09 +00:00
|
|
|
if 'dn' in data:
|
|
|
|
self.dn = data['dn']
|
|
|
|
if 'firstname' in data:
|
|
|
|
self.firstname = data['firstname']
|
|
|
|
if 'lastname' in data:
|
|
|
|
self.lastname = data['lastname']
|
|
|
|
if 'gruppe' in data:
|
|
|
|
self.group = data['gruppe']
|
2019-12-29 16:55:21 +00:00
|
|
|
if 'lockLimit' in data:
|
|
|
|
self.limit = int(data['lockLimit'])
|
2019-12-28 22:34:09 +00:00
|
|
|
if 'locked' in data:
|
|
|
|
self.locked = bool(data['locked'])
|
|
|
|
if 'autoLock' in data:
|
|
|
|
self.autoLock = bool(data['autoLock'])
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'mail' in data:
|
|
|
|
self.mail = data['mail']
|
2020-03-03 21:33:47 +00:00
|
|
|
if 'statusgorup' in data:
|
|
|
|
self.statusgroup = data['statusgroup']
|
|
|
|
if 'voting' in data:
|
|
|
|
self.voting = data['voting']
|
2020-05-17 11:35:33 +00:00
|
|
|
if 'workgroups' in data:
|
|
|
|
self.workgroups = data['workgroups']
|
|
|
|
else:
|
|
|
|
self.workgroups = None
|
2019-12-28 22:34:09 +00:00
|
|
|
|
2019-12-28 20:52:49 +00:00
|
|
|
def initGeruechte(self, creditLists):
|
|
|
|
if type(creditLists) == list:
|
|
|
|
self.geruechte = creditLists
|
2019-12-19 17:26:41 +00:00
|
|
|
|
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
|
|
|
|
"""
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("create creditlist for user {{ {} }} in year {{ {} }}".format(self, year))
|
2019-12-19 17:26:41 +00:00
|
|
|
data = create_empty_data()
|
2019-12-22 21:27:39 +00:00
|
|
|
data['user_id'] = self.id
|
|
|
|
data['last_schulden'] = amount
|
2019-12-19 17:26:41 +00:00
|
|
|
data['year_date'] = year
|
|
|
|
credit = CreditList(data)
|
|
|
|
self.geruechte.append(credit)
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("creditlist is {{ {} }}".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
|
|
|
|
"""
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("get creditlist from user on year {{ {} }}".format(year))
|
2019-05-02 00:21:50 +00:00
|
|
|
for geruecht in self.geruechte:
|
|
|
|
if geruecht.year == year:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("creditlist is {{ {} }} for user {{ {} }}".format(geruecht, self))
|
2019-05-02 00:21:50 +00:00
|
|
|
return geruecht
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("no creditlist found for user {{ {} }}".format(self))
|
2019-05-02 00:21:50 +00:00
|
|
|
geruecht = self.createGeruecht(year=year)
|
|
|
|
|
2019-12-22 21:27:39 +00:00
|
|
|
return self.getGeruecht(year=year)
|
2019-05-02 00:21:50 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
"""
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("add amount to user {{ {} }} in year {{ {} }} and month {{ {} }}".format(self, year, month))
|
2019-05-02 00:21:50 +00:00
|
|
|
geruecht = self.getGeruecht(year=year)
|
|
|
|
retVal = geruecht.addAmount(amount, month=month)
|
|
|
|
|
|
|
|
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)
|
|
|
|
"""
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("add credit to user {{ {} }} in year {{ {} }} and month {{ {} }}".format(self, year, month))
|
2019-05-02 00:21:50 +00:00
|
|
|
geruecht = self.getGeruecht(year=year)
|
|
|
|
retVal = geruecht.addCredit(credit, month=month)
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("update all creditlists ")
|
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)
|
2019-12-28 20:52:49 +00:00
|
|
|
|
|
|
|
return self.geruechte
|
2019-05-02 00:21:50 +00:00
|
|
|
|
|
|
|
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-12-19 17:26:41 +00:00
|
|
|
|
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 = {
|
2020-03-03 21:33:47 +00:00
|
|
|
"id": self.id,
|
2019-12-28 20:52:49 +00:00
|
|
|
"userId": self.uid,
|
|
|
|
"uid": self.uid,
|
2019-12-19 07:12:29 +00:00
|
|
|
"dn": self.dn,
|
2019-04-11 21:56:55 +00:00
|
|
|
"firstname": self.firstname,
|
|
|
|
"lastname": self.lastname,
|
|
|
|
"group": self.group,
|
2019-12-28 22:34:09 +00:00
|
|
|
"username": self.uid,
|
|
|
|
"locked": self.locked,
|
|
|
|
"autoLock": self.autoLock,
|
2020-01-26 22:31:22 +00:00
|
|
|
"limit": self.limit,
|
2020-03-03 21:33:47 +00:00
|
|
|
"mail": self.mail,
|
|
|
|
"statusgroup": self.statusgroup,
|
2020-05-17 11:35:33 +00:00
|
|
|
"voting": self.voting,
|
|
|
|
"workgroups": self.workgroups
|
2019-04-11 21:56:55 +00:00
|
|
|
}
|
|
|
|
return dic
|
|
|
|
|
2019-05-02 23:40:13 +00:00
|
|
|
def __repr__(self):
|
2019-12-28 20:52:49 +00:00
|
|
|
return "User({}, {}, {})".format(self.uid, self.dn, self.group)
|
2019-12-19 17:26:41 +00:00
|
|
|
|