class Collection(BaseModel, db.Model):
    """集合"""

    __tablename__ = "ds_collections"
    __table_args__ = {'extend_existing': True}

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 编号
    user_id = db.Column(db.Integer,
                        db.ForeignKey('ds_users.id'),
                        nullable=False)
    name = db.Column(db.String(32), nullable=False)  # 集合名字
    type = db.Column(db.Integer, default=1)  # 类型 (分类/人脸 | 检测/切割)
    desc = db.Column(db.String(128))  # 数据集描述
    images = db.relationship("Image", backref="collection")  # 集合/图片一对多
    versions = db.relationship("Version", backref="collection")  # 集合/版本一对多
    labels = db.relationship("Label", backref="collection")  # 集合/标签一对多

    parameter_1 = db.Column(db.String(32))
    parameter_2 = db.Column(db.Integer)

    def to_dict(self):
        """将对象转换为字典数据"""
        collection_dict = {
            "id": self.id,
            "name": self.name,
            "type": self.type,
            "desc": self.desc,
        }

        return collection_dict
Esempio n. 2
0
class Version(BaseModel, db.Model):
    """版本"""

    __tablename__ = "ds_versions"
    __table_args__ = {'extend_existing': True}

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 版本编号
    collection_id = db.Column(db.Integer,
                              db.ForeignKey('ds_collections.id'),
                              nullable=False)
    name = db.Column(db.String(32), nullable=False)  # 版本名称
    description = db.Column(db.String(128))  # 版本描述
    zip_path = db.Column(db.String(64))  # 版本地址
    average_number = db.Column(db.Integer, default=0)  # 平均框值
    mean_size = db.Column(db.String(16), default='0*0')  # 平均大小
    label_info = db.Column(db.String(512))  # 标签使用详情
    images = db.relationship("Image", backref="version")  # 版本/图片一对多

    def to_dict(self):
        """将对象转换为字典数据"""

        version_dict = {
            "version_id": self.id,
            "version_name": self.name,
            "version_description": self.description,
            "version_average_number": self.average_number,
            "version_mean_size": self.mean_size
        }
        return version_dict
Esempio n. 3
0
class Role(db.Model):
    __tablename__ = 'roles'
    __table_args__ = {'extend_existing': True}
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    default = db.Column(db.Boolean, default=False,
                        index=True)  # 用户默认角色,default是False
    permissions = db.Column(db.Integer)  # 用户权限设置,是一个数值
    # lazy='dynamic'表示表Role对象点users属性,返回的是对象,仍可进行过滤等相关操作,默认lazy='select'
    users = db.relationship('User', backref='role',
                            lazy='dynamic')  # lazy='select'时查询的时对应User表的结果

    def __init__(self, **kwargs):
        super(Role, self).__init__(**kwargs)
        if self.permissions is None:
            self.permissions = 0

    def __repr__(self):
        return '<Role %r>' % self.name

    @staticmethod
    def insert_roles():
        roles = {
            'User': (Permission.UPDATE, ),  # 权限为1
            'Moderator': (Permission.UPDATE, Permission.CRUD),  # 权限 1,2
            'Administrator': (Permission.UPDATE, Permission.CRUD,
                              Permission.ADMINISTER)  # 权限 1,2,4
        }

        default_role = 'User'
        for r in roles:  # 通过角色查找现有角色去设置权限,方便修改权限和添加新角色
            role = Role.query.filter_by(name=r).first()
            if role is None:  # 当数据库中没有某个角色时创建个新角色,并添加user权限
                role = Role(name=r)
            role.reset_permissions()
            for perm in roles[r]:  # 遍历 User对应的权限元组
                role.add_permission(perm)
            role.default = (role.name == default_role)  # 判断是不是默认用户
            db.session.add(role)
        db.session.commit()

    def has_permission(self, perm):  # 判断当前角色是否已经有该权限
        return self.permissions & perm == perm

    def add_permission(self, perm):  # 角色添加权限
        if not self.has_permission(perm):
            self.permissions += perm

    def remove_permiss(self, perm):  # 角色删除权限
        if self.has_permission(perm):
            self.permissions -= perm

    def reset_permissions(self):  # 重置角色
        self.permissions = 1
Esempio n. 4
0
class Share(db.Model):
    __tablename__ = "ds_shares"
    __table_args__ = {'extend_existing': True}

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    my_share = db.Column(db.String(128))  # 我的分享 "{1:[4,5,6], 2:[7,8]}"
    share_collection = db.Column(db.String(128))  # 被分享的数据集 "[1,2,3,4]"
    users = db.relationship('User', backref='share',
                            uselist=False)  # 参数uselist=False 表示一对一

    def to_dict(self):
        """将对象转换为字典数据"""

        share_dict = {"share_collection": self.share_collection}
        return share_dict
Esempio n. 5
0
class User(db.Model, BaseModel):
    __tablename__ = 'ds_users'
    __table_args__ = {'extend_existing': True}

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 用户编号
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))  # ???
    shares_id = db.Column(db.Integer,
                          db.ForeignKey('ds_shares.id'))  # 用户/分享集合一对一
    name = db.Column(db.String(32), unique=True, nullable=False)  # 用户昵称
    email = db.Column(db.String(128), index=True, unique=True)  # 邮箱
    password_hash = db.Column(db.String(128), nullable=False)  # 加密的密码
    login_time = db.Column(db.Integer)  # 登录时间验证
    collections = db.relationship("Collection", backref="user")  # 用户/集合一对多

    def __repr__(self):
        return '<User %r>' % self.name  # 实例化User类后,直接打印实例返回name属性,默认返回一个对象,现在是重写

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        if self.role is None:
            if self.name == current_app.config['ADMIN']:
                self.role = Role.query.filter_by(name='Administrator').first()
            elif self.email == current_app.config['ADMIN_EMAIL']:
                self.role = Role.query.filter_by(name='Administrator').first()
            else:
                self.role = Role.query.filter_by(default=True).first()

        if self.share is None:
            self.share = Share()

    @property
    def password(self):
        """获取password属性时被调用"""
        raise AttributeError("不可读")

    @password.setter
    def password(self, passwd):
        """设置password属性时被调用,设置密码加密"""
        self.password_hash = generate_password_hash(passwd)

    def check_password(self, passwd):
        """检查密码的正确性"""
        return check_password_hash(self.password_hash, passwd)

    # 查看用户是否有权限
    def can(self, permission):
        return self.role is not None and \
               (self.role.permissions & permission) == permission

    # 验证用户是否管理员
    def is_administrator(self):
        return self.can(Permission.ADMINISTER)

    @staticmethod
    def get(id):
        return User.query.filter_by(id=id).first()

    def add(self, user):
        db.session.add(user)
        return session_commit()

    def update(self):
        return session_commit()

    def delete(self, id):
        self.query.filter_by(id=id).delete()
        return session_commit()

    def to_dict(self):
        """将对象转换为字典数据"""
        user_dict = {
            "id": self.id,
            "name": self.name,
        }
        return user_dict