class Category(db.Model): id = db.TinyPrimaryKeyField() name = db.CharField(max_length=20) #分类名 slug = db.CharField(max_length=20) #访问url order = db.TinyIntegerField(default=1) #排序 def validate(self): if self.name and self.slug: self.slug = self.slug.lower() if not re.match('^[0-9a-z]+$', self.slug): raise Exception('访问目录只能是字母和数字组合') ft = ((Category.name == self.name) | (Category.slug == self.slug)) if self.id: ft = ft & (Category.id != self.id) if Category.select().where(ft).count() > 0: raise Exception('分类同名或者目录同名') else: raise Exception('请输入分类名或者访问目录') @classmethod def maxorder(cls): return Category.select().count() + 1 class Meta: db_table = 'categorys' order_by = ('order', )
class Page(db.Model): """ 静态页面 """ id = db.TinyPrimaryKeyField() name = db.CharField(max_length=20) slug = db.CharField(index=True, max_length=20) # 访问路径 content = db.TextField() # 页面内容 template = db.CharField(max_length=20, default='staticpage.html') def validate(self): if self.name and self.slug: self.slug = self.slug.lower() if not re.match('^[0-9a-z]+$', self.slug): raise Exception('访问目录只能是字母和数字组合') ft = ((Page.name == self.name) | (Page.slug == self.slug)) if self.id: ft = ft & (Page.id != self.id) if Page.select().where(ft).count() > 0: raise Exception('分类同名或者目录同名') else: raise Exception('请输入分类名或者访问目录') class Meta: db_table = 'page'
class Distribution(db.Model): """ 配送方式 """ id = db.TinyPrimaryKeyField() pdid = db.TinyIntegerField() # 是否为第一级 name = db.CharField(max_length=20) # 配送方式名称 price = db.FloatField(default=0.0) # 配送价格 content = db.CharField() # 如果为第二级则有内容选择 def validate(self): if self.name: ft = (Distribution.name == self.name) if self.id: ft = ft & (Distribution.id != self.id) if Distribution.select().where(ft).count() > 0: raise Exception('此配送方式已存在') else: raise Exception('请输入配送方式') class Meta: db_table = 'distribution' order_by = ('pdid',)
class Ad(db.Model): id = db.TinyPrimaryKeyField() url = db.CharField(max_length=50) #商品连接地址 def validate(self): if not self.url: raise Exception('情输入访问url') class Meta: db_table = 'ads'
class Area(db.Model): id = db.TinyPrimaryKeyField() pid = db.TinyIntegerField(default=0) name = db.CharField(max_length=30) def validate(self): if self.name: ft = (Area.name == self.name) if self.id: ft = ft & (Area.id != self.id) if Area.select().where(ft).count() > 0: raise Exception('地区名已存在') else: raise Exception('请输入地区名') class Meta: db_table = 'areas'