from datetime import datetime from geruecht.logger import getDebugLogger debug = getDebugLogger() class AccessToken(): """ Model for an AccessToken Attributes: timestamp: Is a Datetime from current Time. user: Is an User. token: String to verify access later. """ timestamp = None user = None token = None def __init__(self, id, user, token, lifetime, lock_bar=False, timestamp=datetime.now(), browser=None, platform=None): """ Initialize Class AccessToken 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 self.user = user self.timestamp = timestamp self.lifetime = lifetime self.token = token self.lock_bar = lock_bar self.browser = browser self.platform = platform debug.debug("accesstoken is {{ {} }}".format(self)) def updateTimestamp(self): """ Update the Timestamp Update the Timestamp to the current Time. """ debug.debug("update timestamp from accesstoken {{ {} }}".format(self)) 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 def __eq__(self, token): return True if self.token == token else False def __sub__(self, other): return other - self.timestamp def __str__(self): return "AccessToken(user={}, token={}, timestamp={}, lifetime={}".format(self.user, self.token, self.timestamp, self.lifetime) def __repr__(self): return "AccessToken(user={}, token={}, timestamp={}, lifetime={}".format(self.user, self.token, self.timestamp, self.lifetime)