diff --git a/server_environment/models/server_env_mixin.py b/server_environment/models/server_env_mixin.py index 87f485dd6..18760ee7f 100644 --- a/server_environment/models/server_env_mixin.py +++ b/server_environment/models/server_env_mixin.py @@ -270,8 +270,12 @@ def _compute_server_env(self): If an env-computed field has no key in configuration files, read from the ``_env_default`` field from database. """ + self._server_env_compute_fields(self._server_env_fields) + + def _server_env_compute_fields(self, field_names): for record in self: - for field_name, options in self._server_env_fields.items(): + for field_name in field_names: + options = self._server_env_fields[field_name] if record._server_env_has_key_defined(field_name): record._compute_server_env_from_config(field_name, options) @@ -293,6 +297,33 @@ def _inverse_server_env(self, field_name): elif default_field: record[default_field] = record[field_name] + def _validate_fields(self, field_names, excluded_names=()): + """Put the protected env-computed fields in cache before constraints + + Env-computed fields share ``_compute_server_env`` and have no field + dependency: while one of them is written, the ORM protects all of + them against recomputation, and a constraint reading one that is not + in cache gets an empty value instead of computing it. + + The fields being written are skipped: if they were evicted from + cache, the ORM writes them again before inversing them, and filling + them from the configuration or stored defaults would hide that and + lose their new values. For the same reason, this is not done in the + inverse, which runs while other written fields await theirs. + """ + field_names = set(field_names) + if self._constraint_methods: + for record in self: + missing = [ + name + for name in self._server_env_fields + if name not in field_names + and name not in record._cache + and self.env.is_protected(self._fields[name], record) + ] + record._server_env_compute_fields(missing) + return super()._validate_fields(field_names, excluded_names) + def _compute_server_env_is_editable(self): """Compute _is_editable values diff --git a/server_environment/tests/__init__.py b/server_environment/tests/__init__.py index de89319b7..b413bc747 100644 --- a/server_environment/tests/__init__.py +++ b/server_environment/tests/__init__.py @@ -2,3 +2,4 @@ from . import test_server_environment_config from . import test_environment_variable from . import test_preserve_not_env_managed_data +from . import test_sibling_cache diff --git a/server_environment/tests/common.py b/server_environment/tests/common.py index 69eb34a4d..7c89a7bc3 100644 --- a/server_environment/tests/common.py +++ b/server_environment/tests/common.py @@ -56,8 +56,12 @@ def load_config( parser = server_env._load_config() serv_config_class.serv_config = parser server_env.serv_config = parser + # env-computed fields have no dependency to invalidate them + # when the configuration changes + self.env.invalidate_all() yield finally: serv_config_class.serv_config = original_serv_config server_env.serv_config = original_serv_config + self.env.invalidate_all() diff --git a/server_environment/tests/models.py b/server_environment/tests/models.py index 6f7447f22..7c3a196a1 100644 --- a/server_environment/tests/models.py +++ b/server_environment/tests/models.py @@ -1,4 +1,5 @@ from odoo import api, fields, models +from odoo.exceptions import ValidationError class ExternalService(models.Model): @@ -32,6 +33,17 @@ def _server_env_fields(self): }, } + # Reads sibling env fields (``user``, ``password``) while another one + # (``host``) is written. Tests creating or writing a ``host`` must + # provide a ``user`` as well. + @api.constrains("host") + def _check_credentials_when_host(self): + for record in self: + if record.host and not (record.user and record.password): + raise ValidationError( + self.env._("A user and a password are required with a host.") + ) + def _compute_password(self): for record in self: record.password = "computed_password" diff --git a/server_environment/tests/test_sibling_cache.py b/server_environment/tests/test_sibling_cache.py new file mode 100644 index 000000000..efa1de2fa --- /dev/null +++ b/server_environment/tests/test_sibling_cache.py @@ -0,0 +1,55 @@ +# Copyright 2026 Camptocamp SA +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo.exceptions import ValidationError +from odoo.orm.model_classes import add_to_registry +from odoo.tests import tagged + +from . import common + + +# The test model's table is rolled back but the model stays in the +# registry: at install, the next module loading would find it tableless +@tagged("post_install", "-at_install") +class TestSiblingCache(common.ServerEnvironmentCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + from .models import ExternalService + + add_to_registry(cls.registry, ExternalService) + cls.registry._setup_models__(cls.env.cr, ["external.service"]) + cls.registry.init_models( + cls.env.cr, ["external.service"], {"models_to_check": True} + ) + + def setUp(self): + super().setUp() + self.service = self.env["external.service"].create( + { + "name": "my_service", + "description": "Description my_service", + "host": "localhost", + "user": "foo", + } + ) + # Start from an empty cache, as a web client save or an RPC call does + self.service.invalidate_recordset() + + def test_constraint_reads_sibling_env_field_from_config(self): + """A sibling defined in the configuration is not read as empty.""" + config = "[external_service.my_service]\nuser=from_config\n" + with self.load_config(public=config): + self.service.write({"host": "other.example.com"}) + + def test_constraint_reads_sibling_env_field_from_default(self): + """Siblings with a stored default (``user``) or a ``compute_default`` + (``password``) are not read as empty.""" + self.service.write({"host": "other.example.com"}) + + def test_constraint_still_raises(self): + """The constraint is still enforced with the sibling's actual value.""" + self.service.write({"user": False}) + self.service.invalidate_recordset() + with self.assertRaises(ValidationError): + self.service.write({"host": "other.example.com"})