Exemple #1
0
class Role(db.Model):
    """
    角色表
    """
    __tablename__ = 'role'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(10))

    # 多对多的双向关联,用于跨表查询
    permissions = db.relationship('Permission', secondary=permission_role, passive_deletes=True)
    users = db.relationship('User', secondary=user_role, passive_deletes=True)

    def __str__(self):
        return '<Role %s>' % self.name
Exemple #2
0
class Role(TimeStamp, db.Model, SerializerMixin):
    __tablename__ = 'role'

    serialize_only = ('role_name', 'description', 'id', 'is_active')

    id = db.Column(db.Integer, primary_key=True)
    role_name = db.Column(db.String(150), nullable=True)
    description = db.Column(db.Text, nullable=True)
    is_active = db.Column(db.SmallInteger, default=1)

    users = db.relationship("User",
                            secondary="user_roles",
                            back_populates="roles")

    def __repr__(self):
        return '<Role %r>' % (self.role_name)

    @classmethod
    def find_by_id(cls, id):
        return cls.query.filter_by(id=id).first()

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    def to_json(self):
        return {
            'role_name': self.role_name,
            'description': self.description,
            'id': self.id,
            'is_active': self.is_active
        }
Exemple #3
0
class RolesPermissions(BaseModel, db.Model):
    """
    desc:   角色权限关联表

    """
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    role_id = db.Column(db.Integer, db.ForeignKey("role.id"))
    role = db.relationship("Role",
                           backref=db.backref("roles_permissions",
                                              lazy="dynamic"))
    permission_id = db.Column(db.Integer, db.ForeignKey("permission.id"))
    permission = db.relationship("Permission",
                                 backref=db.backref("roles_permissions",
                                                    lazy="dynamic"))

    def __init__(self, role, permission):
        self.role = role
        self.permission = permission
Exemple #4
0
class UsersRoles(BaseModel, db.Model):
    """
    desc:   用户角色关联表

    """
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User",
                           backref=db.backref("user_roles",
                                              lazy="dynamic",
                                              cascade="all, delete-orphan"))
    role_id = db.Column(db.Integer, db.ForeignKey("role.id"))
    role = db.relationship("Role",
                           backref=db.backref("role_users",
                                              lazy="dynamic",
                                              cascade="all, delete-orphan"))

    def __init__(self, user, role):
        self.user = user
        self.role = role
Exemple #5
0
class Permission(db.Model):
    """
    权限表
    """
    __tablename__ = 'permission'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(10))
    url = db.Column(db.String(64))
    menu_ref = db.Column(db.Integer, db.ForeignKey('permission.id'))  # 自关联
    code = db.Column(db.String(10))
    group_id = db.Column(db.Integer, db.ForeignKey('permissiongroup.id'))

    # 自关联声明
    self_ref = db.relationship('Permission', remote_side=[id])

    # 多对多的双向关联,用于跨表查询
    roles = db.relationship('Role', secondary=permission_role, passive_deletes=True)

    def __str__(self):
        return '<Permission> %s' % self.title
Exemple #6
0
class Menu(db.Model):
    """
    菜单表
    """
    __tablename__ = 'menu'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(10))

    # 双向关联,用于跨表查询
    perm_groups = db.relationship('PermissionGroup', backref='menu')

    def __str__(self):
        return '<Menu %s>' % self.title
Exemple #7
0
class PermissionGroup(db.Model):
    """
    权限组表
    """
    __tablename__ = 'permissiongroup'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(10))
    menu_id = db.Column(db.Integer, db.ForeignKey('menu.id'))

    # 双向关联,用于跨表查询
    permissions = db.relationship('Permission', backref='perm_group')

    def __str__(self):
        return '<PermissionGroup %s>' % self.title
Exemple #8
0
class User(TimeStamp, db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True, nullable=False)
    first_name = db.Column(db.String(255), unique=False, nullable=True)
    last_name = db.Column(db.String(255), unique=False, nullable=True)
    password = db.Column(db.Text, unique=False, nullable=False)
    user_type = db.Column(db.SmallInteger, default=1)
    access_token = db.Column(db.Text, nullable=True)

    roles = db.relationship("Role",
                            secondary="user_roles",
                            back_populates="users")

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    def to_json(self):
        return {
            'first_name': self.first_name,
            'last_name': self.last_name,
            'email': self.email,
            'id': self.id,
            'access_token': self.access_token,
            'user_type': self.user_type
        }

    @classmethod
    def find_by_email(cls, email):
        return cls.query.filter_by(email=email).first()

    @classmethod
    def find_by_id(cls, id):
        return cls.query.filter_by(id=id).first()

    @staticmethod
    def generate_hash(password):
        return sha256.hash(password)

    @staticmethod
    def verify_hash(password, hash):
        return sha256.verify(password, hash)

    def __repr__(self):
        return '<User %r>' % (self.first_name)
Exemple #9
0
class User(db.Model):
    """
    用户信息表
    """
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(16))
    password = db.Column(db.String(100))
    create_time = db.Column(db.DateTime, default=datetime.datetime.now)

    # 多对多的双向关联,用于跨表查询
    roles = db.relationship('Role', secondary=user_role, passive_deletes=True)

    def check_password(self, password):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.password, password)

    def __str__(self):
        return '<User %s>' % self.username