From 23a93026aa58e82c06d4fe0b27953a79d0c2eb04 Mon Sep 17 00:00:00 2001 From: maxime Date: Thu, 23 Jun 2022 11:03:52 +0200 Subject: [PATCH] [halfdomain] fix feature HALFAPI_DOMAIN_MODULE for acls route --- halfapi/half_domain.py | 34 ++++++++++++++++++++++++---------- halfapi/halfapi.py | 3 ++- halfapi/testing/test_domain.py | 5 ++++- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/halfapi/half_domain.py b/halfapi/half_domain.py index 30b69f3..85309be 100644 --- a/halfapi/half_domain.py +++ b/halfapi/half_domain.py @@ -93,34 +93,48 @@ class HalfDomain(Starlette): def m_acl(module, acl=None): """ Returns the imported acl module for the domain module """ - if (not acl): + if not acl: acl = getattr(module, '__acl__', '.acl') return importlib.import_module(acl, module.__package__) @staticmethod - def acls(domain, module=None, acl=None): + def acls(module, acl=None): """ Returns the ACLS constant for the given domain """ - if not module: - module = importlib.import_module(domain) - m_acl = HalfDomain.m_acl(module, acl) try: return getattr(m_acl, 'ACLS') 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 - 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 = {} - 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) - 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) d_res[acl_name] = { 'callable': fct, diff --git a/halfapi/halfapi.py b/halfapi/halfapi.py index 149e5af..906ed0b 100644 --- a/halfapi/halfapi.py +++ b/halfapi/halfapi.py @@ -213,10 +213,11 @@ class HalfAPI(Starlette): sys.exit(0) def acls_route(self): + module = None res = { domain: HalfDomain.acls_route( domain, - module=domain_conf.get('module'), + module_path=domain_conf.get('module'), acl=domain_conf.get('acl')) for domain, domain_conf in self.config.get('domain', {}).items() if isinstance(domain_conf, dict) and domain_conf.get('enabled', False) diff --git a/halfapi/testing/test_domain.py b/halfapi/testing/test_domain.py index 3ff1100..33fc3ff 100644 --- a/halfapi/testing/test_domain.py +++ b/halfapi/testing/test_domain.py @@ -64,6 +64,9 @@ class TestDomain(TestCase): self.client = TestClient(self.halfapi.application) + self.module = importlib.import_module( + getattr(self, 'MODULE', self.DOMAIN) + ) def tearDown(self): @@ -114,7 +117,7 @@ class TestDomain(TestCase): 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]) for acl_name in ACLS: