|
|
|
@ -8,47 +8,34 @@ from {{cookiecutter.app_name}}.database import ( |
|
|
|
|
CRUDMixin, |
|
|
|
|
ReferenceCol, |
|
|
|
|
relationship, |
|
|
|
|
Column, |
|
|
|
|
BcryptType, |
|
|
|
|
) |
|
|
|
|
from {{cookiecutter.app_name}}.extensions import bcrypt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Role(CRUDMixin, db.Model): |
|
|
|
|
__tablename__ = 'roles' |
|
|
|
|
name = db.Column(db.String(80), unique=True, nullable=False) |
|
|
|
|
name = Column(db.String(80), unique=True, nullable=False) |
|
|
|
|
user_id = ReferenceCol('users', nullable=True) |
|
|
|
|
user = relationship('User', backref='roles') |
|
|
|
|
|
|
|
|
|
class User(UserMixin, CRUDMixin, db.Model): |
|
|
|
|
|
|
|
|
|
__tablename__ = 'users' |
|
|
|
|
username = db.Column(db.String(80), unique=True, nullable=False) |
|
|
|
|
email = db.Column(db.String(80), unique=True, nullable=False) |
|
|
|
|
password = db.Column(db.String, nullable=False) # The hashed password |
|
|
|
|
created_at = db.Column(db.DateTime(), nullable=False) |
|
|
|
|
first_name = db.Column(db.String(30), nullable=True) |
|
|
|
|
last_name = db.Column(db.String(30), nullable=True) |
|
|
|
|
active = db.Column(db.Boolean()) |
|
|
|
|
is_admin = db.Column(db.Boolean()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, username=None, email=None, password=None, |
|
|
|
|
first_name=None, last_name=None, |
|
|
|
|
active=False, is_admin=False): |
|
|
|
|
self.username = username |
|
|
|
|
self.email = email |
|
|
|
|
if password: |
|
|
|
|
self.set_password(password) |
|
|
|
|
self.active = active |
|
|
|
|
self.is_admin = is_admin |
|
|
|
|
self.created_at = dt.datetime.utcnow() |
|
|
|
|
self.first_name = first_name |
|
|
|
|
self.last_name = last_name |
|
|
|
|
|
|
|
|
|
def set_password(self, password): |
|
|
|
|
self.password = bcrypt.generate_password_hash(password) |
|
|
|
|
|
|
|
|
|
def check_password(self, password): |
|
|
|
|
return bcrypt.check_password_hash(self.password, password) |
|
|
|
|
username = Column(db.String(80), unique=True, nullable=False) |
|
|
|
|
email = Column(db.String(80), unique=True, nullable=False) |
|
|
|
|
password = Column(BcryptType, nullable=True) |
|
|
|
|
created_at = Column(db.DateTime, nullable=False) |
|
|
|
|
first_name = Column(db.String(30), nullable=True) |
|
|
|
|
last_name = Column(db.String(30), nullable=True) |
|
|
|
|
active = Column(db.Boolean(), default=False) |
|
|
|
|
is_admin = Column(db.Boolean(), default=False) |
|
|
|
|
|
|
|
|
|
def __init__(self, username, email, **kwargs): |
|
|
|
|
db.Model.__init__(self, username=username, email=email, **kwargs) |
|
|
|
|
if not self.created_at: |
|
|
|
|
self.created_at = dt.datetime.utcnow() |
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
def full_name(self): |
|
|
|
|