class Content(db.Model): """ 发布资源内容表 在ulord链上, 一个资源claim_id不会变, 而txid可以生成一笔新交易(在更新时) 删除: 在链上会将claim_id解绑, 从一个特殊交易转变为普通交易(新建: 普通交易变为特殊带claim_id的交易) 这张表只存储最新的数据, 如果有更新, 则更新表中的数据 """ __tablename__ = 'content' id = db.Column(db.Integer, primary_key=True, comment=u'自增id') claim_id = db.Column(db.String(40), unique=True, nullable=False, comment=u'元数据在ulord链上的claim_id(资源的唯一标识)') author = db.Column(db.String(64), nullable=False, index=True, comment=u'资源发布者(某个应用的用户名)') appkey = db.Column(db.String(32), db.ForeignKey('apps.appkey'), nullable=False, index=True, comment=u'应用的appkey') txid = db.Column(db.String(64), unique=True, nullable=False, comment=u'元数据上链交易id') title = db.Column(db.String(64), nullable=False, comment=u'资源标题') ipfs_hash = db.Column(db.String(46), nullable=False, comment=u'内容文件在IPFS中的hash') price = db.Column(db.Numeric(20, 8), nullable=False, default=0, comment=u'定价') content_type = db.Column(db.String(16), nullable=False, comment=u'资源类型(后缀)') currency = db.Column(db.String(8), default='ULD', nullable=False, comment=u'货币类型') des = db.Column(db.String(1024), comment=u'资源描述') status = db.Column(db.Integer, nullable=False, index=True, default=1, comment=u'状态:1.新增 2.更新 3.删除') enabled = db.Column(db.Boolean, nullable=False, default=True, comment=u'标识资源是否可用') create_timed = db.Column(db.DateTime, server_default=db.func.now(), comment=u'资源创建时间, 默认为当前时间') update_timed = db.Column(db.DateTime, onupdate=db.func.now(), comment=u'最后更新时间') views = db.Column(db.Integer, default=0, comment=u'浏览量') tags = db.relationship('Tag', secondary='content_tag', backref=db.backref('content', lazy='dynamic')) consumes = db.relationship('Consume', backref=db.backref('content'), lazy='dynamic') @property def create_timed_str(self): """输出日期字符串""" return self.create_timed.strftime("%Y-%m-%d %H:%M:%S") @property def create_timed_timestamp(self): """输出时间戳""" return int(time.mktime(self.create_timed.timetuple())) @property def update_timed_str(self): """输出日期字符串""" if self.update_timed: return self.update_timed.strftime("%Y-%m-%d %H:%M:%S") else: return None @property def update_timed_timestamp(self): """输出时间戳""" if self.update_timed: return int(time.mktime(self.update_timed.timetuple())) else: return None
class Type(db.Model): """ 应用分类表,由管理员插入数据 """ __tablename__ = 'app_type' id = db.Column(db.Integer, primary_key=True, comment=u'自增id') parent_id = db.Column(db.Integer, db.ForeignKey('app_type.id'), index=True, comment=u'父类别id') name = db.Column(db.String(32), unique=True, nullable=False, index=True, comment=u'类型名称') des = db.Column(db.String(256), comment=u'类型描述') app = db.relationship('Application', backref='type', lazy='dynamic') parent = db.relationship('Type', uselist=False)
class Role(db.Model): """ 用户角色权限表 normal, blocked, admin """ __tablename__ = 'user_role' id = db.Column(db.Integer, primary_key=True, comment=u'角色id') name = db.Column(db.String(32), unique=True, nullable=False, comment=u'角色名') des = db.Column(db.String(500), nullable=False, comment=u'角色描述') user = db.relationship('User', backref='role', lazy='dynamic')
class User(db.Model): """ 开发者用户表 """ __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True, comment=u'自增id') username = db.Column(db.String(32), unique=True, nullable=False, index=True, comment=u'开发者帐号') password_hash = db.Column('password', db.String(128), nullable=False, comment=u'开发者密码') telphone = db.Column(db.String(24), unique=True, comment=u'开发者电话') email = db.Column(db.String(128), unique=True, comment=u'开发者邮箱') create_timed = db.Column(db.DateTime, server_default=db.func.now(), comment=u'创建时间') update_timed = db.Column(db.DateTime, onupdate=db.func.now(), comment=u'最后更新时间(新纪录为空值)') role_id = db.Column(db.Integer, db.ForeignKey('user_role.id'), nullable=False, comment='角色id') app = db.relationship('Application', backref='user', lazy='dynamic') @property def password(self): raise AttributeError("Password don't allow read.") @password.setter def password(self, password): self.password_hash = password def __repr__(self): return '<User {}>'.format(self.username) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def generate_auth_token(self, expiration=60 * 60 * 3): s = Serializer(current_app.config['SECRET_KEY'], expires_in=expiration) return s.dumps({'id': self.id}) @staticmethod def verify_auth_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) if 'id' not in data: return None user = User.query.get(data['id']) return user except Exception as e: print('e: ', e) return None @property def create_timed_str(self): """输出日期字符串""" return self.create_timed.strftime("%Y-%m-%d %H:%M:%S") @property def create_timed_timestamp(self): """输出时间戳""" return int(time.mktime(self.create_timed.timetuple())) @property def update_timed_str(self): """输出日期字符串""" if self.update_timed: return self.update_timed.strftime("%Y-%m-%d %H:%M:%S") else: return None @property def update_timed_timestamp(self): """输出时间戳""" if self.update_timed: return int(time.mktime(self.update_timed.timetuple())) else: return None