Skip to content

Commit

Permalink
[IMP] rating: don't call compute method directly
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jem-odoo authored and beledouxdenis committed Dec 10, 2018
1 parent d40956c commit 4ab3edb
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions addons/rating/models/rating.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,18 @@ def _compute_rating_count(self):
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):
Expand Down

0 comments on commit 4ab3edb

Please sign in to comment.