From d04ca8fa609d29481c94d4474ad6878ab3866daf Mon Sep 17 00:00:00 2001 From: Lucas Perais Date: Fri, 27 Oct 2023 16:07:36 +0200 Subject: [PATCH] [FIX] base_automation: fix check and execute time based Before this commit, the ir.cron "Automation Rules: check and execute" crashed because the "_check" method used get_eval_context where it shouldn't This is due to b846d1ebe9129a11ee0678aa287784e767ff50e5, which makes _get_eval_context a record's method, not a model one. closes odoo/odoo#140153 Signed-off-by: Lucas Perais (lpe) --- .../base_automation/models/base_automation.py | 2 +- addons/test_base_automation/tests/test_flow.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/addons/base_automation/models/base_automation.py b/addons/base_automation/models/base_automation.py index dcb2031294f..8d2ae7862c0 100644 --- a/addons/base_automation/models/base_automation.py +++ b/addons/base_automation/models/base_automation.py @@ -892,10 +892,10 @@ class BaseAutomation(models.Model): self = self.with_context(__action_done={}) # retrieve all the automation rules to run based on a timed condition - eval_context = self._get_eval_context() for automation in self.with_context(active_test=True).search([('trigger', 'in', TIME_TRIGGERS)]): _logger.info("Starting time-based automation rule `%s`.", automation.name) last_run = fields.Datetime.from_string(automation.last_run) or datetime.datetime.utcfromtimestamp(0) + eval_context = automation._get_eval_context() # retrieve all the records that satisfy the automation's condition domain = [] diff --git a/addons/test_base_automation/tests/test_flow.py b/addons/test_base_automation/tests/test_flow.py index d53338ebfe9..01c6dc59acc 100644 --- a/addons/test_base_automation/tests/test_flow.py +++ b/addons/test_base_automation/tests/test_flow.py @@ -855,6 +855,24 @@ if env.context.get('old_values', None): # on write lead.unlink() self.assertEqual(called_count, 1) + def test_004_check_method(self): + model = self.env["ir.model"]._get("base.automation.lead.test") + TIME_TRIGGERS = [ + 'on_time', + 'on_time_created', + 'on_time_updated', + ] + self.env["base.automation"].search([('trigger', 'in', TIME_TRIGGERS)]).active = False + + automation = self.env["base.automation"].create({ + "name": "Cron BaseAuto", + "trigger": "on_time", + "model_id": model.id, + }) + self.assertFalse(automation.last_run) + self.env["base.automation"]._check(False) + self.assertTrue(automation.last_run) + @common.tagged('post_install', '-at_install') class TestCompute(common.TransactionCase):