class CMDR(object): ''' Created, Modified, Deleted, Replication. ''' created = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False) modified = db.Column(db.DateTime(), default=None, onupdate=datetime.utcnow) deleted = db.Column(db.DateTime(), default=None, onupdate=datetime.utcnow)
def modified(cls): return db.Column( db.DateTime(), nullable=False, server_default=text("(now() at time zone 'utc')"), onupdate=text("(now() at time zone 'utc')"), )
class Session(CMDR, db.Model): ''' Sessions ''' session_id = db.Column(UUID, primary_key=True) expires = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False) user_id = db.Column(UUID) tokens = db.Column(JSONB) # Related tokens (i.e. FCM, bla ba) def __init__(self): pass
class Session(CommonModel, db.Model, DeletableMixin): '''User sessions''' __tablename__ = 'sessions' session_id = db.Column(UUID, primary_key=True, default=new_uuid) expires = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False) user_id = db.Column(UUID, ForeignKey('users.user_id')) # Related tokens (i.e. pubnub_channel, FCM, etc) tokens = db.Column(JSONB) user = relationship('User') def __init__(self, user, tokens=None): self.user_id = user.user_id self.user = user self.tokens = tokens or {}
class Session(CommonModel, db.Model): '''User sessions''' __tablename__ = 'sessions' session_id = db.Column(UUID, primary_key=True, default=new_uuid) expires = db.Column( db.DateTime(), default=datetime.utcnow, nullable=False ) user_id = db.Column(UUID, ForeignKey('users.user_id')) # Related tokens (i.e. pubnub_channel, FCM, etc) tokens = db.Column(JSONB) user = relationship('User') def __init__(self, user, tokens=None): self.user_id = user.user_id self.user = user self.tokens = tokens or {} def as_public_dict(self): return { 'session_id': self.session_id, 'expires': self.expires, 'user': self.user.as_public_dict() } def __repr__(self): return '<Session %r>' % self.session_id @classmethod def delete(cls, session_id): return cls.query.filter( cls.session_id == str(session_id)).delete() @classmethod def find_by_id(cls, session_id): query = cls.query.filter(cls.session_id == str(session_id)) return query.order_by(cls.created).first()
def deleted(cls): return db.Column( db.DateTime(), server_default=text('NULL'), )