[halfdomain] fix feature HALFAPI_DOMAIN_MODULE for acls route

This commit is contained in:
maxime 2022-06-23 11:03:52 +02:00
parent 05cf37c775
commit 23a93026aa
3 changed files with 30 additions and 12 deletions

View File

@ -93,34 +93,48 @@ class HalfDomain(Starlette):
def m_acl(module, acl=None): def m_acl(module, acl=None):
""" Returns the imported acl module for the domain module """ Returns the imported acl module for the domain module
""" """
if (not acl): if not acl:
acl = getattr(module, '__acl__', '.acl') acl = getattr(module, '__acl__', '.acl')
return importlib.import_module(acl, module.__package__) return importlib.import_module(acl, module.__package__)
@staticmethod @staticmethod
def acls(domain, module=None, acl=None): def acls(module, acl=None):
""" Returns the ACLS constant for the given domain """ Returns the ACLS constant for the given domain
""" """
if not module:
module = importlib.import_module(domain)
m_acl = HalfDomain.m_acl(module, acl) m_acl = HalfDomain.m_acl(module, acl)
try: try:
return getattr(m_acl, 'ACLS') return getattr(m_acl, 'ACLS')
except AttributeError: except AttributeError:
raise Exception(f'Missing acl.ACLS constant in {domain} module') raise Exception(f'Missing acl.ACLS constant in module {m_acl.__package__}')
@staticmethod @staticmethod
def acls_route(domain, module=None, acl=None): def acls_route(domain, module_path=None, acl=None):
""" Dictionary of acls
Format :
{
[acl_name]: {
callable: fct_reference,
docs: fct_docstring,
result: fct_result
}
}
"""
d_res = {} d_res = {}
if module is None:
module = importlib.import_module(domain) module = importlib.import_module(domain) \
if module_path is None \
else importlib.import_module(module_path)
m_acl = HalfDomain.m_acl(module, acl) m_acl = HalfDomain.m_acl(module, acl)
for acl_name, doc, order in HalfDomain.acls(domain, acl=acl): for acl_name, doc, order in HalfDomain.acls(
module,
acl=acl):
fct = getattr(m_acl, acl_name) fct = getattr(m_acl, acl_name)
d_res[acl_name] = { d_res[acl_name] = {
'callable': fct, 'callable': fct,

View File

@ -213,10 +213,11 @@ class HalfAPI(Starlette):
sys.exit(0) sys.exit(0)
def acls_route(self): def acls_route(self):
module = None
res = { res = {
domain: HalfDomain.acls_route( domain: HalfDomain.acls_route(
domain, domain,
module=domain_conf.get('module'), module_path=domain_conf.get('module'),
acl=domain_conf.get('acl')) acl=domain_conf.get('acl'))
for domain, domain_conf in self.config.get('domain', {}).items() for domain, domain_conf in self.config.get('domain', {}).items()
if isinstance(domain_conf, dict) and domain_conf.get('enabled', False) if isinstance(domain_conf, dict) and domain_conf.get('enabled', False)

View File

@ -64,6 +64,9 @@ class TestDomain(TestCase):
self.client = TestClient(self.halfapi.application) self.client = TestClient(self.halfapi.application)
self.module = importlib.import_module(
getattr(self, 'MODULE', self.DOMAIN)
)
def tearDown(self): def tearDown(self):
@ -114,7 +117,7 @@ class TestDomain(TestCase):
assert self.DOMAIN in d_r.keys() assert self.DOMAIN in d_r.keys()
ACLS = HalfDomain.acls(self.DOMAIN) ACLS = HalfDomain.acls(self.module, self.ACL)
assert len(ACLS) == len(d_r[self.DOMAIN]) assert len(ACLS) == len(d_r[self.DOMAIN])
for acl_name in ACLS: for acl_name in ACLS: