import hashlib

from flask_login import UserMixin
from App.extens import login_manager

from App.extens import db

#user模型应该继承UserMixin
class User(db.Model,UserMixin):
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    username = db.Column(db.String(30),unique=True,nullable=False)
    password_hash = db.Column(db.String(128),nullable=False)
    gender = db.Column(db.Integer,default=0)

    __tablename__ = "user"

    @staticmethod
    def check_user(username,password):
        password = hashlib.sha1(password.encode("utf8")).hexdigest()
        res = User.query.filter(User.username==username,User.password_hash==password).first()
        return res

#回调函数
@login_manager.user_loader
def load_user(uid):
    return User.query.get(uid)