Skip to content

Commit

Permalink
[RFR] base_rest: don't add fake modules to the registry
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRijnhart committed Dec 7, 2022
1 parent b320af1 commit 865be2e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
19 changes: 3 additions & 16 deletions base_rest/models/rest_service_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,16 @@ def _build_controller(self, service, controller_def):

# generate an addon name used to register our new controller for
# the current database
addon_name = "{}_{}_{}".format(
addon_name = base_controller_cls._module
identifier = "{}_{}_{}".format(
self.env.cr.dbname,
service._collection.replace(".", "_"),
service._usage.replace(".", "_"),
)
base_controller_cls._identifier = identifier
# put our new controller into the new addon module
ctrl_cls.__module__ = "odoo.addons.{}".format(addon_name)

# instruct the registry that our fake addon is part of the loaded
# modules
# TODO: causes
# Traceback (most recent call last):
# File "/home/odoo/odoo/service/server.py", line 1310, in preload_registries
# post_install_suite = loader.make_suite(module_names, 'post_install')
# File "/home/odoo/odoo/tests/loader.py", line 58, in make_suite
# return OdooSuite(sorted(tests, key=lambda t: t.test_sequence))
# File "/home/odoo/odoo/tests/loader.py", line 54, in <genexpr>
# for m in get_test_modules(module_name)
# File "/home/odoo/odoo/tests/loader.py", line 22, in get_test_modules
# results = _get_tests_modules(importlib.util.find_spec(f'odoo.addons.{module}'))
# File "/home/odoo/odoo/tests/loader.py", line 31, in _get_tests_modules
# spec = importlib.util.find_spec('.tests', mod.name)
# AttributeError: 'NoneType' object has no attribute 'name'
self.env.registry._init_modules.add(addon_name)

# register our conroller into the list of available controllers
Expand Down
18 changes: 8 additions & 10 deletions base_rest/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class RestServiceRegistryCase(ComponentRegistryCase):
# pylint: disable=W8106
@staticmethod
def _setup_registry(class_or_instance):
class_or_instance._registry_init_modules = set(
class_or_instance.env.registry._init_modules
)
ComponentRegistryCase._setup_registry(class_or_instance)

class_or_instance._service_registry = RestServicesRegistry()
Expand Down Expand Up @@ -143,9 +140,6 @@ def my_controller_route_without_auth_2(self):

@staticmethod
def _teardown_registry(class_or_instance):
class_or_instance.env.registry._init_modules = (
class_or_instance._registry_init_modules
)
ComponentRegistryCase._teardown_registry(class_or_instance)
http.Controller.children_classes = (
class_or_instance._controller_children_classes
Expand Down Expand Up @@ -174,16 +168,20 @@ def _build_services(class_or_instance, *classes):
)

@staticmethod
def _get_controller_for(service):
addon_name = "{}_{}_{}".format(
def _get_controller_for(service, addon="base_rest"):
identifier = "{}_{}_{}".format(
get_db_name(),
service._collection.replace(".", "_"),
service._usage.replace(".", "_"),
)
controllers = http.Controller.children_classes.get(addon_name, [])
controllers = [
controller
for controller in http.Controller.children_classes.get(addon, [])
if getattr(controller, "_identifier", None) == identifier
]
if not controllers:
return
return controllers[0]
return controllers[-1]

@staticmethod
def _get_controller_route_methods(controller):
Expand Down
1 change: 0 additions & 1 deletion base_rest_demo/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def test_controller_registry(self):
# at the end of the start process, our tow controllers must into the
# controller registered
controllers = Controller.children_classes.get("base_rest_demo", [])
self.assertEqual(len(controllers), 4)

self.assertIn(
BaseRestDemoPrivateApiController,
Expand Down

0 comments on commit 865be2e

Please sign in to comment.