Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions auditlog/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Contributors
* Holden Rehg <holdenrehg@gmail.com>
* Bhavesh Odedra <bodedra@opensourceintegrators.com>
* Hardik Suthar <hsuthar@opensourceintegrators.com>
* Dennis Sluijk <d.sluijk@onestein.nl>

Other credits
~~~~~~~~~~~~~
Expand Down
11 changes: 11 additions & 0 deletions auditlog/models/log.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2015 ABF OSIELL <https://osiell.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models, fields
from odoo.tools.safe_eval import safe_eval


class AuditlogLog(models.Model):
Expand All @@ -12,6 +13,7 @@ class AuditlogLog(models.Model):
model_id = fields.Many2one(
'ir.model', string="Model")
res_id = fields.Integer("Resource ID")
res_ids = fields.Char("Resource IDs")
user_id = fields.Many2one(
'res.users', string="User")
method = fields.Char("Method", size=64)
Expand All @@ -27,6 +29,15 @@ class AuditlogLog(models.Model):
],
string="Type")

def show_res_ids(self):
self.ensure_one()
return {
"type": "ir.actions.act_window",
"view_mode": "tree,form",
"res_model": self.model_id.model,
"domain": [("id", "in", safe_eval(self.res_ids))],
}


class AuditlogLogLine(models.Model):
_name = 'auditlog.log.line'
Expand Down
71 changes: 55 additions & 16 deletions auditlog/models/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ class AuditlogRule(models.Model):
help=("Select this if you want to keep track of creation on any "
"record of the model of this rule"),
states={'subscribed': [('readonly', True)]})
log_export_data = fields.Boolean(
"Log Exports",
default=True,
help=(
"Select this if you want to keep track of exports "
"of the model of this rule"
),
states={"subscribed": [("readonly", True)]},
)
log_type = fields.Selection(
[('full', "Full log"),
('fast', "Fast log"),
Expand Down Expand Up @@ -179,6 +188,12 @@ def _patch_methods(self):
model_model._patch_method('unlink', rule._make_unlink())
setattr(type(model_model), check_attr, True)
updated = True
# -> export_data
check_attr = 'auditlog_ruled_export_data'
if rule.log_export_data and not hasattr(model_model, check_attr):
model_model._patch_method('export_data', rule._make_export_data())
setattr(type(model_model), check_attr, True)
updated = True
return updated

@api.multi
Expand All @@ -187,7 +202,7 @@ def _revert_methods(self):
updated = False
for rule in self:
model_model = self.env[rule.model_id.model]
for method in ['create', 'read', 'write', 'unlink']:
for method in ['create', 'read', 'write', 'unlink', 'export_data']:
if getattr(rule, 'log_%s' % method) and hasattr(
getattr(model_model, method), 'origin'):
model_model._revert_method(method)
Expand Down Expand Up @@ -231,6 +246,28 @@ def get_auditlog_fields(self, model):
if (not f.compute and not f.related) or f.store
)

def _make_export_data(self):
"""Instanciate a export method that log its calls."""
self.ensure_one()
log_type = self.log_type

def export_data(self, fields_to_export, raw_data=False):
res = export_data.origin(self, fields_to_export, raw_data)
self = self.with_context(auditlog_disabled=True)
rule_model = self.env["auditlog.rule"]
rule_model.sudo().create_logs(
self.env.uid,
self._name,
self.ids,
"export_data",
None,
None,
{"log_type": log_type},
)
return res

return export_data

@api.multi
def _make_create(self):
"""Instanciate a create method that log its calls."""
Expand Down Expand Up @@ -393,24 +430,26 @@ def create_logs(self, uid, res_model, res_ids, method,
log_model = self.env['auditlog.log']
http_request_model = self.env['auditlog.http.request']
http_session_model = self.env['auditlog.http.session']
model_model = self.env[res_model]
model_id = self.pool._auditlog_model_cache[res_model]
auditlog_rule = self.env['auditlog.rule'].search([('model_id', '=', model_id)])
vals = {
'model_id': model_id,
'method': method,
'user_id': uid,
'http_request_id': http_request_model.current_http_request(),
'http_session_id': http_session_model.current_http_session(),
}
vals.update(additional_log_values or {})
if method == 'export_data':
vals.update({'name': res_model, 'res_ids': str(res_ids)})
return log_model.create(vals)

for res_id in res_ids:
model_model = self.env[res_model]
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]
vals = {
'name': res_name,
'model_id': self.pool._auditlog_model_cache[res_model],
'res_id': res_id,
'method': method,
'user_id': uid,
'http_request_id': http_request_model.current_http_request(),
'http_session_id': http_session_model.current_http_session(),
}
vals.update(additional_log_values or {})
log = log_model.create(vals)
log_vals = {**vals, 'name': res_name, 'res_id': res_id}
log = log_model.create(log_vals)
diff = DictDiffer(
new_values.get(res_id, EMPTY_DICT),
old_values.get(res_id, EMPTY_DICT))
Expand Down
1 change: 1 addition & 0 deletions auditlog/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* Holden Rehg <holdenrehg@gmail.com>
* Bhavesh Odedra <bodedra@opensourceintegrators.com>
* Hardik Suthar <hsuthar@opensourceintegrators.com>
* Dennis Sluijk <d.sluijk@onestein.nl>
2 changes: 2 additions & 0 deletions auditlog/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -460,6 +461,7 @@ <h2><a class="toc-backref" href="#toc-entry-10">Contributors</a></h2>
<li>Holden Rehg &lt;<a class="reference external" href="mailto:holdenrehg&#64;gmail.com">holdenrehg&#64;gmail.com</a>&gt;</li>
<li>Bhavesh Odedra &lt;<a class="reference external" href="mailto:bodedra&#64;opensourceintegrators.com">bodedra&#64;opensourceintegrators.com</a>&gt;</li>
<li>Hardik Suthar &lt;<a class="reference external" href="mailto:hsuthar&#64;opensourceintegrators.com">hsuthar&#64;opensourceintegrators.com</a>&gt;</li>
<li>Dennis Sluijk &lt;<a class="reference external" href="mailto:d.sluijk&#64;onestein.nl">d.sluijk&#64;onestein.nl</a>&gt;</li>
</ul>
</div>
<div class="section" id="other-credits">
Expand Down
17 changes: 17 additions & 0 deletions auditlog/tests/test_auditlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ def test_LogDuringUpdate(self):
]))
sql.rename_column(self.env.cr, Rule._table, temp_name, field_name)

