-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
28 lines (23 loc) · 905 Bytes
/
user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from google.appengine.ext import ndb
from password import make_password_hash, valid_password
class User(ndb.Model):
"""Models blog user"""
name = ndb.StringProperty(required=True)
password_hash = ndb.StringProperty(required=True)
salt = ndb.StringProperty(required=True)
email = ndb.StringProperty()
@classmethod
def by_name(cls, name):
return cls.query(User.name == name).get()
@classmethod
def register(cls, name, password, email=None):
password_hash, salt = make_password_hash(name, password)
return User(name=name,
password_hash=password_hash, salt=salt,
email=email)
@classmethod
def login(cls, name, password):
user = cls.by_name(name)
if user and valid_password(name, password, user.password_hash,
user.salt):
return user