diff --git a/addons/base_automation/models/base_automation.py b/addons/base_automation/models/base_automation.py index 201b16417f7..9c596578261 100644 --- a/addons/base_automation/models/base_automation.py +++ b/addons/base_automation/models/base_automation.py @@ -206,8 +206,8 @@ class BaseAutomation(models.Model): """ Filter the records that satisfy the precondition of action ``self``. """ self_sudo = self.sudo() if self_sudo.filter_pre_domain and records: - domain = [('id', 'in', records.ids)] + safe_eval.safe_eval(self_sudo.filter_pre_domain, self._get_eval_context()) - return records.sudo().search(domain).with_env(records.env) + domain = safe_eval.safe_eval(self_sudo.filter_pre_domain, self._get_eval_context()) + return records.sudo().filtered_domain(domain).with_env(records.env) else: return records diff --git a/addons/test_base_automation/tests/test_flow.py b/addons/test_base_automation/tests/test_flow.py index 780a037eadd..107b167a838 100644 --- a/addons/test_base_automation/tests/test_flow.py +++ b/addons/test_base_automation/tests/test_flow.py @@ -4,7 +4,7 @@ from unittest.mock import patch from odoo.addons.base.tests.common import TransactionCaseWithUserDemo -from odoo.tests import tagged +from odoo.tests import common, tagged from odoo.exceptions import AccessError @@ -354,3 +354,53 @@ record['name'] = record.name + 'X'""", rec3.write({'name': 'a first record'}) rec4 = Model.with_user(self.user_demo).create({'name': 'again another record'}) rec4.write({'name': 'another value'}) + +@common.tagged('post_install','-at_install') +class TestCompute(common.TransactionCase): + def test_inversion(self): + """ If a stored field B depends on A, an update to the trigger for A + should trigger the recomputaton of A, then B. + + However if a search() is performed during the computation of A + ??? and _order is affected ??? a flush will be triggered, forcing the + computation of B, based on the previous A. + + This happens if a rule has has a non-empty filter_pre_domain, even if + it's an empty list (``'[]'`` as opposed to ``False``). + """ + company1 = self.env['res.partner'].create({ + 'name': "Gorofy", + 'is_company': True, + }) + company2 = self.env['res.partner'].create({ + 'name': "Awiclo", + 'is_company': True + }) + r = self.env['res.partner'].create({ + 'name': 'Bob', + 'is_company': False, + 'parent_id': company1.id + }) + self.assertEqual(r.display_name, 'Gorofy, Bob') + r.parent_id = company2 + self.assertEqual(r.display_name, 'Awiclo, Bob') + + self.env['base.automation'].create({ + 'name': "test rule", + 'filter_pre_domain': False, + 'trigger': 'on_create_or_write', + 'state': 'code', # no-op action + 'model_id': self.env.ref('base.model_res_partner').id, + }) + r.parent_id = company1 + self.assertEqual(r.display_name, 'Gorofy, Bob') + + self.env['base.automation'].create({ + 'name': "test rule", + 'filter_pre_domain': '[]', + 'trigger': 'on_create_or_write', + 'state': 'code', # no-op action + 'model_id': self.env.ref('base.model_res_partner').id, + }) + r.parent_id = company2 + self.assertEqual(r.display_name, 'Awiclo, Bob')