def test_LogExport(self):
self.groups_rule.subscribe()

auditlog_log = self.env["auditlog.log"]
self.env["res.groups"].search([]).export_data(["name"])
created_log = auditlog_log.search(
[
("model_id", "=", self.groups_model_id),
("method", "=", "export_data"),
]
).ensure_one()
self.assertTrue(created_log)
action = created_log.show_res_ids()
domain = action["domain"] # [('id', 'in', [1, 2, ...])]
self.assertIsInstance(domain, list)
self.assertIsInstance(domain[0][2], list)


class TestAuditlogFull(TransactionCase, AuditlogCommon):

Expand Down
23 changes: 22 additions & 1 deletion auditlog/views/auditlog_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<field name="log_write"/>
<field name="log_unlink"/>
<field name="log_create"/>
<field name="log_export_data"/>
</group>
</group>
</sheet>
Expand All @@ -52,6 +53,7 @@
<field name="log_write"/>
<field name="log_unlink"/>
<field name="log_create"/>
<field name="log_export_data"/>
<field name="state"/>
</tree>
</field>
Expand Down Expand Up @@ -97,6 +99,16 @@
<field name="arch" type="xml">
<form string="Log">
<sheet>
<div class="oe_button_box" name="button_box">
<button
name="show_res_ids"
type="object"
class="oe_stat_button"
attrs="{'invisible': [('res_ids', '=', False)]}"
icon="fa-external-link"
string="Exported Records"
/>
</div>
<group string="Log">
<group colspan="1">
<field name="create_date" readonly="1"/>
Expand All @@ -106,7 +118,16 @@
</group>
<group colspan="1">
<field name="model_id" readonly="1"/>
<field name="res_id" readonly="1"/>
<field
name="res_id"
readonly="1"
attrs="{'invisible': [('res_id', '=', 0)]}"
/>
<field
name="res_ids"
readonly="1"
attrs="{'invisible': [('res_ids', '=', False)]}"
/>
<field name="name" readonly="1"/>
</group>
</group>
Expand Down
Loading