RoleMixin __init__ hasattr fails
I have a class Role as follows (following your example in the docs):
class Role(Base, RoleMixin):
id = Column(Integer, primary_key=True)
name = Column(String(20))
parents = relationship(
'Role',
secondary=roles_parents,
primaryjoin=(id == roles_parents.c.role_id),
secondaryjoin=(id == roles_parents.c.parent_id),
backref=backref('children', lazy='joined'),
lazy='joined'
)
def __init__(self, name):
RoleMixin.__init__(self)
self.name = name
...
However in Python 3.6.3 with flask-rbac 0.2.1 I have some problems.
$ pip show flask-rbac
Name: Flask-RBAC
Version: 0.2.1
Summary: RBAC support for Flask
Home-page: https://github.com/shonenada/flask-rbac
Author: Yaoda Liu
Author-email: [email protected]
License: UNKNOWN
Location: c:\program files (x86)\python\lib\site-packages
Requires: Flask
Required-by:
I can narrow it down to the RoleMixin.init:
def __init__(self, name=None):
self.name = name
if not hasattr(self.__class__, 'parents'):
self.parents = set()
if not hasattr(self.__class__, 'children'):
self.children = set()
RoleMixin.roles[name] = self
hasattr(self.class, 'parents') returns False hasattr(self.class, 'children') returns False
I would expect the Role class to have a parent attribute and therefor this method to return False. In my case, self.parents and self.children get set to set() which messes up the rest of the method calls when checking for an access control decision.
I believe children and parents should be set by my class. Parents are already set to the relationship and children are set via SQLAlchemy via a backref. This is probably done dynamically and might explain why hasattr(self.class, 'children') returns False.
It is unclear to me why hasattr(self.class, 'parents') returns False in my case.