[FIX] base_automation: avoid recomputing readonly field before write

`base_automation` module patches `write` method to detect changes that trigger
the automated actions. It's implemented by reading current values before making
the write operaion. It may lead to max recursion error if compute method updates
other records. Example: in `account_asset` Depreciation values are computed for
all Depreciation Lines at the same time [1].

Fix it by breaking recursion on computed not stored readonly fields: `write`
method for such fields might be called by compute method only and hence the old
value is always equal to new value.

STEPS:

- Create an Asset model of 240 Months, Straight Line, No pro rata
- Create Assets using this asset model and confirm it.
- Create an automated action for account.move and trigger on update (Action can
be anything)
- Now try to access the Assets

[1]: https://github.com/odoo/enterprise/blob/45dd0884c84d0e8a31c2c36a975f1b1f7cf6d01c/account_asset/models/account_move.py#L39-L46

opw-3147688

closes odoo/odoo#115216

X-original-commit: 0561ad2324905aed5afd5cc163e5e6aebee09d40
Signed-off-by: Julien Castiaux (juc) <juc@odoo.com>
Signed-off-by: Ivan Elizaryev (iel) <iel@odoo.com>
This commit is contained in:
Ivan Yelizariev
2023-03-14 17:03:35 +01:00
parent b944808bb3
commit 59edbb7894
@@ -403,9 +403,15 @@ class BaseAutomation(models.Model):
# check preconditions on records
pre = {action: action._filter_pre(records) for action in actions}
# read old values before the update
# but skip readonly computed not stored fields
fields = [f for f in vals if f in self._fields and not (
self._fields[f].readonly and
self._fields[f].compute and
not self._fields[f].store
)]
old_values = {
old_vals.pop('id'): old_vals
for old_vals in (records.read(list(vals)) if vals else [])
for old_vals in (records.read(fields) if fields else [])
}
# call original method
write.origin(self.with_env(actions.env), vals, **kw)