from datetime import datetime from flask_security import UserMixin, RoleMixin from flask_security.utils import hash_password from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() users_roles = db.Table( 'users_roles', db.Column('user_id', db.Integer(), db.ForeignKey('users.id')), db.Column('role_id', db.Integer(), db.ForeignKey('roles.id'))) class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer(), primary_key=True) #: The username of the user. This is also the display name and thus is immutable username = db.Column(db.String(length=50), unique=True, nullable=False) #: The email address of the user email = db.Column(db.String(length=255), unique=True, nullable=True) #: The (hashed) password of the user password = db.Column(db.String(length=255)) #: A flag to show whether the user is enabled (active) or not active = db.Column(db.Boolean(), default=False) #: The timestamp when this user was created created_at = db.Column(db.DateTime(), default=datetime.utcnow) #: The timestamp when the user was activated confirmed_at = db.Column(db.DateTime()) #: The roles of the user roles = db.relationship('Role', secondary=users_roles, backref=db.backref('users', lazy='dynamic')) def __repr__(self): return f'' class Role(db.Model, RoleMixin): __tablename__ = 'roles' id = db.Column(db.Integer(), primary_key=True) #: The name of the role name = db.Column(db.Unicode(length=80), unique=True) #: A description of the role description = db.Column(db.UnicodeText) def __repr__(self): return f''