From 9be23c2f6367265fa9e9f033197b6d6ebb78ea3e Mon Sep 17 00:00:00 2001 From: jem-odoo Date: Wed, 7 Nov 2018 07:55:23 +0000 Subject: [PATCH] [IMP] rating: don't call compute method directly The correct way to trigger a recomputation of a field is not to call directly the compute method. The ORM offers mecanism to do it: we have to flag the field as "recompute to do" and then call `recompute` method (availble on every models). This one will call the recompute method of all fields marked as "to recompute". This will optimize the performance by doing recomputation in batch. Task-1903565 --- addons/rating/models/rating.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/addons/rating/models/rating.py b/addons/rating/models/rating.py index f547506cea6..f3614786844 100644 --- a/addons/rating/models/rating.py +++ b/addons/rating/models/rating.py @@ -202,12 +202,18 @@ class RatingMixin(models.AbstractModel): def write(self, values): """ If the rated ressource name is modified, we should update the rating res_name too. If the rated ressource parent is changed we should update the parent_res_id too""" - result = super(RatingMixin, self).write(values) - for record in self: - if record._rec_name in values: - record.rating_ids._compute_res_name() - if record.rating_get_parent() in values: - record.rating_ids.write({'parent_res_id': record[record.rating_get_parent()].id}) + with self.env.norecompute(): + result = super(RatingMixin, self).write(values) + for record in self: + if record._rec_name in values: # set the res_name of ratings to be recomputed + res_name_field = self.env['rating.rating']._fields['res_name'] + record.rating_ids._recompute_todo(res_name_field) + if record.rating_get_parent() in values: + record.rating_ids.write({'parent_res_id': record[record.rating_get_parent()].id}) + + if self.env.recompute and self._context.get('recompute', True): # trigger the recomputation of all field marked as "to recompute" + self.recompute() + return result def unlink(self):