示例#1
0
class ReadHistory(db.Model, CRUDMixin):
    """ 阅读历史"""

    __tablename__ = 'rd_history'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
    }
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.String(50), nullable=False)
    article_id = db.Column(db.Integer, nullable=False)
    count = db.Column(db.Integer, default=0)

    @classmethod
    def create_or_update(cls, user_id, article_id):
        """ 创建或更新"""
        item = cls.get_first(user_id=user_id, article_id=article_id)
        if item:
            try:
                item.count += 1
                db.session.commit()
            except:
                db.session.rollback()
        else:
            cls.create(user_id=user_id, article_id=article_id)
示例#2
0
class ArticleTagRef(db.Model, CRUDMixin):
    """ 文章标签关联表"""
    __tablename__ = 'article_tag_ref'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
    }

    id = db.Column(db.Integer, primary_key=True)
    article_id = db.Column(db.Integer, nullable=False)
    tag_id = db.Column(db.Integer, nullable=False)
示例#3
0
class Tag(db.Model, CRUDMixin):
    """ 标签"""

    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
    }

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30), unique=True, nullable=False)

    def to_json(self):
        return {
            'id': self.id,
            'name': self.name,
        }
示例#4
0
class Article(db.Model, CRUDMixin, ArticleStatus):
    """ 用户发表文章
    :field user_id: 微信openid
    :field category_id: 分类id
    :field title: 文章标题
    :field content: 文章内容
    :field image: 图片Url
    :field status: 文章状态
    """

    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
    }

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.String(50), nullable=False)
    category_id = db.Column(db.Integer, nullable=False)
    title = db.Column(db.String(255), nullable=False)
    content = db.Column(mysql.MEDIUMTEXT, nullable=False)
    image = db.Column(db.TEXT)
    status = db.Column(db.Integer, default=0)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now)
    soft_del = db.Column(db.Boolean, default=False)

    def to_json(self):
        user = User.get_first(id=self.user_id)
        category = Category.get_first(id=self.category_id)
        return {
            'id': self.id,
            'title': self.title,
            'content': self.content,
            'image': self.image,
            'createTime': fmt_dt(self.create_time),
            'updateTime': fmt_dt(self.update_time),
            'user': {
                'id': user.id,
                'name': user.name,
            },
            'category': {
                'id': category.id,
                'name': category.name,
            },
            'status': self.status,
        }
示例#5
0
class Favorite(db.Model, CRUDMixin):
    """ 用户收藏"""

    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
    }
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.String(50), nullable=False)
    article_id = db.Column(db.Integer, nullable=False)
    soft_del = db.Column(db.Boolean, default=False)

    @classmethod
    def create_or_update(cls, user_id, article_id):
        """ 创建胡更新"""
        item = cls.get_first(user_id=user_id, article_id=article_id)
        if item:
            item.delete()
        else:
            item.create(user_id=user_id, article_id=article_id)
示例#6
0
class Admin(db.Model, UserMixin):
    """ 用户"""

    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8',
    }

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)
    password_hash = db.Column(db.String(50), nullable=False)
    salt = db.Column(db.String(50), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now)
    soft_del = db.Column(db.Boolean, default=False)

    def __init__(self, username, password):
        """ 初始化"""
        self.username = username
        self.password = password

    @classmethod
    def create(cls, username, password):
        """ 创建用户"""
        admin = cls(username, password)
        db.session.add(admin)
        db.session.commit()
        return admin

    @property
    def password(self):
        """ 获取密码hash值"""
        raise AttributeError('Password is not readable.')

    @password.setter
    def password(self, password):
        """ 设置密码"""
        salt = get_code()
        self.salt = salt
        self.password_hash = cryptor.encrypt(password, salt=salt)

    def verify_password(self, password):
        """ 验证密码"""
        return self.password_hash == cryptor.encrypt(password, salt=self.salt)

    @classmethod
    def authenticate(cls, username, password):
        """ 验证"""
        admin = Admin.query.filter_by(username=username).first()
        if admin and admin.verify_password(password):
            return admin

    def __repr__(self):
        return '<Admin id: {}, username: {}>'.format(self.id, self.username)
示例#7
0
class Reply(db.Model, CRUDMixin):
    """ 用户评论"""

    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
    }

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.String(50), nullable=False)
    article_id = db.Column(db.Integer, nullable=False)
    content = db.Column(db.TEXT, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now)
    soft_del = db.Column(db.Boolean, default=False)
示例#8
0
class Like(db.Model, CRUDMixin):
    """ 点赞
    :field user_id: 点赞用户
    :field o_id: 模块的id
    :field type: 模块的类型(文章或这评论)
    """

    TYPE_ARTICLE = 'Article'
    TYPE_REPLY = 'Reply'

    __tablename__ = 'likes'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
    }
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.String(50), nullable=False)
    m_id = db.Column(db.Integer, nullable=False)
    type = db.Column(db.String(10), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    soft_del = db.Column(db.Boolean, default=False)
示例#9
0
class User(db.Model, CRUDMixin):
    """ 微信用户
    :field id: 微信openid
    :field name: 微信名称
    :field avatar_url: 微信头像
    :field gender: 性别
    :field country: 微信用户所在国家
    :field province: 微信用户所在省份
    :field city: 微信用户所在城市
    """
    __tablename__ = 'user'
    __table_args__ = {
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8mb4',
    }

    id = db.Column(db.String(50),
                   unique=True,
                   nullable=False,
                   primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    avatar_url = db.Column(db.String(400))
    gender = db.Column(db.Integer, default=1)
    country = db.Column(db.String(50), default='')
    province = db.Column(db.String(50), default='')
    city = db.Column(db.String(50), default='')
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now)
    last_login_time = db.Column(db.DateTime, default=datetime.now)
    soft_del = db.Column(db.Boolean, default=False)

    def to_json(self):
        return {
            'id': self.id,
            'name': self.name,
            'avatarUrl': self.avatar_url,
            'gender': self.gender,
            'country': self.country,
            'province': self.province,
            'city': self.city,
            'createTime': fmt_dt(self.create_time),
            'updateTime': fmt_dt(self.update_time),
        }