Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][BKP] component: misc improvements #487

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
dbname = self.env.cr.dbname
try:
self.components_registry = _component_databases[dbname]
except KeyError:
_logger.error(
except KeyError as exc:
msg = (

Check warning on line 256 in component/core.py

View check run for this annotation

Codecov / codecov/patch

component/core.py#L255-L256

Added lines #L255 - L256 were not covered by tests
"No component registry for database %s. "
"Probably because the Odoo registry has not been built "
"yet.",
"yet."
)
_logger.error(

Check warning on line 261 in component/core.py

View check run for this annotation

Codecov / codecov/patch

component/core.py#L261

Added line #L261 was not covered by tests
msg,
dbname,
)
raise
raise RegistryNotReadyError(msg) from exc

Check warning on line 265 in component/core.py

View check run for this annotation

Codecov / codecov/patch

component/core.py#L265

Added line #L265 was not covered by tests
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 <guewen.baconnier@camptocamp.com>
* Laurent Mignon <laurent.mignon@acsone.eu>
* Simone Orsi <simone.orsi@camptocamp.com>
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)

Check warning on line 8 in component/utils.py

View check run for this annotation

Codecov / codecov/patch

component/utils.py#L8

Added line #L8 was not covered by tests


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
Loading