class Tag(db.Model, TimestampMixin): __tablename__ = 'tags' __table_args__ = (db.UniqueConstraint('tag'), ) id = db.Column(db.Integer, primary_key=True) tag = db.Column(db.String(80), nullable=False) lang = db.Column(db.String(2), nullable=False)
class User(BaseModel, ModelMixin): __tablename__ = 'user' nickname = db.Column(db.String(32), unique=True, comment='用户名') email = db.Column(db.String(50), unique=True, comment='邮箱') password = db.Column(db.String(32), nullable=True, comment='密码') secret_token = db.Column(db.String(32), nullable=True, comment='访问密匙') salt = db.Column(db.String(32), nullable=True, comment='密码盐')
class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), nullable=False, unique=True) password_hash = db.Column(db.String(128), nullable=False) def __repr__(self): return self.username
class SearchCompany(db.Model, TimestampMixin): __tablename__ = 'search_companies' id = db.Column(db.Integer, primary_key=True) company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable=False) initial_index = db.Column(db.String(255), nullable=False) phoneme_index = db.Column(db.String(255), nullable=False)
class CompanyName(db.Model, TimestampMixin): __tablename__ = 'company_names' __table_args__ = (db.UniqueConstraint('company_id', 'lang'), ) id = db.Column(db.Integer, primary_key=True) company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable=False) name = db.Column(db.String(80), nullable=False) lang = db.Column(db.String(2), nullable=False)
class User(BaseModel): """ User database model """ __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(255), nullable=False) last_name = db.Column(db.String(255), nullable=False) email = db.Column(db.String(255), unique=True, nullable=False) user_type = db.Column(db.Enum(UserType), default='AUTHOR') @classmethod def find_by_email(cls, email): """ Find user by email """ return cls.query.filter_by(email=email).first()
class TaskLog(db.Model): __tablename__ = 'taskLog' id = db.Column(db.Integer, primary_key=True) taskId = db.Column(db.Integer, unique=False) startTime = db.Column(db.DateTime, unique=False) endTime = db.Column(db.DateTime, unique=False) status = db.Column(db.Integer, unique=False) code = db.Column(db.Integer, unique=False) result = db.Column(db.String(1000), unique=False)
class Task(db.Model): __tablename__ = 'task' id = db.Column(db.Integer, primary_key=True) corn = db.Column(db.String(40), unique=False) name = db.Column(db.String(300), unique=True) desc = db.Column(db.String(500), unique=False) created = db.Column(db.DateTime, default=datetime.now) url = db.Column(db.String(300), unique=False) method = db.Column(db.String(4), unique=False) params = db.Column(db.String(300), unique=False) headers = db.Column(db.String(300), unique=False) success = db.Column(db.String(300), unique=False) dependId = db.Column(db.Integer, unique=False)
class User(db.Model): id = db.Column(db.INTEGER, primary_key=True) user_code = db.Column(db.String(100)) # 根据生成密码的规则,password代表的是密码hash,并非明文密码 password = db.Column(db.String(100)) @property # property包装器表明将函数直接当作属性使用 # 必须如此声明,此可以使用_password包装器 # 以及使用setter和getter方法 def _password(self): return self.password @_password.setter def _password(self, password): self.password = generate_password_hash(password) # self.save() def check_password(self, password): return check_password_hash(self.password, password) @classmethod def create(cls, **kwargs): try: obj = cls(user_code=kwargs.get('user_code')) if kwargs.get('password'): obj.password = kwargs.get('password') else: # 使用=,调用的是_password.setter方法 obj._password = '******' db.session.add(obj) db.session.commit() return obj except Exception as e: logging.debug(e)
class Author(BaseModel): """ Author database model. Used for storing author profile and personal details. """ __tablename__ = 'author' id = db.Column(db.Integer, primary_key=True, autoincrement=True) first_name = db.Column(db.String(255)) last_name = db.Column(db.String(255)) avatar = db.Column(db.String(255), nullable=True) books = db.relationship('Book', secondary=books, lazy='subquery', backref=db.backref('authors', lazy=True)) birthday = db.Column(db.Date, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) @property def name(self): """ Author full name """ first_name = f'{self.first_name} ' if self.first_name is not None else '' last_name = self.last_name if self.last_name is not None else '' return f'{first_name}{last_name}'.strip() def __repr__(self): """ Author representation """ return self.name
class Book(BaseModel): """ Book database model. Used for storing authors books details. """ __tablename__ = 'book' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(255)) year = db.Column(db.Integer) def __repr__(self): """ Book representation """ return f"<Book {self.title}>"
class Image(BaseModel, ModelMixin): """用户上传图片""" __tablename__ = 'image' user_id = db.Column(db.Integer, db.ForeignKey('user.id'), comment='上传的用户', default=None) default_show = db.Column(db.String(200), comment='默认上传显示的地址') raw = db.Column(db.Text)
class User(db.Model): id = db.Column(db.Integer) user = db.Column(db.String(24))
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), unique=True) password_hash = db.Column(db.String(128)) email = db.Column(db.String(64), unique=True) confirmed = db.Column(db.Boolean, default=False) # 头像 icon = db.Column(db.String(64), default='default.jpg') # 在关联模型中添加反向引用 # 参数1:关联的模型 # backref:反向引用字段名 # lazy:加载时机,dynamic不加载数据,但是提供数据的查询 # 一对一关系添加:uselist = False posts = db.relationship('Posts', backref='user', lazy='dynamic') # 添加收藏功能 favorites = db.relationship('Posts', secondary='collections', backref=db.backref('users', lazy='dynamic'), lazy='dynamic') # 保护密码属性 @property def password(self): raise AttributeError('密码是不可读属性') # 设置密码时,保存加密后的hash值 @password.setter def password(self, password): self.password_hash = generate_password_hash(password) # 密码校验,正确返回True,错误返回False def verify_password(self, password): return check_password_hash(self.password_hash, password) # 生成账户激活的token def generate_activate_token(self, expires_in=3600): s = Serializer(current_app.config['SECRET_KEY'], expires_in=expires_in) return s.dumps({'id': self.id}) # 校验账户激活的token @staticmethod def check_activate_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return False u = User.query.get(data.get('id')) if not u: # flash('用户不存在') return False if not u.confirmed: # 账户没有激活 u.confirmed = True db.session.add(u) return True # 判断该用户是否收藏了指定的博客 #后边调用的时候 #通过当前登录用户.方法 此刻应该在模型中写 def is_favorite(self, pid): # 获取所有收藏的博客 favorites = self.favorites.all() posts = list(filter(lambda p: p.id == pid, favorites)) if len(posts) > 0: return True return False # 添加收藏 def add_favorite(self, pid): p = Posts.query.get(pid) #根据id 获取帖子对象 self.favorites.append(p) db.session.commit() # 取消收藏 def del_favorite(self, pid): p = Posts.query.get(pid) self.favorites.remove(p) db.session.commit()