####################first region#######################
from werkzeug.security import generate_password_hash,check_password_hash
pw1 = generate_password_hash('tony')
pw2 = generate_password_hash('tony')
print pw1
print pw2
print check_password_hash(pw1,'tony')
print check_password_hash(pw2,'tony')
####################second region####################
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine,Column,Integer,String,Sequence
from sqlalchemy import and_,or_
from sqlalchemy.orm import sessionmaker
DB_URI = 'XXX-XXX-XXX'
eng = create_engine(DB_URI)
Base = declarative_base()
class User(Base):
__tablename__ = 'hash_users'
id = Column(Integer,primary_key=True)
name = Column(String(128),nullable=False)
_password = Column(String(256),nullable=False)
def __init__(self,name,password):
self.name = name
self._password = password
#hybrid_property装饰器把password变成了一个混合属性,可以通过user.password属性来访问哈希的密码,也会在给user.password
#赋值的时候触发password.setter
@hybrid_property
def password(self):
return self._password
@password.setter
def _set_password(self,plaintext):
self._password = generate_password_hash(plaintext)
def verify_password(self,password):
return check_password_hash(self._password,password)