diff options
| author | Yannick Tivisse <yti@odoo.com> | 2020-07-08 13:26:27 +0000 |
|---|---|---|
| committer | Yannick Tivisse <yti@odoo.com> | 2020-08-04 15:01:45 +0000 |
| commit | 62355b6ad3e2af16efabe1c3ca2fb053a6084bc1 (patch) | |
| tree | 4286761086dad2cc81b3b4a5c70f46057b79f458 /odoo/addons/test_new_api/models/test_new_api.py | |
| parent | eecb1e01fc7cd7de3f3f7fd3d531e43bb8484a88 (diff) | |
[FIX] test_new_api: Fix bad shared cache usage for stored computed fields
Purpose
=======
Since the 13.0, a regression has been introduced in hr_timesheet.
Considering:
- The account.analytic.line model, representing a timesheet entry
- The project.task model, representing a task, on which we have the fields:
- timesheet_ids: One2many
- planned_hours: Computed (sudo), stored, representing the sum of the timesheet amounts
- An ir.rule restricting the timesheets CRUD to his own only.
A user can only see its own timesheets on a task, but the field "Planned Hours",
which is stored-compute_sudo, should take all the timesheet lines into account
However, when adding a new line and then recomputing the value, no existing line
from another user is binded on self, then the value is erased and saved on the
database.
Specification
=============
This commit introduces a test that illustrates that bad usage of a shared cache.
A correct way to fix this could be to avoid using the cache if an ir.rule is pointing
to the related model. That would force the recomputation in a real sudoed environment.
closes odoo/odoo#55408
Taskid: 2285924
X-original-commit: 50e229fe66b2a82717d4260287e18a875bbd26f5
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
Diffstat (limited to 'odoo/addons/test_new_api/models/test_new_api.py')
| -rw-r--r-- | odoo/addons/test_new_api/models/test_new_api.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/odoo/addons/test_new_api/models/test_new_api.py b/odoo/addons/test_new_api/models/test_new_api.py index 0ceb37a0988..602173c4145 100644 --- a/odoo/addons/test_new_api/models/test_new_api.py +++ b/odoo/addons/test_new_api/models/test_new_api.py @@ -1052,3 +1052,29 @@ class SelectionNonStored(models.Model): ('foo', "Foo"), ('bar', "Bar"), ], store=False) + + +# Special classes to ensure the correct usage of a shared cache amongst users. +# See the method test_shared_cache_computed_field +class SharedCacheComputeParent(models.Model): + _name = 'test_new_api.model_shared_cache_compute_parent' + _description = 'model_shared_cache_compute_parent' + + name = fields.Char(string="Task Name") + line_ids = fields.One2many( + 'test_new_api.model_shared_cache_compute_line', 'parent_id', string="Timesheets") + total_amount = fields.Integer(compute='_compute_total_amount', store=True, compute_sudo=True) + + @api.depends('line_ids.amount') + def _compute_total_amount(self): + for parent in self: + parent.total_amount = sum(parent.line_ids.mapped('amount')) + + +class ShareCacheComputeLine(models.Model): + _name = 'test_new_api.model_shared_cache_compute_line' + _description = 'model_shared_cache_compute_line' + + parent_id = fields.Many2one('test_new_api.model_shared_cache_compute_parent') + amount = fields.Integer() + user_id = fields.Many2one('res.users', default= lambda self: self.env.user) # Note: There is an ir.rule about this. |
