From f098c787c0c94fb18992ae40b5aea0fcfb1315c7 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 | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/addons/rating/models/rating.py b/addons/rating/models/rating.py index d0cda7f1bda..5c81e0032e8 100644 --- a/addons/rating/models/rating.py +++ b/addons/rating/models/rating.py @@ -166,13 +166,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: - for rating in record.rating_ids: - rating.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):