def do_upgrade(): """Implement your upgrades here.""" if not op.has_table('oauth2CLIENT'): op.create_table( 'oauth2CLIENT', db.Column('name', db.String(length=40), nullable=True), db.Column('description', db.Text(), nullable=True), db.Column('website', URLType(), nullable=True), db.Column('user_id', db.Integer(15, unsigned=True), nullable=True), db.Column('client_id', db.String(length=255), nullable=False), db.Column('client_secret', db.String(length=255), nullable=False), db.Column('is_confidential', db.Boolean(), nullable=True), db.Column('is_internal', db.Boolean(), nullable=True), db.Column('_redirect_uris', db.Text(), nullable=True), db.Column('_default_scopes', db.Text(), nullable=True), db.ForeignKeyConstraint(['user_id'], ['user.id'], ), db.PrimaryKeyConstraint('client_id'), mysql_charset='utf8', mysql_engine='MyISAM' ) else: warnings.warn("*** Creation of table 'oauth2CLIENT' skipped!") if not op.has_table('oauth2TOKEN'): op.create_table( 'oauth2TOKEN', db.Column('id', db.Integer(15, unsigned=True), autoincrement=True, nullable=False), db.Column('client_id', db.String(length=40), nullable=False), db.Column('user_id', db.Integer(15, unsigned=True), nullable=True), db.Column('token_type', db.String(length=255), nullable=True), db.Column('access_token', db.String(length=255), nullable=True), db.Column('refresh_token', db.String(length=255), nullable=True), db.Column('expires', db.DateTime(), nullable=True), db.Column('_scopes', db.Text(), nullable=True), db.Column('is_personal', db.Boolean(), nullable=True), db.Column('is_internal', db.Boolean(), nullable=True), db.ForeignKeyConstraint( ['client_id'], ['oauth2CLIENT.client_id'],), db.ForeignKeyConstraint(['user_id'], ['user.id'], ), db.PrimaryKeyConstraint('id'), db.UniqueConstraint('access_token'), db.UniqueConstraint('refresh_token'), mysql_charset='utf8', mysql_engine='MyISAM' ) else: warnings.warn("*** Creation of table 'oauth2TOKEN' skipped!")
def do_upgrade(): """ Implement your upgrades here """ if not op.has_table('remoteACCOUNT'): op.create_table( 'remoteACCOUNT', db.Column('id', db.Integer(display_width=15), nullable=False), db.Column('user_id', db.Integer(display_width=15), nullable=False), db.Column('client_id', db.String(length=255), nullable=False), db.Column('extra_data', db.JSON, nullable=True), db.ForeignKeyConstraint(['user_id'], ['user.id'], ), db.PrimaryKeyConstraint('id'), db.UniqueConstraint('user_id', 'client_id'), mysql_charset='utf8', mysql_engine='MyISAM' ) else: warnings.warn("*** Creation of table 'remoteACCOUNT table skipped!'") if not op.has_table('remoteTOKEN'): op.create_table( 'remoteTOKEN', db.Column('id_remote_account', db.Integer(display_width=15), nullable=False), db.Column('token_type', db.String(length=40), nullable=False), db.Column('access_token', db.Text(), nullable=False), db.Column('secret', db.Text(), nullable=False), db.ForeignKeyConstraint(['id_remote_account'], ['remoteACCOUNT.id'], ), db.PrimaryKeyConstraint('id_remote_account', 'token_type'), mysql_charset='utf8', mysql_engine='MyISAM' ) else: warnings.warn("*** Creation of table 'remoteTOKEN' skipped!'")
def do_upgrade(): """Implement your upgrades here.""" op.create_table('quotaUSAGE', db.Column('id', db.Integer(display_width=15), nullable=False), db.Column('object_type', db.String(length=40), nullable=True), db.Column('object_id', db.String(length=250), nullable=True), db.Column('metric', db.String(length=40), nullable=True), db.Column('value', db.BigInteger(), nullable=False), db.Column('modified', db.DateTime(), nullable=False), db.PrimaryKeyConstraint('id'), db.UniqueConstraint('object_type', 'object_id', 'metric'), mysql_charset='utf8', mysql_engine='MyISAM' ) op.create_index(op.f('ix_quotaUSAGE_object_id'), 'quotaUSAGE', ['object_id'], unique=False) op.create_index(op.f('ix_quotaUSAGE_object_type'), 'quotaUSAGE', ['object_type'], unique=False)
def do_upgrade(): """ Implement your upgrades here """ op.create_table('pages', db.Column('id', mysql.INTEGER(display_width=15), nullable=False), db.Column('url', db.String(length=100), nullable=False), db.Column('title', db.String(length=200), nullable=True), db.Column('content', db.TEXT(length=4294967294), nullable=True), db.Column('template_name', db.String(length=70), nullable=True), db.Column('created', db.DateTime(), nullable=False), db.Column('last_modified', db.DateTime(), nullable=False), db.PrimaryKeyConstraint('id'), db.UniqueConstraint('url'), mysql_charset='utf8', mysql_engine='MyISAM')
class GroupAdmin(db.Model): """Represent an administrator of a group.""" __tablename__ = 'groupADMIN' __table_args__ = (db.UniqueConstraint('group_id', 'admin_type', 'admin_id'), db.Model.__table_args__) id = db.Column(db.Integer(15, unsigned=True), nullable=False, primary_key=True, autoincrement=True) """GroupAdmin identifier.""" group_id = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(Group.id), nullable=False, primary_key=True) """Group for membership.""" admin_type = db.Column(db.Unicode(255)) """Generic relationship to an object.""" admin_id = db.Column(db.Integer) """Generic relationship to an object.""" # # Relations # group = db.relationship(Group, backref=db.backref('admins', cascade="all, delete-orphan")) """Group relationship.""" admin = generic_relationship(admin_type, admin_id) """Generic relationship to administrator of group.""" @classmethod def create(cls, group, admin): """Create a new group admin. :param group: Group object. :param admin: Admin object. :returns: Newly created GroupAdmin object. :raises: IntegrityError """ try: obj = cls( group=group, admin=admin, ) db.session.add(obj) db.session.commit() return obj except IntegrityError: db.session.rollback() raise @classmethod def get(cls, group, admin): """Get specific GroupAdmin object.""" try: ga = cls.query.filter_by( group=group, admin_id=admin.get_id(), admin_type=resolve_admin_type(admin)).one() return ga except Exception: return None @classmethod def delete(cls, group, admin): """Delete admin from group. :param group: Group object. :param admin: Admin object. """ try: obj = cls.query.filter(cls.admin == admin, cls.group == group).one() db.session.delete(obj) db.session.commit() except Exception: db.session.rollback() raise @classmethod def query_by_group(cls, group): """Get all admins for a specific group.""" return cls.query.filter_by(group=group) @classmethod def query_by_admin(cls, admin): """Get all groups for for a specific admin.""" return cls.query.filter_by(admin_type=resolve_admin_type(admin), admin_id=admin.get_id()) @classmethod def query_admins_by_group_ids(cls, groups_ids=None): """Get count of admins per group.""" assert groups_ids is None or isinstance(groups_ids, list) query = db.session.query(Group.id, func.count( GroupAdmin.id)).join(GroupAdmin).group_by(Group.id) if groups_ids: query = query.filter(Group.id.in_(groups_ids)) return query
class ResourceUsage(db.Model): """Usage of a specific. Note: Model is not suitable to store metrics with high granularity. """ __tablename__ = 'quotaUSAGE' __table_args__ = ( # Requires object_type, object_id and metric combined to be less than # ~333 chars due to MyISAM constraint. db.UniqueConstraint('object_type', 'object_id', 'metric'), db.Model.__table_args__) id = db.Column(db.Integer(15, unsigned=True), nullable=False, primary_key=True, autoincrement=True) object_type = db.Column(db.String(40), index=True) """Generic relationship to an object type.""" object_id = db.Column(db.String(250), index=True) """Generic relationship to an object.""" metric = db.Column(db.String(40)) """Metric.""" value = db.Column(db.BigInteger(), nullable=False, default=0) modified = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now) """Modification timestamp.""" @classmethod def create(cls, object_type, object_id, metric, value): """Update or create a new value of a metric.""" m = cls(object_type=object_type, object_id=object_id, metric=metric, value=value) db.session.add(m) db.session.commit() return m @classmethod def update_or_create(cls, object_type, object_id, metric, value): """Update or create a new value of a metric.""" m = cls.get(object_type, object_id, metric) if m is None: m = cls.create(object_type, object_id, metric, value) resource_usage_updated.send(metric, object_type=object_type, object_id=object_id, value=value, old_value=None) else: old_value = m.value m.value = value db.session.commit() resource_usage_updated.send(metric, object_type=object_type, object_id=object_id, value=value, old_value=old_value) return m @classmethod def get(cls, object_type, object_id, metric): """Get specific metric.""" try: return cls.query.filter_by(object_type=object_type, object_id=object_id, metric=metric).one() except NoResultFound: return None
class RemoteAccount(db.Model): """Storage for remote linked accounts.""" __tablename__ = 'remoteACCOUNT' __table_args__ = (db.UniqueConstraint('user_id', 'client_id'), db.Model.__table_args__) # # Fields # id = db.Column(db.Integer(15, unsigned=True), primary_key=True, autoincrement=True) """Primary key.""" user_id = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), nullable=False) """Local user linked with a remote app via the access token.""" client_id = db.Column(db.String(255), nullable=False) """Client ID of remote application (defined in OAUTHCLIENT_REMOTE_APPS).""" extra_data = db.Column(MutableDict.as_mutable(db.JSON), nullable=False) """Extra data associated with this linked account.""" # # Relationships propoerties # user = db.relationship('User') """SQLAlchemy relationship to user.""" tokens = db.relationship( "RemoteToken", backref="remote_account", ) """SQLAlchemy relationship to RemoteToken objects.""" @classmethod def get(cls, user_id, client_id): """Get RemoteAccount object for user. :param user_id: User id :param client_id: Client id. """ return cls.query.filter_by( user_id=user_id, client_id=client_id, ).first() @classmethod def create(cls, user_id, client_id, extra_data): """Create new remote account for user. :param user_id: User id. :param client_id: Client id. :param extra_data: JSON-serializable dictionary of any extra data that needs to be save together with this link. """ account = cls(user_id=user_id, client_id=client_id, extra_data=extra_data or dict()) db.session.add(account) db.session.commit() return account def delete(self): """Delete remote account together with all stored tokens.""" RemoteToken.query.filter_by(id_remote_account=self.id).delete() db.session.delete(self) db.session.commit()