Skip to content

Commit

Permalink
Merge PR #487 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by simahawk
  • Loading branch information
OCA-git-bot committed Sep 3, 2024
2 parents 24795fb + 491a548 commit 6e178cb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
13 changes: 8 additions & 5 deletions component/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from odoo import models
from odoo.tools import LastOrderedSet, OrderedSet

from .exception import NoComponentError, SeveralComponentError
from .exception import NoComponentError, RegistryNotReadyError, SeveralComponentError

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -252,14 +252,17 @@ def __init__(
dbname = self.env.cr.dbname
try:
self.components_registry = _component_databases[dbname]
except KeyError:
_logger.error(
except KeyError as exc:
msg = (
"No component registry for database %s. "
"Probably because the Odoo registry has not been built "
"yet.",
"yet."
)
_logger.error(
msg,
dbname,
)
raise
raise RegistryNotReadyError(msg) from exc
self._propagate_kwargs = ["collection", "model_name", "components_registry"]
for attr_name, value in kwargs.items():
setattr(self, attr_name, value)
Expand Down
4 changes: 4 additions & 0 deletions component/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class NoComponentError(ComponentException):

class SeveralComponentError(ComponentException):
"""More than one component have been found"""


class RegistryNotReadyError(ComponentException):
"""Component registry not ready yet for given DB."""
1 change: 1 addition & 0 deletions component/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Guewen Baconnier <[email protected]>
* Laurent Mignon <[email protected]>
* Simone Orsi <[email protected]>
1 change: 1 addition & 0 deletions component/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import test_component
from . import test_lookup
from . import test_work_on
from . import test_utils
20 changes: 20 additions & 0 deletions component/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2023 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

from unittest import mock

from odoo.addons.component.utils import is_component_registry_ready

from .common import TransactionComponentRegistryCase


class TestUtils(TransactionComponentRegistryCase):
def test_registry_ready(self):
path = "odoo.addons.component.utils.get_component_registry"
with mock.patch(path) as mocked:
mocked.return_value = None
self.assertFalse(is_component_registry_ready(self.env.cr.dbname))
self._setup_registry(self)
mocked.return_value = self.comp_registry
self.assertTrue(is_component_registry_ready(self.env.cr.dbname))
self._teardown_registry(self)
14 changes: 14 additions & 0 deletions component/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

from .core import _component_databases


def get_component_registry(dbname):
return _component_databases.get(dbname)


def is_component_registry_ready(dbname):
"""Return True if the registry is ready to be used."""
comp_registry = get_component_registry(dbname)
return comp_registry.ready if comp_registry else False

0 comments on commit 6e178cb

Please sign in to comment.