flaschengeist/flaschengeist/models/session.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.3 KiB
Python
Raw Normal View History

2020-10-15 00:19:51 +00:00
from datetime import datetime, timedelta, timezone
2020-10-20 15:53:29 +00:00
from . import ModelSerializeMixin, UtcDateTime
from .user import User
from flaschengeist.database import db
from secrets import compare_digest
from flaschengeist import logger
class Session(db.Model, ModelSerializeMixin):
"""Model for a Session
2019-05-02 16:50:59 +00:00
Args:
expires: Is a Datetime from current Time.
2019-04-17 12:46:46 +00:00
user: Is an User.
token: String to verify access later.
"""
__tablename__ = "session"
2020-10-20 15:53:29 +00:00
expires: datetime = db.Column(UtcDateTime)
token: str = db.Column(db.String(32), unique=True)
lifetime: int = db.Column(db.Integer)
browser: str = db.Column(db.String(30))
platform: str = db.Column(db.String(30))
2019-04-11 21:56:55 +00:00
_id = db.Column("id", db.Integer, primary_key=True)
_user: User = db.relationship("User", back_populates="_sessions")
_user_id = db.Column("user_id", db.Integer, db.ForeignKey("user.id"))
def refresh(self):
2019-04-17 12:46:46 +00:00
"""Update the Timestamp
Update the Timestamp to the current Time.
"""
logger.debug("update timestamp from session with token {{ {} }}".format(self))
2020-10-20 15:53:29 +00:00
self.expires = datetime.now(timezone.utc) + timedelta(seconds=self.lifetime)
2019-04-11 21:56:55 +00:00
def __eq__(self, token):
return compare_digest(self.token, token)