flaschengeist/geruecht/model/accessToken.py

80 lines
2.5 KiB
Python
Raw Normal View History

2019-04-11 21:56:55 +00:00
from datetime import datetime
from geruecht.logger import getDebugLogger
debug = getDebugLogger()
2019-04-11 21:56:55 +00:00
class AccessToken():
2019-04-17 12:46:46 +00:00
""" Model for an AccessToken
2019-05-02 16:50:59 +00:00
2019-04-17 12:46:46 +00:00
Attributes:
timestamp: Is a Datetime from current Time.
user: Is an User.
token: String to verify access later.
"""
2019-04-11 21:56:55 +00:00
timestamp = None
user = None
token = None
def __init__(self, id, user, token, lifetime, lock_bar=False, timestamp=datetime.now(), browser=None, platform=None):
2019-04-17 12:46:46 +00:00
""" Initialize Class AccessToken
2019-05-02 16:50:59 +00:00
2019-04-17 12:46:46 +00:00
No more to say.
Args:
User: Is an User to set.
token: Is a String to verify later
timestamp: Default current time, but can set to an other datetime-Object.
"""
debug.debug("init accesstoken")
self.id = id
2019-04-11 21:56:55 +00:00
self.user = user
self.timestamp = timestamp
2020-03-07 13:56:44 +00:00
self.lifetime = lifetime
2019-04-11 21:56:55 +00:00
self.token = token
self.lock_bar = lock_bar
self.browser = browser
self.platform = platform
debug.debug("accesstoken is {{ {} }}".format(self))
2019-04-11 21:56:55 +00:00
def updateTimestamp(self):
2019-04-17 12:46:46 +00:00
""" Update the Timestamp
Update the Timestamp to the current Time.
"""
debug.debug("update timestamp from accesstoken {{ {} }}".format(self))
2019-04-11 21:56:55 +00:00
self.timestamp = datetime.now()
def toJSON(self):
""" Create Dic to dump in JSON
Returns:
A Dic with static Attributes.
"""
dic = {
"id": self.id,
"timestamp": {'year': self.timestamp.year,
'month': self.timestamp.month,
'day': self.timestamp.day,
'hour': self.timestamp.hour,
'minute': self.timestamp.minute,
'second': self.timestamp.second
},
"lifetime": self.lifetime,
"browser": self.browser,
"platform": self.platform
}
return dic
2019-04-11 21:56:55 +00:00
def __eq__(self, token):
return True if self.token == token else False
def __sub__(self, other):
return other - self.timestamp
def __str__(self):
2020-03-09 18:54:51 +00:00
return "AccessToken(user={}, token={}, timestamp={}, lifetime={}".format(self.user, self.token, self.timestamp, self.lifetime)
2019-04-11 21:56:55 +00:00
def __repr__(self):
2020-03-09 18:54:51 +00:00
return "AccessToken(user={}, token={}, timestamp={}, lifetime={}".format(self.user, self.token, self.timestamp, self.lifetime)