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)
class Photo(db.Model, CRUDMixin): id = sqla.Column(sqla.Integer, primary_key=True) create_time = sqla.Column(sqla.DateTime, default=datetime.utcnow) name = db.Column(sqla.String(50)) qiniu_key = db.Column(sqla.String(200)) album_id = db.Column(sqla.Integer, db.ForeignKey('album.id')) def __repr__(self): return u'<Photo: {}>'.format(self.name) @property def url(self): return u'{}/{}'.format(flask_qiniu.PUBLIC_CDN_DOMAIN, self.qiniu_key) @property def thumbnail(self): return u'{}?imageView/2/w/150'.format(self.url) @property def with_watermark(self): return '' @property def with_watermark_thumbnail(self): return ''
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'))
class Activity(CRUDMixin, CreateUpdateTimeMixin, db.Model): id = db.Column(sqla.Integer, primary_key=True) # create_time = db.Column(sqla.DateTime, default=datetime.utcnow) # update_time = db.Column(sqla.DateTime, onupdate=datetime.utcnow) title = db.Column(db.String(255)) origin = db.Column(db.String(255), default='') html = db.Column(db.Text) # status = db.Column(sqla.Boolean, default=False) publish = db.Column(sqla.Boolean, default=False) category = db.Column(sqla.Enum('policy', 'news', 'events', 'research', 'activity', 'achievement', name='category'), default='policy') user_id = db.Column(db.Integer, db.ForeignKey(USER_TABLE_USER_ID)) user = db.relationship('User') @classmethod def weekly_popular_top10(cls): return cls.query.limit(10).all() @property def link(self): return url_for('activity_content.activity_view', id=self.id) @property def edit_link(self): redirect_url = url_for('activity_content.activity_view', id=self.id) return '/admin/activity/edit?url={}&id={}'.format( redirect_url, self.id)
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))
class Page(CRUDMixin, CreateUpdateTimeMixin, db.Model): id = db.Column(db.Integer, primary_key=True) # create_time = db.Column(db.DateTime, default=datetime.now) # update_time = db.Column(db.DateTime, onupdate=datetime.now) title = db.Column(db.String(255)) html = db.Column(db.Text) status = db.Column(db.String(2), default='1') user_id = db.Column(db.Integer, db.ForeignKey(USER_TABLE_USER_ID)) @property def edit_link(self): redirect_url = url_for('activity_content.pages', page_title=self.title) return '/admin/page/edit?url={}&id={}'.format(redirect_url, self.id)
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)
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)
# -*- coding: utf-8 -*- from __future__ import absolute_import from datetime import datetime from flask_security import RoleMixin from flask_login import UserMixin from youjiao.extensions import db, redis_cli, USER_TABLE_NAME, USER_TABLE_USER_ID from youjiao.utils.database import CRUDMixin import sqlalchemy as sqla from .permissions import vip_permission from .utils import generate_random_number_4, generate_random_string_4, encrypt_password, \ verify_password, get_hmac, password_context roles_users = db.Table( 'roles_users', db.Column('user_id', sqla.Integer(), db.ForeignKey('youjiao_user.id')), db.Column('role_id', sqla.Integer(), db.ForeignKey('role.id')), sqla.UniqueConstraint('user_id', 'role_id') ) 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