|
|
|
@ -1,6 +1,8 @@ |
|
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
from passlib.apps import custom_app_context as pwd_context |
|
|
|
|
import datetime as dt |
|
|
|
|
|
|
|
|
|
from {{cookiecutter.repo_name}}.database import db |
|
|
|
|
from {{cookiecutter.repo_name}}.extensions import bcrypt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class User(db.Model): |
|
|
|
@ -10,18 +12,19 @@ class User(db.Model): |
|
|
|
|
username = db.Column(db.String(80), unique=True, nullable=False) |
|
|
|
|
email = db.Column(db.String(80), unique=True, nullable=False) |
|
|
|
|
password_hash = db.Column(db.String, nullable=False) |
|
|
|
|
created_at = db.Column(db.DateTime(), nullable=False) |
|
|
|
|
|
|
|
|
|
def __init__(self, username=None, email=None, password=None): |
|
|
|
|
def __init__(self, username, email, password): |
|
|
|
|
self.username = username |
|
|
|
|
self.email = email |
|
|
|
|
if password: |
|
|
|
|
self.set_password(password) |
|
|
|
|
self.set_password(password) |
|
|
|
|
self.created_at = dt.datetime.utcnow() |
|
|
|
|
|
|
|
|
|
def set_password(self, password): |
|
|
|
|
self.password_hash = pwd_context.encrypt(password) |
|
|
|
|
self.password_hash = bcrypt.generate_password_hash(password) |
|
|
|
|
|
|
|
|
|
def check_password(self, password): |
|
|
|
|
return pwd_context.verify(password, self.password_hash) |
|
|
|
|
return bcrypt.check_password_hash(self.password_hash, password) |
|
|
|
|
|
|
|
|
|
def __repr__(self): |
|
|
|
|
return '<User "{username}">'.format(username=self.username) |
|
|
|
|