flaschengeist/flaschengeist/system/models/accessToken.py

67 lines
2.2 KiB
Python
Raw Normal View History

2019-04-11 21:56:55 +00:00
from datetime import datetime
from ..database import db
from flask import current_app
from werkzeug.local import LocalProxy
from secrets import compare_digest
logger = LocalProxy(lambda: current_app.logger)
class AccessToken(db.Model):
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.
"""
__tablename__ = 'session'
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship("User", back_populates="sessions")
token = db.Column(db.String(30))
lifetime = db.Column(db.Integer)
browser = db.Column(db.String(30))
platform = db.Column(db.String(30))
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.
"""
logger.debug("update timestamp from accesstoken {{ {} }}".format(self))
self.timestamp = datetime.utcnow()
2019-04-11 21:56:55 +00:00
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 compare_digest(self.token, token)
2019-04-11 21:56:55 +00:00
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)