[FIX] api: environment recomputation

In a workflow context (for instance, in the invoice workflow),
context is not passed.

Therefore, relying on the 'recompute' key being the context
in order to not recompute the fields does not work with Workflows.

It leads to huge performance issues,
as fields are recomputed recursively (instead of sequentially)
when several records are implied.
For instance, when reconciling several invoices with one payment
(100 invoices with 1 payment for instance),
records of each invoice are recomputed uselessly in each workflow call
(for each "confirm_paid" method done for each invoice).

With a significant number of invoices (100, for instance),
it even leads to a "Maximum recursion depth reached" errror.

closes #4905
This commit is contained in:
Denis Ledoux 2015-02-12 14:57:31 +01:00
parent a67747f77e
commit c9154e08aa
4 changed files with 70 additions and 53 deletions

View File

@ -1391,7 +1391,8 @@ class account_move_line(osv.osv):
self.create(cr, uid, data, context) self.create(cr, uid, data, context)
del vals['account_tax_id'] del vals['account_tax_id']
if check and not context.get('novalidate') and (context.get('recompute', True) or journal.entry_posted): recompute = journal.env.recompute and context.get('recompute', True)
if check and not context.get('novalidate') and (recompute or journal.entry_posted):
tmp = move_obj.validate(cr, uid, [vals['move_id']], context) tmp = move_obj.validate(cr, uid, [vals['move_id']], context)
if journal.entry_posted and tmp: if journal.entry_posted and tmp:
move_obj.button_validate(cr,uid, [vals['move_id']], context) move_obj.button_validate(cr,uid, [vals['move_id']], context)

View File

@ -890,6 +890,19 @@ class Environment(object):
if invalids: if invalids:
raise Warning('Invalid cache for fields\n' + pformat(invalids)) raise Warning('Invalid cache for fields\n' + pformat(invalids))
@property
def recompute(self):
return self.all.recompute
@contextmanager
def norecompute(self):
tmp = self.all.recompute
self.all.recompute = False
try:
yield
finally:
self.all.recompute = tmp
class Environments(object): class Environments(object):
""" A common object for all environments in a request. """ """ A common object for all environments in a request. """
@ -897,6 +910,7 @@ class Environments(object):
self.envs = WeakSet() # weak set of environments self.envs = WeakSet() # weak set of environments
self.todo = {} # recomputations {field: [records]} self.todo = {} # recomputations {field: [records]}
self.mode = False # flag for draft/onchange self.mode = False # flag for draft/onchange
self.recompute = True
def add(self, env): def add(self, env):
""" Add the environment `env`. """ """ Add the environment `env`. """

View File

@ -3973,7 +3973,7 @@ class BaseModel(object):
self.pool[model_name]._store_set_values(cr, user, todo, fields_to_recompute, context) self.pool[model_name]._store_set_values(cr, user, todo, fields_to_recompute, context)
# recompute new-style fields # recompute new-style fields
if context.get('recompute', True): if recs.env.recompute and context.get('recompute', True):
recs.recompute() recs.recompute()
self.step_workflow(cr, user, ids, context=context) self.step_workflow(cr, user, ids, context=context)
@ -4220,7 +4220,7 @@ class BaseModel(object):
# check Python constraints # check Python constraints
recs._validate_fields(vals) recs._validate_fields(vals)
if context.get('recompute', True): if recs.env.recompute and context.get('recompute', True):
result += self._store_get_values(cr, user, [id_new], result += self._store_get_values(cr, user, [id_new],
list(set(vals.keys() + self._inherits.values())), list(set(vals.keys() + self._inherits.values())),
context) context)
@ -4233,7 +4233,7 @@ class BaseModel(object):
# recompute new-style fields # recompute new-style fields
recs.recompute() recs.recompute()
if self._log_create and context.get('recompute', True): if self._log_create and recs.env.recompute and context.get('recompute', True):
message = self._description + \ message = self._description + \
" '" + \ " '" + \
self.name_get(cr, user, [id_new], context=context)[0][1] + \ self.name_get(cr, user, [id_new], context=context)[0][1] + \
@ -5629,12 +5629,13 @@ class BaseModel(object):
field, recs = self.env.get_todo() field, recs = self.env.get_todo()
# evaluate the fields to recompute, and save them to database # evaluate the fields to recompute, and save them to database
names = [f.name for f in field.computed_fields if f.store] names = [f.name for f in field.computed_fields if f.store]
for rec, rec1 in zip(recs, recs.with_context(recompute=False)): for rec in recs:
try: try:
values = rec._convert_to_write({ values = rec._convert_to_write({
name: rec[name] for name in names name: rec[name] for name in names
}) })
rec1._write(values) with rec.env.norecompute():
rec._write(values)
except MissingError: except MissingError:
pass pass
# mark the computed fields as done # mark the computed fields as done

View File

@ -736,10 +736,11 @@ class one2many(_column):
result = [] result = []
context = dict(context or {}) context = dict(context or {})
context.update(self._context) context.update(self._context)
context['recompute'] = False # recomputation is done by outer create/write
if not values: if not values:
return return
obj = obj.pool[self._obj] obj = obj.pool[self._obj]
rec = obj.browse(cr, user, [], context=context)
with rec.env.norecompute():
_table = obj._table _table = obj._table
for act in values: for act in values:
if act[0] == 0: if act[0] == 0: