Exemplo n.º 1
0
class Application(db.Model):
    """ 应用表 """
    __tablename__ = 'apps'
    __table_args__ = (db.UniqueConstraint('user_id', 'appname'), )

    id = db.Column(db.Integer, primary_key=True, comment=u'自增id')
    user_id = db.Column(db.Integer,
                        db.ForeignKey('user.id'),
                        nullable=False,
                        comment=u'应用所属用户')
    appname = db.Column(db.String(128), nullable=False, comment=u'应用名')
    apptype_id = db.Column(db.Integer,
                           db.ForeignKey('app_type.id'),
                           nullable=False,
                           comment=u'应用分类')
    appdes = db.Column(db.String(500), comment=u'应用描述')
    appkey = db.Column(db.String(32),
                       unique=True,
                       index=True,
                       nullable=False,
                       comment=u'应用的标识')
    secret = db.Column(db.String(32),
                       unique=True,
                       nullable=False,
                       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'最后更新时间')
Exemplo n.º 2
0
class Consume(db.Model):
    """ 资源消费表 """
    __tablename__ = 'consume'
    __table_args__ = (db.UniqueConstraint('claim_id', 'customer', 'appkey'),
                      )  # 消费者对某资源只需一次付费

    txid = db.Column(db.String(64), primary_key=True, comment=u'消费资源,交易id')
    claim_id = db.Column(db.String(64),
                         db.ForeignKey('content.claim_id'),
                         nullable=False,
                         comment=u'外键,资源交易id')
    customer = db.Column(db.String(64),
                         nullable=False,
                         index=True,
                         comment=u'消费者(某个应用的用户名)')
    appkey = db.Column(db.String(32),
                       db.ForeignKey('apps.appkey'),
                       index=True,
                       nullable=False)
    price = db.Column(db.Numeric(20, 8),
                      nullable=False,
                      default=0,
                      comment=u'消费价格')
    create_timed = db.Column(db.DateTime,
                             server_default=db.func.now(),
                             comment=u'资源消费时间, 默认为当前时间')
Exemplo n.º 3
0
class ContentHistory(db.Model):
    """资源历史记录(新增/更新/删除)"""
    __tablename__ = 'content_history'

    txid = db.Column(db.String(64), primary_key=True, comment=u'元数据上链交易id')
    claim_id = db.Column(db.String(40), 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')
    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.删除')
    create_timed = db.Column(db.DateTime, server_default=db.func.now(), comment=u'资源创建时间, 默认为当前时间')

    @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()))
Exemplo n.º 4
0
class StatisticsAppUser(db.Model):
    """ Statistical user """
    __tablename__ = 'statistics_appuser'
    __table_args__ = (db.UniqueConstraint('appkey', 'app_username'), )

    id = db.Column(db.Integer, primary_key=True, comment=u'自增id')
    appkey = db.Column(db.String(32),
                       db.ForeignKey('apps.appkey'),
                       nullable=False)
    app_username = db.Column(db.String(64), nullable=False, comment=u'app的用户名')
    create_timed = db.Column(db.DateTime,
                             nullable=False,
                             server_default=db.func.now(),
                             comment=u'创建时间')
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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')
Exemplo n.º 7
0
class AppUser(db.Model):
    """ 用于记录应用的用户, 仅作备份

    当应用给其用户请求生成钱包时,插入一条数据
    """
    __tablename__ = 'app_user'
    __table_args__ = (db.UniqueConstraint('appkey', 'app_username'), )

    id = db.Column(db.Integer, primary_key=True, comment=u'自增id')
    appkey = db.Column(db.String(32),
                       db.ForeignKey('apps.appkey'),
                       nullable=False)
    app_username = db.Column(db.String(64), nullable=False, comment=u'app的用户名')
    create_timed = db.Column(db.DateTime,
                             nullable=False,
                             server_default=db.func.now(),
                             comment=u'创建时间')
Exemplo n.º 8
0
class Tag(db.Model):
    """ 资源标签表 """
    __tablename__ = 'tag'

    name = db.Column(db.String(32), primary_key=True, comment=u'主键,标签名称')
    create_timed = db.Column(db.DateTime,
                             server_default=db.func.now(),
                             comment=u'创建时间')
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
class Consume(db.Model):
    """ 资源消费表 """
    __tablename__ = 'consume'
    __table_args__ = (db.UniqueConstraint('claim_id', 'customer', 'appkey'),)  # 消费者对某资源只需一次付费

    txid = db.Column(db.String(64), primary_key=True, comment=u'消费资源,交易id')
    claim_id = db.Column(db.String(64), db.ForeignKey('content.claim_id'), nullable=False, comment=u'外键,资源交易id')
    customer = db.Column(db.String(64), nullable=False, index=True, comment=u'消费者(某个应用的用户名)')
    appkey = db.Column(db.String(32), db.ForeignKey('apps.appkey'), index=True, nullable=False)
    price = db.Column(db.Numeric(20, 8), nullable=False, default=0, comment=u'消费价格')
    create_timed = db.Column(db.DateTime, server_default=db.func.now(), comment=u'资源消费时间, 默认为当前时间')

    @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()))
