Browse Source

[FIX] auditlog: log computed fields stored in db as expected

Fixing #1134.
Odoo stores values of computed fields at the end of the transaction
only, as such performing a 'read()' to make a data snapshot on the record
created in the current transaction doesn't return the expected result
regarding these fields.
Also as a side-effect 'read()' alters the environment cache and break the
values on the record inducing issues in the whole user
transaction/workflow.

This fix replaces the use of 'read()' to do the data snapshot directly
from the cache of the record (computed values are already there).
12.0-mig-module_prototyper_last
sebalix 5 years ago
parent
commit
a68087698c
  1. 11
      auditlog/models/rule.py

11
auditlog/models/rule.py

@ -214,9 +214,14 @@ class AuditlogRule(models.Model):
self = self.with_context(auditlog_disabled=True) self = self.with_context(auditlog_disabled=True)
rule_model = self.env['auditlog.rule'] rule_model = self.env['auditlog.rule']
new_record = create_full.origin(self, vals, **kwargs) new_record = create_full.origin(self, vals, **kwargs)
new_values = dict(
(d['id'], d) for d in new_record.sudo()
.with_context(prefetch_fields=False).read(list(self._fields)))
# Take a snapshot of record values from the cache instead of using
# 'read()'. It avoids issues with related/computed fields which
# stored in the database only at the end of the transaction, but
# their values exist in cache.
new_values = {new_record.id: {}}
for fname, field in new_record._fields.items():
new_values[new_record.id][fname] = field.convert_to_read(
new_record[fname], new_record)
rule_model.sudo().create_logs( rule_model.sudo().create_logs(
self.env.uid, self._name, new_record.ids, self.env.uid, self._name, new_record.ids,
'create', None, new_values, {'log_type': log_type}) 'create', None, new_values, {'log_type': log_type})

Loading…
Cancel
Save