Files
odoo_source/addons/base_automation/tests
Thomas Lefebvre (thle) 5fe2866cc4 [FIX] base_automation: differentiate between creating and writing
Steps to reproduce:
-------------------
[A] Create an automated action with:
- Model: Tasks (project.task)
- Trigger: On save
- When updating: ID (to mimic "on_create" trigger)
- Execute code:
```py
record.message_post(body="This is a message from automated action (fields triggers: ['id'])")
```

[B] Create a server action (contextual action) with:
- Type: execute code
- Model: Tasks (project.task)
- code:
```py
record.write({})
```

[C] Test:
- create a new task --> message has been posted (OK)
- write some values on the task --> no message has been posted (OK)
- run the server action --> message has been posted (KO)

Issue:
------
No message should be posted as the ID field has not been modified.

Cause:
------
`if not self._context.get('old_values')` is considered as `True`
because we have an empty dict.
In fact, when we make a write on a record, old_values will be always a dict.

```py
old_values = {
    old_vals.pop('id'): old_vals
    for old_vals in (records.read(list(vals)) if vals else [])
}
```

Solution:
---------
Old values are not defined in the context during a create.
We can compare `self._context.get('old_values')` with `None`
to differentiate between creating and writing on records.

opw-3736068

closes odoo/odoo#155832

Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2024-04-16 14:12:46 +00:00
..