class Category(SurrogatePK,Model): """产品分类表:categorys 列表参数: - product_id:外键产品表 产品id - name:分类名称 - ico:分类图标 - sort:分类排序 - active:是否激活 用户是否删除? - show:是否显示 """ __tablename__ = 'categorys' name = Column(db.String(100)) ico = Column(db.String(200)) sort = Column(db.Integer()) active = Column(db.Boolean(),default=True) show = Column(db.Boolean(),default=True) #:自身上级,引用自身无限级分类 parent = reference_col('categorys') childrens = relationship("Category",lazy="joined",join_depth=2) product_id = relationship(Product, backref='products')
class CategoryAttr(SurrogatePK, Model): """默认的三个类型选项 用户无法更改,mark标记唯一, 管理员更改,外键模板 模板可添加多个文章页等 数据库读取mark标记 """ __tablename__ = 'category_attrs' name = Column(db.String(80), nullable=False) summary = Column(db.String(80), nullable=False) mark = Column(db.String(80), nullable=False) #链接地址 url = Column(db.String(80), nullable=False) #模板页 templates = Column(db.String(80), nullable=False) #用户分类 category = relationship('Category', backref='categorys_attr', lazy='select') #默认内容 static_context = relationship('TemplatesDefault', backref='category_attrs') #模板 usertemplate_id = reference_col('user_templates') #: 栏目页 ,(文章页,图集页,内容页 ) def __repr__(self): return '<({name!r})>'.format(name=self.name)
class Category(SurrogatePK,Model): __tablename__ = 'categorys' #:栏目标题 title = Column(db.String(80),nullable=False) #:栏目描 summary = Column(db.String(100)) #:关键字 key = Column(db.String(80)) #:关键描述 word = Column(db.String(500)) #:创建时间 creation_date = Column(db.DateTime(),default=dt.datetime.now) #:排序 sort = Column(db.Integer,default=100) #:是否显示 is_enable = Column(db.Boolean,default=True) #:首页展示顺序 main_sort = Column(db.Integer,default=100) #:栏目静态内容 static_content = Column(db.UnicodeText) #:附加内容 attach_value = Column(db.UnicodeText) #状态 0用户删除不做正式删除 1正常 state = Column(db.Integer,default=1) attr_id = reference_col('category_attrs') #:所属用户栏目 user = reference_col('users') #:引用自身无限级分类 parent_id = reference_col('categorys') children = relationship("Category",lazy="joined",join_depth=2,post_update=True) #所属文章 article = relationship('Article', backref='categorys',lazy='dynamic') #所属图集 photo = relationship('Photo', backref='categorys',lazy='dynamic') #栏目静态内容 static_context = relationship('StaticContext', backref='categorys',lazy='dynamic') def __repr__(self): return '<CategoryAttr({name!r})>'.format(name=self.title)
class UserTemplate(SurrogatePK, Model): __tablename__ = 'user_templates' name = Column(db.String(80), nullable=False) #是否完结默认模板内容,完结列表不在显示,以后模板多了会有很多列表所以在此设置 is_end = Column(db.Boolean, default=False) #模板容量,用户选择该模板注册后所拥有的容量 memory_capacity = Column(db.Integer, default=0) #默认模板主页内容 # template_default = relationship('TemplatesDefault', backref='template',lazy='select') categoryattr = relationship('CategoryAttr', backref='usertemplate') user_id = relationship('User', backref='usertemplate', uselist=False) #用户网址模板关联 user_url_and_template = relationship('UserUrlAndTemplate', backref='usertemplate')
class Role(SurrogatePK, Model): """A role for a user.""" __tablename__ = 'roles' name = Column(db.String(80), unique=True, nullable=False) user_id = reference_col('users', nullable=True) user = relationship('User', backref='roles') def __init__(self, name, **kwargs): """Create instance.""" db.Model.__init__(self, name=name, **kwargs) def __repr__(self): """Represent instance as a unique string.""" return '<Role({name})>'.format(name=self.name)
class User(UserMixin, SurrogatePK, Model): """A user of the app.""" __tablename__ = 'users' username = Column(db.String(80), nullable=False) #: The hashed password password = Column(db.Binary(128), nullable=False) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) first_name = Column(db.String(30), nullable=True) last_name = Column(db.String(30), nullable=True) active = Column(db.Boolean(), default=True) is_admin = Column(db.Boolean(), default=False) phone = Column(db.String(20)) memory_capacity = Column(db.Integer, default=0) #用户内存容量 #网站名称 web_name = Column(db.String(200)) #网站关键词字 web_key = Column(db.String(200)) web_word = Column(db.String(500)) #网站分页数据 article_page = Column(db.Integer, default=20) photo_page = Column(db.Integer, default=20) #logo字 web_logo_word = Column(db.String(50)) web_logo_img = Column(db.String(200)) #文章展示和图集展示静态内容 article_context = Column(db.UnicodeText) photo_context = Column(db.UnicodeText) #网站状态,1正常 0前台关闭 web_state = Column(db.Integer, default=1) category = relationship('Category', backref='users', lazy='dynamic') banner = relationship('Banner', backref='banners', lazy='dynamic') static_context = relationship('StaticContext', backref='users', lazy='dynamic') article = relationship('Article', backref='users', lazy='dynamic') photo = relationship('Photo', backref='users', lazy='dynamic') user_url_and_template = relationship('UserUrlAndTemplate', backref='users') user_mark = relationship('UserMark', backref='users') user_mark_type = relationship('UserMarkType', backref='users') #用户模板表 templates = reference_col('user_templates') def __init__(self, username, password=None, **kwargs): """Create instance.""" db.Model.__init__(self, username=username, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash(password) def check_password(self, value): """Check password.""" return bcrypt.check_password_hash(self.password, value) @property def full_name(self): """Full user name.""" return '{0} {1}'.format(self.first_name, self.last_name) def __repr__(self): """Represent instance as a unique string.""" return '<User({username!r})>'.format(username=self.username) def __str__(self): return 'users:%s' % self.username #管理员 def is_administrator(self): return self.is_admin def is_superadmin(self): return self.is_admin
class User(SurrogatePK, Model, UserMixin): """用户表. 表名称:users 列名称: - username:用户名 - password:密码 ,hashed password - created_at:创建时间 - name:姓名 - active:是否激活。默认false - active_at:激活时间,默认当前时间 - is_admin:是否管理员 - is_center:是否报单中心 - phone:手机号,用于登录等 - buys_id:外键引用 产品购买表 - integral:积分 - coins:硬币 - parent_center:引用自身 上级 - children_center:引用自身 - recommends:引用推荐表 - recommender:外键引用推荐人表 - buys_car_id:外键购物车 """ __tablename__ = 'users' username = Column(db.String(80), unique=True, nullable=False) password = Column(db.Binary(128), nullable=False) created_at = Column(db.DateTime, nullable=False, default=datetime.datetime.now) name = Column(db.String(30)) active = Column(db.Boolean(), default=False) active_at = Column(db.DateTime, nullable=False, default=datetime.datetime.now) is_admin = Column(db.Boolean(), default=False) is_center = Column(db.Boolean(), default=False) phone = Column(db.String(30), unique=True) integral = Column(db.Integer(), default=0) coins = Column(db.Integer(), default=0) parent_center = reference_col('users') buys_id = relationship('Order', backref='user') children_center = relationship("User", join_depth=2, lazy="joined", post_update=True) recommends = db.relationship('Recommend', foreign_keys=[Recommend.recommender_id], backref=db.backref('recommender', lazy='joined'), lazy='dynamic', cascade='all, delete-orphan') recommender = db.relationship('Recommend', foreign_keys=[Recommend.recommends_id], backref=db.backref('recommends', lazy='joined'), lazy='dynamic', cascade='all, delete-orphan') buys_car_id = relationship(BuysCar, backref='users') roles = db.relationship('Role', secondary=users_roles, backref=db.backref('roles', lazy='dynamic')) def __init__(self, username, password=None, **kwargs): """Create instance.""" db.Model.__init__(self, username=username, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash(password) def check_password(self, value): """Check password.""" return bcrypt.check_password_hash(self.password, value) @property def full_name(self): """Full user name.""" return '{0} {1}'.format(self.first_name, self.last_name) def __repr__(self): """Represent instance as a unique string.""" return '<User({username!r})>'.format(username=self.username) def is_administrator(self): """管理员""" return self.is_admin def init_insert(): User.create(username='******', password='******', active=True) def generate_auth_token(self, expiration=600): 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) except SignatureExpired: return None # valid token, but expired except BadSignature: return None # invalid token user = User.query.get(data['id']) return user def add_role(self, role): self.roles.append(role) def add_roles(self, roles): for role in roles: self.add_role(role) def get_roles(self): for role in self.roles: yield role