Exemplo n.º 11
0
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
Exemplo n.º 12
0
# -*- coding: utf-8 -*-
# @Date    : 2018/4/3
# @Author  : Shu
# @Email   : [email protected]

from ulord.extensions import db
import time

content_tag = db.Table(
    'content_tag',
    db.Column('tag_name', db.String(32), db.ForeignKey('tag.name')),
    db.Column('content_id', db.Integer, db.ForeignKey('content.id')),
    db.PrimaryKeyConstraint('tag_name', 'content_id'))


class Tag(db.Model):
    """ 资源标签表 """
    __tablename__ = 'tag'

    name = db.Column(db.String(32), primary_key=True, comment=u'主键,标签名称')
    create_timed = db.Column(db.DateTime,
                             server_default=db.func.now(),
                             comment=u'创建时间')


class Consume(db.Model):
    """ 资源消费表 """
    __tablename__ = 'consume'
    __table_args__ = (db.UniqueConstraint('claim_id', 'customer', 'appkey'),
                      )  # 消费者对某资源只需一次付费
Exemplo n.º 13
0
class ContentHistory(db.Model):
    """资源历史记录(新增/更新/删除)"""
    __tablename__ = 'content_history'

    id = db.Column(db.Integer, primary_key=True, comment='自增id')
    txid = db.Column(db.String(64), comment='元数据上链交易id')
    claim_id = db.Column(db.String(40),
                         nullable=False,
                         comment='元数据在ulord链上的claim_id(资源的唯一标识)')
    claim_name = db.Column(db.String(32), nullable=False, comment='资源在链上的更新标识')
    fee = db.Column(db.Float, default=0, comment='手续费')
    nout = db.Column(db.Integer, default=0, comment='此次交易中的第几个交易')
    author = db.Column(db.String(64),
                       nullable=False,
                       index=True,
                       comment='资源发布者(某个应用的用户名)')
    appkey = db.Column(db.String(32),
                       db.ForeignKey('apps.appkey'),
                       nullable=False,
                       index=True,
                       comment='应用的appkey')
    title = db.Column(db.String(64), nullable=False, comment='资源标题')
    udfs_hash = db.Column(db.String(46),
                          nullable=False,
                          comment='内容文件在UDFS中的hash')
    price = db.Column(db.Float, nullable=False, default=0, comment='定价')
    content_type = db.Column(db.String(16), nullable=False, comment='资源类型(后缀)')
    currency = db.Column(db.String(8),
                         default='UT',
                         nullable=False,
                         comment='货币类型')
    bid = db.Column(db.Float, nullable=False, comment="支付给平台的费用")
    des = db.Column(db.String(1024), comment='资源描述')
    status = db.Column(db.Integer,
                       nullable=False,
                       index=True,
                       default=1,
                       comment='状态:1.新增 2.更新 3.删除')
    create_timed = db.Column(db.DateTime,
                             server_default=db.func.now(),
                             comment='资源创建时间, 默认为当前时间')
    thumbnail = db.Column(db.String(1024), comment='缩略图')
    preview = db.Column(db.String(1024), comment='预览')
    language = db.Column(db.String(8), comment='语言')
    license = db.Column(db.String(16), comment='许可证')
    license_url = db.Column(db.String(128), comment='许可证url')

    @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()))
Exemplo n.º 14
0
# -*- coding: utf-8 -*-
# @Date    : 2018/4/3
# @Author  : Shu
# @Email   : [email protected]

from ulord.extensions import db
import time

content_tag = db.Table('content_tag', db.Column('tag_name', db.String(32), db.ForeignKey('tag.name')),
                       db.Column('content_id', db.Integer, db.ForeignKey('content.id')),
                       db.PrimaryKeyConstraint('tag_name', 'content_id'))


class Tag(db.Model):
    """ 资源标签表 """
    __tablename__ = 'tag'

    name = db.Column(db.String(32), primary_key=True, comment=u'主键,标签名称')
    create_timed = db.Column(db.DateTime, server_default=db.func.now(), comment=u'创建时间')


class Consume(db.Model):
    """ 资源消费表 """
    __tablename__ = 'consume'
    __table_args__ = (db.UniqueConstraint('claim_id', 'customer', 'appkey'),)  # 消费者对某资源只需一次付费

    txid = db.Column(db.String(64), primary_key=True, comment=u'消费资源,交易id')
    claim_id = db.Column(db.String(64), db.ForeignKey('content.claim_id'), nullable=False, comment=u'外键,资源交易id')
    customer = db.Column(db.String(64), nullable=False, index=True, comment=u'消费者(某个应用的用户名)')
    appkey = db.Column(db.String(32), db.ForeignKey('apps.appkey'), index=True, nullable=False)
    price = db.Column(db.Numeric(20, 8), nullable=False, default=0, comment=u'消费价格')