[FIX] auditlog: add support for create multi
parent
0f990fcbab
commit
090bd7d20c
|
@ -1,6 +1,8 @@
|
||||||
# Copyright 2015 ABF OSIELL <https://osiell.com>
|
# Copyright 2015 ABF OSIELL <https://osiell.com>
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
import copy
|
||||||
|
|
||||||
from odoo import _, api, fields, models, modules
|
from odoo import _, api, fields, models, modules
|
||||||
|
|
||||||
FIELDS_BLACKLIST = [
|
FIELDS_BLACKLIST = [
|
||||||
|
@ -234,50 +236,54 @@ class AuditlogRule(models.Model):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
log_type = self.log_type
|
log_type = self.log_type
|
||||||
|
|
||||||
@api.model
|
@api.model_create_multi
|
||||||
@api.returns("self", lambda value: value.id)
|
@api.returns("self", lambda value: value.id)
|
||||||
def create_full(self, vals, **kwargs):
|
def create_full(self, vals_list, **kwargs):
|
||||||
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_records = create_full.origin(self, vals_list, **kwargs)
|
||||||
# Take a snapshot of record values from the cache instead of using
|
# Take a snapshot of record values from the cache instead of using
|
||||||
# 'read()'. It avoids issues with related/computed fields which
|
# 'read()'. It avoids issues with related/computed fields which
|
||||||
# stored in the database only at the end of the transaction, but
|
# stored in the database only at the end of the transaction, but
|
||||||
# their values exist in cache.
|
# their values exist in cache.
|
||||||
new_values = {new_record.id: {}}
|
new_values = {}
|
||||||
for fname, field in new_record._fields.items():
|
for new_record in new_records:
|
||||||
new_values[new_record.id][fname] = field.convert_to_read(
|
new_values.setdefault(new_record.id, {})
|
||||||
new_record[fname], new_record
|
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.env.uid,
|
||||||
self._name,
|
self._name,
|
||||||
new_record.ids,
|
new_records.ids,
|
||||||
"create",
|
"create",
|
||||||
None,
|
None,
|
||||||
new_values,
|
new_values,
|
||||||
{"log_type": log_type},
|
{"log_type": log_type},
|
||||||
)
|
)
|
||||||
return new_record
|
return new_records
|
||||||
|
|
||||||
@api.model
|
@api.model_create_multi
|
||||||
@api.returns("self", lambda value: value.id)
|
@api.returns("self", lambda value: value.id)
|
||||||
def create_fast(self, vals, **kwargs):
|
def create_fast(self, vals_list, **kwargs):
|
||||||
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"]
|
||||||
vals2 = dict(vals)
|
vals_list2 = copy.deepcopy(vals_list)
|
||||||
new_record = create_fast.origin(self, vals, **kwargs)
|
new_records = create_fast.origin(self, vals_list, **kwargs)
|
||||||
new_values = {new_record.id: vals2}
|
new_values = {}
|
||||||
|
for vals, new_record in zip(vals_list2, new_records):
|
||||||
|
new_values.setdefault(new_record.id, vals)
|
||||||
rule_model.sudo().create_logs(
|
rule_model.sudo().create_logs(
|
||||||
self.env.uid,
|
self.env.uid,
|
||||||
self._name,
|
self._name,
|
||||||
new_record.ids,
|
new_records.ids,
|
||||||
"create",
|
"create",
|
||||||
None,
|
None,
|
||||||
new_values,
|
new_values,
|
||||||
{"log_type": log_type},
|
{"log_type": log_type},
|
||||||
)
|
)
|
||||||
return new_record
|
return new_records
|
||||||
|
|
||||||
return create_full if self.log_type == "full" else create_fast
|
return create_full if self.log_type == "full" else create_fast
|
||||||
|
|
||||||
|
|
|
@ -100,6 +100,35 @@ class AuditlogCommon(object):
|
||||||
).ensure_one()
|
).ensure_one()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_LogCreation4(self):
|
||||||
|
"""Fourth test, create several records at once (with create multi
|
||||||
|
feature starting from Odoo 12) and check that the same number of logs
|
||||||
|
has been generated.
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.groups_rule.subscribe()
|
||||||
|
|
||||||
|
auditlog_log = self.env["auditlog.log"]
|
||||||
|
groups_vals = [
|
||||||
|
{"name": "testgroup1"},
|
||||||
|
{"name": "testgroup3"},
|
||||||
|
{"name": "testgroup2"},
|
||||||
|
]
|
||||||
|
groups = self.env["res.groups"].create(groups_vals)
|
||||||
|
# Ensure that the recordset returns is in the same order
|
||||||
|
# than list of vals
|
||||||
|
expected_names = ["testgroup1", "testgroup3", "testgroup2"]
|
||||||
|
self.assertEqual(groups.mapped("name"), expected_names)
|
||||||
|
|
||||||
|
logs = auditlog_log.search(
|
||||||
|
[
|
||||||
|
("model_id", "=", self.groups_model_id),
|
||||||
|
("method", "=", "create"),
|
||||||
|
("res_id", "in", groups.ids),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.assertEqual(len(logs), len(groups))
|
||||||
|
|
||||||
|
|
||||||
class TestAuditlogFull(TransactionCase, AuditlogCommon):
|
class TestAuditlogFull(TransactionCase, AuditlogCommon):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Reference in New Issue