class Gatekeeper(db.Model): """权限""" __tablename__ = "gatekeeper" id = db.Column(db.Integer, unique=True, primary_key=True) name = db.Column(db.String(30)) description = db.Column(db.String(50)) staff_required = db.Column(db.Boolean, default=False) percent = db.Column(db.Integer, default=0) whitelist = db.relationship('Users', backref='user', lazy=True) def check_user(self, user): """ Staff user can pass check anywhere. Anonymous user can not pass check if staff required then white list is disabled. else check whitelist then check the percent """ if not user: return False if self.staff_required: return False if user in self.whitelist: return True else: return False # hash = hashlib.md5(str(user.id).encode('utf-8'))[-4:] # return int(hash, 16) % 100 < self.percent def __repr__(self): return '%s' % self.name
class Category(db.Model): """分类""" __tablename__ = "category" id = db.Column(db.Integer, unique=True, primary_key=True) title = db.Column(db.String(30)) def __repr__(self): return "<Category %r>" % self.title
class Staff(db.Model): """测试model,可用此model创建数据来测试sqlchemy的聚合、查询等方法""" __tablename__ = "staff" id = db.Column(db.Integer, unique=True, primary_key=True) name = db.Column(db.String(100)) score = db.Column(db.Integer, default=True) depart = db.ARRAY(db.String(100)) def __repr__(self): return "<Staff %r>" % self.id
class Picture(db.Model): """ 图片 """ __tablename__ = "picture" id = db.Column(db.Integer, unique=True, primary_key=True) image_url = db.Column(db.String(100)) desc = db.Column(db.String(80)) # 分类外键 category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship("Category", backref=db.backref('picture')) def __repr__(self): return "<Picture %r>" % self.id
class Picture(db.Model): """ 图片 """ __tablename__ = "picture" id = db.Column(db.Integer, unique=True, primary_key=True) image_url = db.Column(db.String(100)) desc = db.Column(db.String(80)) create_at = db.Column(db.Date, default=datetime.now) # 相册外键 album_id = db.Column(db.Integer, db.ForeignKey('album.id')) album = db.relationship('Album', backref=db.backref('picture')) def __repr__(self): return "<Picture %r>" % self.id
class Users(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) cover = db.Column(db.String(250)) username = db.Column(db.String(250), unique=True, nullable=False) password = db.Column(db.String(250)) gate = db.Column(db.Integer, db.ForeignKey('gatekeeper.id')) login_time = db.Column(db.Integer) begin_at = db.Column(db.Date, default=datetime.now) end_at = db.Column(db.Date, default=datetime.now) def __repr__(self): return self.username @property def is_authenticated(self): return True @property def is_active(self): return True @property def is_anonymous(self): return False def get_id(self): return self.id def set_password(self, password): return generate_password_hash(password) def check_password(self, hash, password): return check_password_hash(hash, password) def get(self, id): return self.query.filter_by(id=id).first() def add(self, user): db.session.add(user) return session_commit() def update(self): return session_commit() def delete(self, id): self.query.filter_by(id=id).delete() return session_commit()
class Video(db.Model): """ 视频 """ __tablename__ = "video" id = db.Column(db.Integer, unique=True, primary_key=True) video_url = db.Column(db.String(100)) desc = db.Column(db.String(80)) time_long = db.Column(db.Integer) small_img = db.Column(db.String(100)) watch = db.Column(db.Integer, default=0) def __repr__(self): return "<Video %r>" % self.id
class Album(db.Model): """相册""" __tablename__ = "album" id = db.Column(db.Integer, unique=True, primary_key=True) title = db.Column(db.String(30)) desc = db.Column(db.String(250)) cate = db.Column(db.Enum( '最爱', '风景', '人物', '动物', '游记', '卡通', '生活', '其他' ), server_default='最爱', nullable=False) cover = db.Column(db.String(250)) create_at = db.Column(db.Date, default=datetime.now) def __repr__(self): return "<Album %r>" % self.title