diff options
| author | Steve Piercy <web@stevepiercy.com> | 2016-04-09 02:52:51 -0700 |
|---|---|---|
| committer | Steve Piercy <web@stevepiercy.com> | 2016-04-09 02:53:04 -0700 |
| commit | 9ff53561ce49c29d48181901d489fe3ea83e2042 (patch) | |
| tree | 089e906dd3d885f7f3433152629ea43cce81f2ad /pyramidtut/tutorial | |
| parent | 7266d9eebfc86e35b0c7756f5b105436d3da40e6 (diff) | |
| download | pyramid-9ff53561ce49c29d48181901d489fe3ea83e2042.tar.gz pyramid-9ff53561ce49c29d48181901d489fe3ea83e2042.tar.bz2 pyramid-9ff53561ce49c29d48181901d489fe3ea83e2042.zip | |
- update definingmodels step
Diffstat (limited to 'pyramidtut/tutorial')
| -rw-r--r-- | pyramidtut/tutorial/tutorial/models/user.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/pyramidtut/tutorial/tutorial/models/user.py b/pyramidtut/tutorial/tutorial/models/user.py new file mode 100644 index 000000000..a835deb01 --- /dev/null +++ b/pyramidtut/tutorial/tutorial/models/user.py @@ -0,0 +1,29 @@ +import bcrypt +from sqlalchemy import ( + Column, + Integer, + Text, +) + +from tutorial.tutorial.models.meta import Base + + +class User(Base): + """ The SQLAlchemy declarative model class for a User object. """ + __tablename__ = 'users' + id = Column(Integer, primary_key=True) + name = Column(Text, nullable=False, unique=True) + role = Column(Text, nullable=False) + + password_hash = Column(Text) + + def set_password(self, pw): + pwhash = bcrypt.hashpw(pw.encode('utf8'), bcrypt.gensalt()) + self.password_hash = pwhash + + def check_password(self, pw): + if self.password_hash is not None: + expected_hash = self.password_hash + actual_hash = bcrypt.hashpw(pw.encode('utf8'), expected_hash) + return expected_hash == actual_hash + return False |
