flaschengeist/flaschengeist/system/models/session.py

41 lines
1.3 KiB
Python

from datetime import datetime, timedelta, timezone
from . import ModelSerializeMixin
from .user import User
from ..database import db
from secrets import compare_digest
from flaschengeist import logger
class Session(db.Model, ModelSerializeMixin):
"""Model for a Session
Args:
expires: Is a Datetime from current Time.
user: Is an User.
token: String to verify access later.
"""
__tablename__ = "session"
expires: datetime = db.Column(db.DateTime)
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))
_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):
"""Update the Timestamp
Update the Timestamp to the current Time.
"""
logger.debug("update timestamp from session with token {{ {} }}".format(self))
self.expires = datetime.utcnow() + timedelta(seconds=self.lifetime)
self.expires.replace(tzinfo=timezone.utc)
def __eq__(self, token):
return compare_digest(self.token, token)