class Application(StandardAttributes, db.Model): name = db.Column(db.String(200), nullable=False) client_id = db.Column(db.String(64), default=lambda: Application.generate_token(10), nullable=False, unique=True) client_secret = db.Column(db.String(64), default=lambda: Application.generate_token(), nullable=False, unique=True) jobs = db.relationship('Job', backref='jobs', lazy=True) user_id = db.Column(GUID, db.ForeignKey('user.id'), primary_key=True, unique=True) user = db.relationship('User', innerjoin=True, uselist=False) __tablename__ = 'application' __repr__ = model_repr('name', 'client_id') @classmethod def generate_token(cls, size=32): return token_hex(size)
class Option(db.Model): id = db.Column(GUID, primary_key=True, default=GUID.default_value) option_id = db.Column(GUID, nullable=False) name = db.Column(db.String(64), nullable=False) value = db.Column(db.Text, nullable=False) __tablename__ = 'option' __table_args__ = (db.UniqueConstraint('option_id', 'name', name='unq_option_name'), )
class Job(StandardAttributes, ApplicationBoundMixin, db.Model): name = db.Column(db.String(200), nullable=False) url = db.Column(db.String(200), nullable=False) params = db.Column(JSON) schedule = db.Column(Schedule) enabled = db.Column(db.Boolean, default=False) __tablename__ = 'job' def get_redis_key(self): return 'redbeat:{}'.format(str(self.id))
class StandardAttributes(object): created_at = db.Column(db.DateTime(True), default=db.func.now(), nullable=False) @declared_attr def id(cls): return db.Column(GUID, primary_key=True, default=GUID.default_value)
class Identity(StandardAttributes, db.Model): user_id = db.Column(GUID, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False, index=True) external_id = db.Column(db.String(64), unique=True, nullable=False) provider = db.Column(db.String(32), nullable=True) config = db.Column(JSON, nullable=False) scopes = db.Column(ARRAY(db.String(64)), nullable=True) user = db.relationship('User') __tablename__ = 'identity' __table_args__ = (db.UniqueConstraint('user_id', 'provider', name='unq_identity_user'), )
class User(StandardAttributes, db.Model): email = db.Column(db.String(128), unique=True, nullable=False) options = db.relationship( 'Option', foreign_keys='[Option.option_id]', primaryjoin='Option.option_id == User.id', viewonly=True, uselist=True ) __tablename__ = 'user'
class ApplicationApiToken(StandardAttributes, db.Model, ApiTokenMixin): application_id = db.Column(GUID, db.ForeignKey('application.id', ondelete='CASCADE'), nullable=False, unique=True) application = db.relationship('Application', backref=db.backref('token', uselist=False), innerjoin=True) __tablename__ = 'application_api_token' __repr__ = model_repr('application_id', 'key') def get_token_key(self): return 'a' def get_tenant(self): self.user
def key(cls): return db.Column( db.String(64), default=lambda: ApiTokenMixin.generate_token(), unique=True, nullable=False )
def application_id(cls): return db.Column( GUID, db.ForeignKey('application.id', ondelete='CASCADE'), nullable=False, index=True )
def id(cls): return db.Column(GUID, primary_key=True, default=GUID.default_value)