Skip to content

Commit 99fa146

Browse files
committed
[FIX] auditlog: Change comparison with old field values.
Fix the comparison with old values when setting fields to empty values. The old values for the comparison when using fast log mode were initialized with False. This causes the problem that when a field is set to an emty value by the user, auditlog does not create a log line because internal checks compare False to False and do not consider logging the change. Full log mode is not affected by this as there everything is logged by default. However, even in fast log mode, some companies have regulatory requirements which force them to log user actions that set empty values to fields.
1 parent 01057ff commit 99fa146

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

auditlog/models/rule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def write_fast(self, vals, **kwargs):
463463
# afterwards as it could not represent the real state
464464
# of the data in the database
465465
vals2 = dict(vals)
466-
old_vals2 = dict.fromkeys(list(vals2.keys()), False)
466+
old_vals2 = dict.fromkeys(list(vals2.keys()), None)
467467
old_values = {id_: old_vals2 for id_ in self.ids}
468468
new_values = {id_: vals2 for id_ in self.ids}
469469
result = write_fast.origin(self, vals, **kwargs)

auditlog/tests/test_auditlog.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,54 @@ def tearDown(self):
343343
super(TestAuditlogFast, self).tearDown()
344344

345345

346+
class TestAuditlogFastWriteEmptyValue(AuditLogRuleCommon):
347+
@classmethod
348+
def setUpClass(cls):
349+
super().setUpClass()
350+
cls.test_model = cls.env["ir.model"].search([("model", "=", "res.partner")])
351+
cls.auditlog_rule = cls.create_rule(
352+
{
353+
"name": "test.model_res_partner",
354+
"model_id": cls.test_model.id,
355+
"log_type": "fast",
356+
"log_read": False,
357+
"log_create": False,
358+
"log_write": True,
359+
"log_unlink": False,
360+
}
361+
)
362+
cls.test_record = (
363+
cls.env["res.partner"]
364+
.with_context(tracking_disable=True)
365+
.create(
366+
{
367+
"name": "testpartner3",
368+
"phone": "123",
369+
}
370+
)
371+
)
372+
cls.auditlog_rule.subscribe()
373+
374+
def test_when_removing_field_value_a_log_line_is_created(self):
375+
self.test_record.write({"phone": False})
376+
logs = self.env["auditlog.log"].search(
377+
[
378+
("res_id", "=", self.test_record.id),
379+
("model_id", "=", self.test_model.id),
380+
]
381+
)
382+
self.assertEqual(len(logs), 1)
383+
self.assertEqual(logs[0].model_name, "Contact")
384+
self.assertEqual(logs[0].model_model, "res.partner")
385+
log_lines = logs.mapped("line_ids")
386+
self.assertEqual(len(log_lines), 1)
387+
self.assertEqual(log_lines[0].field_name, "phone")
388+
self.assertFalse(log_lines[0].old_value)
389+
self.assertFalse(log_lines[0].old_value_text)
390+
self.assertFalse(log_lines[0].new_value)
391+
self.assertFalse(log_lines[0].new_value_text)
392+
393+
346394
class TestFieldRemoval(AuditLogRuleCommon):
347395
@classmethod
348396
def setUpClass(cls):

0 commit comments

Comments
 (0)