[FIX] (test_)base_automation: clear cache for on_change rules

Create an automation rule for a field for which there's no onchange
yet. The rule type is "on_change" (on UI change).

This type of automation rule adds an onchange method on the
selected fields, which thus involve the onchange mecanism: the
"on_change" attribute will be set on that field nodes in views,
s.t. when the user changes it, onchange rpcs are done.

However, views are cached, and creating such a rule didn't clear
the cache. So if the view was already in cache, the rule seemed
not to work, because the client still received the old version of
the view, without the "on_change" attribute.

This commit clears the cache when such a rule is created, s.t.
after a client reload, the feature gets enabled as expected.

opw 3701125

closes odoo/odoo#152498

Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
This commit is contained in:
Aaron Bohy
2024-02-03 06:59:49 +00:00
parent 9f09870107
commit bc800ae093
2 changed files with 25 additions and 0 deletions
@@ -849,6 +849,8 @@ class BaseAutomation(models.Model):
method = make_onchange(automation_rule.id)
for field in automation_rule.on_change_field_ids:
Model._onchange_methods[field.name].append(method)
if automation_rule.on_change_field_ids:
self.env.registry.clear_cache('templates')
if automation_rule.model_id.is_mail_thread and automation_rule.trigger in MAIL_TRIGGERS:
def _message_post(self, *args, **kwargs):
@@ -1100,3 +1100,26 @@ class TestHttp(common.HttpCase):
"_id": obj.id,
"_model": obj._name,
})
def test_on_change_get_views_cache(self):
model_name = "base.automation.lead.test"
my_view = self.env["ir.ui.view"].create({
"name": "My View",
"model": model_name,
"type": "form",
"arch": "<form><field name='active'/></form>",
})
self.assertEqual(
self.env[model_name].get_view(my_view.id)["arch"],
'<form><field name="active"/></form>'
)
model = self.env["ir.model"]._get(model_name)
active_field = self.env["ir.model.fields"]._get(model_name, "active")
create_automation(self, trigger="on_change", model_id=model.id, on_change_field_ids=[Command.set([active_field.id])], _actions={
"state": "code",
"code": "",
})
self.assertEqual(
self.env[model_name].get_view(my_view.id)["arch"],
'<form><field name="active" on_change="1"/></form>'
)