Browse Source

Merge PR #2112 into 12.0

Signed-off-by dreispt
12.0-mig-module_prototyper_last
OCA-git-bot 3 years ago
parent
commit
21f75eec00
  1. 12
      auditlog/models/rule.py
  2. 2
      auditlog/readme/CONTRIBUTORS.rst
  3. 73
      auditlog/tests/test_auditlog.py
  4. 1
      auditlog/views/auditlog_view.xml

12
auditlog/models/rule.py

@ -105,6 +105,10 @@ class AuditlogRule(models.Model):
action_id = fields.Many2one( action_id = fields.Many2one(
'ir.actions.act_window', string="Action", 'ir.actions.act_window', string="Action",
states={'subscribed': [('readonly', True)]}) states={'subscribed': [('readonly', True)]})
capture_record = fields.Boolean(
"Capture Record",
help="Select this if you want to keep track of Unlink Record",
)
_sql_constraints = [ _sql_constraints = [
('model_uniq', 'unique(model_id)', ('model_uniq', 'unique(model_id)',
@ -382,6 +386,9 @@ class AuditlogRule(models.Model):
for res_id in res_ids: for res_id in res_ids:
model_model = self.env[res_model] model_model = self.env[res_model]
name = model_model.browse(res_id).name_get() name = model_model.browse(res_id).name_get()
model_id = self.pool._auditlog_model_cache[res_model]
auditlog_rule = self.env['auditlog.rule'].search(
[("model_id", "=", model_id)])
res_name = name and name[0] and name[0][1] res_name = name and name[0] and name[0][1]
vals = { vals = {
'name': res_name, 'name': res_name,
@ -407,6 +414,11 @@ class AuditlogRule(models.Model):
elif method is 'write': elif method is 'write':
self._create_log_line_on_write( self._create_log_line_on_write(
log, diff.changed(), old_values, new_values) log, diff.changed(), old_values, new_values)
elif method is 'unlink' and auditlog_rule.capture_record:
self._create_log_line_on_read(
log,
list(old_values.get(res_id, EMPTY_DICT).keys()), old_values
)
def _get_field(self, model, field_name): def _get_field(self, model, field_name):
cache = self.pool._auditlog_field_cache cache = self.pool._auditlog_field_cache

2
auditlog/readme/CONTRIBUTORS.rst

@ -1,3 +1,5 @@
* Sebastien Alix <sebastien.alix@camptocamp.com> * Sebastien Alix <sebastien.alix@camptocamp.com>
* Holger Brunn <hbrunn@therp.nl> * Holger Brunn <hbrunn@therp.nl>
* Holden Rehg <holdenrehg@gmail.com> * Holden Rehg <holdenrehg@gmail.com>
* Bhavesh Odedra <bodedra@opensourceintegrators.com>
* Hardik Suthar <hsuthar@opensourceintegrators.com>

73
auditlog/tests/test_auditlog.py

@ -107,6 +107,58 @@ class AuditlogCommon(object):
]) ])
self.assertEqual(len(logs), len(groups)) self.assertEqual(len(logs), len(groups))
def test_LogCreation5(self):
"""Fifth test, create a record and check that the same number of logs
has been generated. And then delete it, check that it has created log
with 0 fields updated.
"""
self.groups_rule.subscribe()
auditlog_log = self.env['auditlog.log']
testgroup5 = self.env['res.groups'].create({
'name': 'testgroup5',
})
self.assertTrue(auditlog_log.search([
('model_id', '=', self.groups_model_id),
('method', '=', 'create'),
('res_id', '=', testgroup5.id),
]).ensure_one())
testgroup5.unlink()
log_record = auditlog_log.search([
('model_id', '=', self.groups_model_id),
('method', '=', 'unlink'),
('res_id', '=', testgroup5.id),
]).ensure_one()
self.assertTrue(log_record)
if not self.groups_rule.capture_record:
self.assertEqual(len(log_record.line_ids), 0)
def test_LogCreation6(self):
"""Six test, create a record and check that the same number of logs
has been generated. And then delete it, check that it has created log
with x fields updated as per rule
"""
self.groups_rule.subscribe()
auditlog_log = self.env['auditlog.log']
testgroup6 = self.env['res.groups'].create({
'name': 'testgroup6',
})
self.assertTrue(auditlog_log.search([
('model_id', '=', self.groups_model_id),
('method', '=', 'create'),
('res_id', '=', testgroup6.id),
]).ensure_one())
testgroup6.unlink()
log_record = auditlog_log.search([
('model_id', '=', self.groups_model_id),
('method', '=', 'unlink'),
('res_id', '=', testgroup6.id),
]).ensure_one()
self.assertTrue(log_record)
if self.groups_rule.capture_record:
self.assertTrue(len(log_record.line_ids) > 0)
class TestAuditlogFull(TransactionCase, AuditlogCommon): class TestAuditlogFull(TransactionCase, AuditlogCommon):
@ -146,3 +198,24 @@ class TestAuditlogFast(TransactionCase, AuditlogCommon):
def tearDown(self): def tearDown(self):
self.groups_rule.unlink() self.groups_rule.unlink()
super(TestAuditlogFast, self).tearDown() super(TestAuditlogFast, self).tearDown()
class TestAuditlogFullCaptureRecord(TransactionCase, AuditlogCommon):
def setUp(self):
super(TestAuditlogFullCaptureRecord, self).setUp()
self.groups_model_id = self.env.ref('base.model_res_groups').id
self.groups_rule = self.env['auditlog.rule'].create({
'name': 'testrule for groups with capture unlink record',
'model_id': self.groups_model_id,
'log_read': True,
'log_create': True,
'log_write': True,
'log_unlink': True,
'log_type': 'full',
'capture_record': True,
})
def tearDown(self):
self.groups_rule.unlink()
super(TestAuditlogFullCaptureRecord, self).tearDown()

1
auditlog/views/auditlog_view.xml

@ -26,6 +26,7 @@
<field name="model_id"/> <field name="model_id"/>
<field name="log_type"/> <field name="log_type"/>
<field name="action_id" readonly="1" groups="base.group_no_one"/> <field name="action_id" readonly="1" groups="base.group_no_one"/>
<field name="capture_record" attrs="{'invisible':['|' ,('log_type','!=', 'full'), ('log_unlink','!=', True)]}"/>
</group> </group>
<group colspan="1"> <group colspan="1">
<field name="log_read"/> <field name="log_read"/>

Loading…
Cancel
Save