[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
This commit is contained in:
jem-odoo
2018-12-04 12:48:19 +00:00
parent 2aa7399d0c
commit 9be23c2f63
+12 -6
View File
@@ -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):