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