Ejemplo n.º 1
0
class LeaveMessage(CRUDMixin, db.Model):
    id = sqla.Column(sqla.Integer, primary_key=True)
    create_time = db.Column(sqla.DateTime, default=datetime.utcnow)
    content = db.Column(sqla.String(length=1000))

    user_id = db.Column(sqla.Integer, db.ForeignKey(USER_TABLE_USER_ID))
    user = db.relationship('User', backref=db.backref('leave_messages'))
Ejemplo n.º 2
0
class Favor(CRUDMixin, db.Model):
    id = sqla.Column(sqla.Integer, primary_key=True)
    create_time = db.Column(sqla.DateTime, default=datetime.utcnow)

    user_id = db.Column(sqla.Integer, db.ForeignKey(USER_TABLE_USER_ID))
    user = db.relationship('User', backref=db.backref('favors'))
    # 喜爱对象id
    obj_id = db.Column(sqla.Integer, db.ForeignKey('book.id'))
    # 喜爱对象类型
    obj_type = db.Column(
        sqla.Enum('book', 'courseware', 'onlinecourse', name='like_obj_type'))

    __table_args__ = (UniqueConstraint('user_id',
                                       'obj_id',
                                       'obj_type',
                                       name='_user_favor_obj'), )

    @property
    def favor_model(self):
        from youjiao.teach_material.models import Book, Courseware
        from youjiao.onlinecourse.models import OnlineCourse
        if self.obj_type == 'book':
            return Book
        if self.obj_type == 'courseware':
            return OnlineCourse
        if self.obj_type == 'onlinecourse':
            return OnlineCourse
        else:
            return None

    @property
    def real_obj(self):
        model = self.favor_model
        return model.query.get(self.obj_id)
Ejemplo n.º 3
0
class Comment(CRUDMixin, db.Model):
    id = sqla.Column(sqla.Integer, primary_key=True)
    create_time = db.Column(sqla.DateTime, default=datetime.utcnow)
    user_id = db.Column(sqla.Integer, db.ForeignKey(USER_TABLE_USER_ID))
    user = db.relationship('User', backref=db.backref('comments'))
    # 评论对象id
    comment_obj_id = db.Column(sqla.Integer)
    # 评论对象类型
    comment_obj_type = db.Column(
        sqla.Enum('book', 'courseware', name='comment_obj_type'))
    content = db.Column(sqla.String(140))
Ejemplo n.º 4
0
class UserProfile(db.Model, CRUDMixin):
    id = db.Column(sqla.Integer, primary_key=True)
    user_id = db.Column(sqla.Integer, db.ForeignKey(USER_TABLE_USER_ID))
    user = db.relationship('User', backref=db.backref('profile', uselist=False))
    # TODO: add column describe/description
    nickname = db.Column(sqla.String(16), unique=True)
    work_place_name = db.Column(sqla.String(255))
    avatar_qiniu_key = db.Column(sqla.String(200), default='default_avatar.png')
    birthday = db.Column(sqla.Date)
    gender = db.Column(sqla.Enum('male', 'female', name='gender'))
    career = db.Column(sqla.String(16))
    province = db.Column(sqla.String(16))
    city = db.Column(sqla.String(16))
    district = db.Column(sqla.String(16))
    street = db.Column(sqla.String(16))

    @property
    def location(self):
        return '{} {} {} {}'.format(self.province, self.city, self.district, self.street)
Ejemplo n.º 5
0
class User(db.Model, UserMixin, CRUDMixin):
    __tablename__ = USER_TABLE_NAME
    id = sqla.Column(sqla.Integer, primary_key=True)
    name = db.Column(sqla.String(50), unique=True)
    email = db.Column(sqla.String(50), unique=True)
    password = db.Column(sqla.String(200))

    phone_number = db.Column(sqla.String(16), unique=True)
    active = db.Column(sqla.Boolean, default=True)
    # is_admin = db.Column(db.Boolean, default=False)
    # TODO: use a base model to add create_time and update_time
    create_time = db.Column(sqla.DateTime, default=datetime.utcnow)
    last_login = db.Column(sqla.DateTime)

    # TODO: add login log field, and add field update in login view
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))
    # profile = db.relationship('UserProfile', backref='user', uselist=False)
    # from youjiao.user_util.models import Comment, Favor
    # comments = db.relationship('Comment', backref='user')
    # favors = db.relationship('Favor', backref='user')

    def __repr__(self):
        return '<User {}>'.format(self.name)

    @classmethod
    def create_user(cls, name, email, password):
        password = encrypt_password(password)
        user = User()
        user.name = name
        user.email = email
        user.password = password
        user.save()
        return user

    @classmethod
    def verify(cls, email_or_name, password):
        user = cls.query.filter((User.email==email_or_name)|(User.name==email_or_name)).first()
        if not user:
            return None
        if not user.verify_and_update_password(password):
            return None
        return user

    def set_password(self, new_password):
        self.password = encrypt_password(new_password)
        self.save()

    def verify_password(self, password):
        return verify_password(password, self.password)

    def verify_and_update_password(self, password):
        """used in login user"""
        signed = get_hmac(password).decode('ascii')
        verified, new_password = password_context.verify_and_update(signed, self.password)
        if verified and new_password:
            self.password = new_password
            self.save()
        return verified

    @property
    def roles_name(self):
        return [role.name for role in self.roles]

    def favor(self, obj_type):
        from youjiao.user_util.models import Favor
        pass

    @property
    def book_visit_recent_key(self):
        return 'user_{}_book_visit_list'.format(self.id)

    @property
    def courseware_visit_recent_key(self):
        return 'user_{}_courseware_visit_list'.format(self.id)

    @property
    def onlinecourse_visit_recent_key(self):
        return 'user_{}_onlinecourse_visit_list'.format(self.id)

    @property
    def book_visit_recent(self):
        from youjiao.teach_material.models import Book
        book_id_list = redis_cli.lrange(self.book_visit_recent_key, 0, 8)
        return Book.query.filter(Book.id.in_(book_id_list))

    @property
    def courseware_visit_recent(self):
        from youjiao.teach_material.models import Courseware
        courseware = redis_cli.lrange(self.courseware_visit_recent_key, 0, 8)
        return Courseware.query.filter(Courseware.id.in_(courseware))

    @property
    def onlinecourse_visit_recent(self):
        from youjiao.onlinecourse.models import OnlineCourse
        onlinecourse_id_list = redis_cli.lrange(self.onlinecourse_visit_recent_key, 0, 8)
        return OnlineCourse.query.filter(OnlineCourse.id.in_(onlinecourse_id_list))

    @property
    def can_view_vip_content(self):
        return vip_permission.can()
Ejemplo n.º 6
0
class VIP(db.Model, CRUDMixin):
    id = db.Column(sqla.Integer, primary_key=True)
    user_id = db.Column(sqla.Integer, db.ForeignKey(USER_TABLE_USER_ID))
    user = db.relationship('User', backref=db.backref('vips'))
    begin = db.Column(sqla.Date)
    end = db.Column(sqla.Date)