class Image(BaseModel, db.Model): """图片""" __tablename__ = "ds_images" __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True, autoincrement=True) # 图片编号 version_id = db.Column(db.Integer, db.ForeignKey('ds_versions.id'), nullable=True) # 版本图片 collection_id = db.Column(db.Integer, db.ForeignKey('ds_collections.id'), nullable=False) # 集合图片 name = db.Column(db.String(128), nullable=False) # 图片名称 site = db.Column(db.String(128), nullable=False) # 图片地址 label_path = db.Column(db.String(128), nullable=True) # 图片标注路径 status = db.Column(db.Integer, default=0) # 图片状态(是否标注) structured_path = db.Column(db.String(128), nullable=True) # 图片结构化路径 md5 = db.Column(db.String(32), index=True) def to_dict(self): """将对象转换为字典数据""" image_dict = { "id": self.id, "name": self.name, "site": self.site, "label_path": self.label_path, "state": self.state, "structured_path": self.structured_path } return image_dict
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
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
class Label(BaseModel, db.Model): """标签""" __tablename__ = "ds_labels" __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=True) # 集合标签 label_id = db.Column(db.Integer) # 集合内的标签ID name = db.Column(db.String(32), nullable=False) # 标签名称/类别 count = db.Column(db.Integer, default=0) # 标签使用量 def to_dict(self): """将对象转换为字典数据""" label_dict = { "label_id": self.label_id, "label_name": self.name, "label_count": self.count } return label_dict
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