class UserReister(UserMixin, db.Model): __tablename__ = 'UserRgeisters' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), unique=True, nullable=False) email = db.Column(db.String(50), unique=True, nullable=False) password_hash = db.Column(db.String(50), nullable=False) @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode( 'utf8') def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) def __repr__(self): return 'username:%s, email:%s' % (self.username, self.email) @login.user_loader def load_user(user_id): return UserReister.query.get(int(user_id))
class UserReister(db.Model): # 如果沒有設置__tablename__的話會依class的名稱產生table name __tablename__ = 'UserRgeisters' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), unique=True, nullable=False) email = db.Column(db.String(50), unique=True, nullable=False) password_hash = db.Column(db.String(50), nullable=False) @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = Bcrypt.generate_password_hash(password).decode('utf8') def check_password(self, password): """ 密碼驗證,驗證使用者輸入的密碼跟資料庫內的加密密碼是否相符 :param password: 使用者輸入的密碼 :return: True/False """ return bcrypt.check_password_hash(self.password_hash, password) def __repr__(self): return 'username:%s, email:%s' % (self.username, self.email)
class UserReister(db.Model): # 如果沒有設置__tablename__的話會依class的名稱產生table name __tablename__ = 'UserRgeisters' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), unique=True, nullable=False) email = db.Column(db.String(50), unique=True, nullable=False) password = db.Column(db.String(50), nullable=False) def __repr__(self): return 'username:%s, email:%s' % (self.username, self.email)
class UserRegister(db.Model): # 如果沒有設置__tablename__的話會依class的名稱產生table name __tablename__ = 'UserRgeisters' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), unique=True, nullable=False) email = db.Column(db.String(50), unique=True, nullable=False) password_hash = db.Column(db.String(50), nullable=False) confirm = db.Column(db.Boolean, default=False) @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = Bcrypt.generate_password_hash(password).decode('utf8') def check_password(self, password): """ 密碼驗證,驗證使用者輸入的密碼跟資料庫內的加密密碼是否相符 :param password: 使用者輸入的密碼 :return: True/False """ return bcrypt.check_password_hash(self.password_hash, password) def __repr__(self): return 'username:%s, email:%s' % (self.username, self.email) def create_confirm_token(self, expires_in=3600): """ 利用itsdangerous來生成令牌,透過current_app來取得目前flask參數['SECRET_KEY']的值 :param expiration: 有效時間,單位為秒 :return: 回傳令牌,參數為該註冊用戶的id """ s = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY'], expires_in=expires_in) return s.dumps({'user_id': self.id}) def validate_confirm_token(self, token): """ 驗證回傳令牌是否正確,若正確則回傳True :param token:驗證令牌 :return:回傳驗證是否正確,正確為True """ s = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) # 驗證 except SignatureExpired: # 當時間超過的時候就會引發SignatureExpired錯誤 return False except BadSignature: # 當驗證錯誤的時候就會引發BadSignature錯誤 return False return data
class Userlog(db.Model): __tablename__ = 'Userlogs' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), nullable=False) time = db.Column(db.DateTime(timezone=True), nullable=False) state = db.Column(db.String(30), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('UserRgeisters.id')) def __repr__(self): return 'username:%s, time:%s , state:%s' % \ (self.username, self.time , self.state)
class UserMessage(db.Model): __tablename__ = 'UserMessage' id = db.Column(db.Integer, primary_key=True) user_A = db.Column(db.Integer, db.ForeignKey('UserRgeisters.id')) user_B = db.Column(db.Integer, db.ForeignKey('UserRgeisters.id')) message = db.Column(db.String(250), nullable=False) time = db.Column(db.DateTime(timezone=True), nullable=False) def __repr__(self): return 'user_A:%s, user_B:%s , messagepython:%s , time:%s' % \ (self.user_A, self.user_B, self.message , self.time)
class UserRelation(db.Model): __tablename__ = 'UserRelation' id = db.Column(db.Integer, primary_key=True) user_A = db.Column(db.Integer, db.ForeignKey('UserRgeisters.id')) user_B = db.Column(db.Integer, db.ForeignKey('UserRgeisters.id')) relation = db.Column(db.String(30), nullable=False) time = db.Column(db.DateTime(timezone=True), nullable=False) def __repr__(self): return 'user_A:%s, user_B:%s , relation:%s , time:%s' % \ (self.user_A, self.user_B, self.relation , self.time)