2009-10-13 05:58:37 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2008-11-03 19:18:56 +00:00
|
|
|
|
##############################################################################
|
2009-11-30 06:07:41 +00:00
|
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
|
# OpenERP, Open Source Management Solution
|
2010-01-12 09:18:39 +00:00
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2008-11-03 19:18:56 +00:00
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 11:15:34 +00:00
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
2008-11-03 19:18:56 +00:00
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 11:15:34 +00:00
|
|
|
|
# GNU Affero General Public License for more details.
|
2008-11-03 19:18:56 +00:00
|
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2009-11-30 06:07:41 +00:00
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-11-03 19:18:56 +00:00
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
2008-09-16 11:45:13 +00:00
|
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp.osv import fields, osv
|
2012-12-17 15:46:28 +00:00
|
|
|
|
from openerp.osv.osv import object_proxy
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp.tools.translate import _
|
|
|
|
|
from openerp import pooler
|
2010-03-17 08:56:10 +00:00
|
|
|
|
import time
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp import tools
|
2012-08-31 13:51:36 +00:00
|
|
|
|
from openerp import SUPERUSER_ID
|
2008-11-07 13:25:56 +00:00
|
|
|
|
|
2008-09-16 11:45:13 +00:00
|
|
|
|
class audittrail_rule(osv.osv):
|
2010-03-17 05:03:01 +00:00
|
|
|
|
"""
|
2010-03-18 09:12:15 +00:00
|
|
|
|
For Auddittrail Rule
|
2010-03-17 05:03:01 +00:00
|
|
|
|
"""
|
2008-09-16 11:45:13 +00:00
|
|
|
|
_name = 'audittrail.rule'
|
2010-03-17 05:03:01 +00:00
|
|
|
|
_description = "Audittrail Rule"
|
2008-09-16 11:45:13 +00:00
|
|
|
|
_columns = {
|
2010-06-24 06:03:46 +00:00
|
|
|
|
"name": fields.char("Rule Name", size=32, required=True),
|
2010-09-15 12:27:23 +00:00
|
|
|
|
"object_id": fields.many2one('ir.model', 'Object', required=True, help="Select object for which you want to generate log."),
|
2010-06-24 06:03:46 +00:00
|
|
|
|
"user_id": fields.many2many('res.users', 'audittail_rules_users',
|
|
|
|
|
'user_id', 'rule_id', 'Users', help="if User is not added then it will applicable for all users"),
|
2010-09-21 06:24:11 +00:00
|
|
|
|
"log_read": fields.boolean("Log Reads", help="Select this if you want to keep track of read/open on any record of the object of this rule"),
|
|
|
|
|
"log_write": fields.boolean("Log Writes", help="Select this if you want to keep track of modification on any record of the object of this rule"),
|
|
|
|
|
"log_unlink": fields.boolean("Log Deletes", help="Select this if you want to keep track of deletion on any record of the object of this rule"),
|
|
|
|
|
"log_create": fields.boolean("Log Creates",help="Select this if you want to keep track of creation on any record of the object of this rule"),
|
|
|
|
|
"log_action": fields.boolean("Log Action",help="Select this if you want to keep track of actions on the object of this rule"),
|
|
|
|
|
"log_workflow": fields.boolean("Log Workflow",help="Select this if you want to keep track of workflow on any record of the object of this rule"),
|
2012-05-04 11:57:48 +00:00
|
|
|
|
"state": fields.selection((("draft", "Draft"), ("subscribed", "Subscribed")), "Status", required=True),
|
2010-06-24 06:03:46 +00:00
|
|
|
|
"action_id": fields.many2one('ir.actions.act_window', "Action ID"),
|
2008-09-16 11:45:13 +00:00
|
|
|
|
}
|
|
|
|
|
_defaults = {
|
2011-11-17 17:50:34 +00:00
|
|
|
|
'state': 'draft',
|
|
|
|
|
'log_create': 1,
|
|
|
|
|
'log_unlink': 1,
|
|
|
|
|
'log_write': 1,
|
2008-09-16 11:45:13 +00:00
|
|
|
|
}
|
|
|
|
|
_sql_constraints = [
|
2011-11-08 16:55:34 +00:00
|
|
|
|
('model_uniq', 'unique (object_id)', """There is already a rule defined on this object\n You cannot define another: please edit the existing one.""")
|
2008-09-16 11:45:13 +00:00
|
|
|
|
]
|
|
|
|
|
__functions = {}
|
|
|
|
|
|
|
|
|
|
def subscribe(self, cr, uid, ids, *args):
|
2010-03-17 08:56:10 +00:00
|
|
|
|
"""
|
2010-03-18 09:12:15 +00:00
|
|
|
|
Subscribe Rule for auditing changes on object and apply shortcut for logs on that object.
|
2010-03-17 08:56:10 +00:00
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
2010-03-18 09:12:15 +00:00
|
|
|
|
@param ids: List of Auddittrail Rule’s IDs.
|
2010-06-24 06:03:46 +00:00
|
|
|
|
@return: True
|
2010-03-17 08:56:10 +00:00
|
|
|
|
"""
|
2010-03-17 05:03:01 +00:00
|
|
|
|
obj_action = self.pool.get('ir.actions.act_window')
|
|
|
|
|
obj_model = self.pool.get('ir.model.data')
|
|
|
|
|
#start Loop
|
2008-09-16 11:45:13 +00:00
|
|
|
|
for thisrule in self.browse(cr, uid, ids):
|
|
|
|
|
obj = self.pool.get(thisrule.object_id.model)
|
|
|
|
|
if not obj:
|
|
|
|
|
raise osv.except_osv(
|
2010-06-24 06:03:46 +00:00
|
|
|
|
_('WARNING: audittrail is not part of the pool'),
|
2009-03-06 22:18:24 +00:00
|
|
|
|
_('Change audittrail depends -- Setting rule as DRAFT'))
|
2008-09-16 11:45:13 +00:00
|
|
|
|
self.write(cr, uid, [thisrule.id], {"state": "draft"})
|
2010-03-17 05:03:01 +00:00
|
|
|
|
val = {
|
2010-06-24 06:03:46 +00:00
|
|
|
|
"name": 'View Log',
|
|
|
|
|
"res_model": 'audittrail.log',
|
|
|
|
|
"src_model": thisrule.object_id.model,
|
2010-03-17 09:45:10 +00:00
|
|
|
|
"domain": "[('object_id','=', " + str(thisrule.object_id.id) + "), ('res_id', '=', active_id)]"
|
2009-11-30 06:07:41 +00:00
|
|
|
|
|
2009-10-24 09:14:21 +00:00
|
|
|
|
}
|
2012-11-30 16:54:15 +00:00
|
|
|
|
action_id = obj_action.create(cr, SUPERUSER_ID, val)
|
2010-06-24 06:03:46 +00:00
|
|
|
|
self.write(cr, uid, [thisrule.id], {"state": "subscribed", "action_id": action_id})
|
2009-10-24 09:14:21 +00:00
|
|
|
|
keyword = 'client_action_relate'
|
2010-06-24 06:03:46 +00:00
|
|
|
|
value = 'ir.actions.act_window,' + str(action_id)
|
2012-11-30 16:54:15 +00:00
|
|
|
|
res = obj_model.ir_set(cr, SUPERUSER_ID, 'action', keyword, 'View_log_' + thisrule.object_id.model, [thisrule.object_id.model], value, replace=True, isobject=True, xml_id=False)
|
2010-03-17 05:03:01 +00:00
|
|
|
|
#End Loop
|
2008-09-16 11:45:13 +00:00
|
|
|
|
return True
|
|
|
|
|
|
2008-11-07 13:25:56 +00:00
|
|
|
|
def unsubscribe(self, cr, uid, ids, *args):
|
2010-03-17 08:56:10 +00:00
|
|
|
|
"""
|
2010-03-18 09:12:15 +00:00
|
|
|
|
Unsubscribe Auditing Rule on object
|
2010-03-17 08:56:10 +00:00
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
2010-03-18 09:12:15 +00:00
|
|
|
|
@param ids: List of Auddittrail Rule’s IDs.
|
2010-06-24 06:03:46 +00:00
|
|
|
|
@return: True
|
2010-03-17 08:56:10 +00:00
|
|
|
|
"""
|
2010-03-17 05:03:01 +00:00
|
|
|
|
obj_action = self.pool.get('ir.actions.act_window')
|
2011-05-16 15:33:31 +00:00
|
|
|
|
ir_values_obj = self.pool.get('ir.values')
|
2010-12-09 06:53:24 +00:00
|
|
|
|
value=''
|
2010-03-17 05:03:01 +00:00
|
|
|
|
#start Loop
|
2008-11-07 13:25:56 +00:00
|
|
|
|
for thisrule in self.browse(cr, uid, ids):
|
2010-03-17 09:45:10 +00:00
|
|
|
|
if thisrule.id in self.__functions:
|
2008-11-07 13:25:56 +00:00
|
|
|
|
for function in self.__functions[thisrule.id]:
|
|
|
|
|
setattr(function[0], function[1], function[2])
|
2010-03-17 05:03:01 +00:00
|
|
|
|
w_id = obj_action.search(cr, uid, [('name', '=', 'View Log'), ('res_model', '=', 'audittrail.log'), ('src_model', '=', thisrule.object_id.model)])
|
2010-12-09 06:53:24 +00:00
|
|
|
|
if w_id:
|
2012-11-30 16:54:15 +00:00
|
|
|
|
obj_action.unlink(cr, SUPERUSER_ID, w_id)
|
2010-12-09 06:53:24 +00:00
|
|
|
|
value = "ir.actions.act_window" + ',' + str(w_id[0])
|
2011-05-16 15:33:31 +00:00
|
|
|
|
val_id = ir_values_obj.search(cr, uid, [('model', '=', thisrule.object_id.model), ('value', '=', value)])
|
2009-10-24 09:14:21 +00:00
|
|
|
|
if val_id:
|
2011-05-19 12:13:36 +00:00
|
|
|
|
ir_values_obj = pooler.get_pool(cr.dbname).get('ir.values')
|
|
|
|
|
res = ir_values_obj.unlink(cr, uid, [val_id[0]])
|
2009-10-24 09:14:21 +00:00
|
|
|
|
self.write(cr, uid, [thisrule.id], {"state": "draft"})
|
2010-03-17 05:03:01 +00:00
|
|
|
|
#End Loop
|
2008-11-07 13:25:56 +00:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
class audittrail_log(osv.osv):
|
2010-03-17 05:03:01 +00:00
|
|
|
|
"""
|
|
|
|
|
For Audittrail Log
|
|
|
|
|
"""
|
2008-11-07 13:25:56 +00:00
|
|
|
|
_name = 'audittrail.log'
|
2010-03-17 05:03:01 +00:00
|
|
|
|
_description = "Audittrail Log"
|
2010-03-18 09:12:15 +00:00
|
|
|
|
|
2010-06-24 06:03:46 +00:00
|
|
|
|
def _name_get_resname(self, cr, uid, ids, *args):
|
|
|
|
|
data = {}
|
|
|
|
|
for resname in self.browse(cr, uid, ids,[]):
|
|
|
|
|
model_object = resname.object_id
|
|
|
|
|
res_id = resname.res_id
|
|
|
|
|
if model_object and res_id:
|
|
|
|
|
model_pool = self.pool.get(model_object.model)
|
|
|
|
|
res = model_pool.read(cr, uid, res_id, ['name'])
|
|
|
|
|
data[resname.id] = res['name']
|
|
|
|
|
else:
|
|
|
|
|
data[resname.id] = False
|
|
|
|
|
return data
|
|
|
|
|
|
2008-11-07 13:25:56 +00:00
|
|
|
|
_columns = {
|
2010-06-24 06:03:46 +00:00
|
|
|
|
"name": fields.char("Resource Name",size=64),
|
|
|
|
|
"object_id": fields.many2one('ir.model', 'Object'),
|
|
|
|
|
"user_id": fields.many2one('res.users', 'User'),
|
|
|
|
|
"method": fields.char("Method", size=64),
|
|
|
|
|
"timestamp": fields.datetime("Date"),
|
|
|
|
|
"res_id": fields.integer('Resource Id'),
|
|
|
|
|
"line_ids": fields.one2many('audittrail.log.line', 'log_id', 'Log lines'),
|
2008-11-07 13:25:56 +00:00
|
|
|
|
}
|
2010-03-18 09:12:15 +00:00
|
|
|
|
|
2008-11-07 13:25:56 +00:00
|
|
|
|
_defaults = {
|
|
|
|
|
"timestamp": lambda *a: time.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
}
|
2009-08-26 12:56:01 +00:00
|
|
|
|
_order = "timestamp desc"
|
2008-11-07 13:25:56 +00:00
|
|
|
|
|
|
|
|
|
class audittrail_log_line(osv.osv):
|
2010-03-17 05:03:01 +00:00
|
|
|
|
"""
|
|
|
|
|
Audittrail Log Line.
|
|
|
|
|
"""
|
|
|
|
|
_name = 'audittrail.log.line'
|
|
|
|
|
_description = "Log Line"
|
|
|
|
|
_columns = {
|
2010-06-24 06:03:46 +00:00
|
|
|
|
'field_id': fields.many2one('ir.model.fields', 'Fields', required=True),
|
|
|
|
|
'log_id': fields.many2one('audittrail.log', 'Log'),
|
|
|
|
|
'log': fields.integer("Log ID"),
|
|
|
|
|
'old_value': fields.text("Old Value"),
|
|
|
|
|
'new_value': fields.text("New Value"),
|
|
|
|
|
'old_value_text': fields.text('Old value Text'),
|
|
|
|
|
'new_value_text': fields.text('New value Text'),
|
|
|
|
|
'field_description': fields.char('Field Description', size=64),
|
2010-03-17 05:03:01 +00:00
|
|
|
|
}
|
2008-11-07 13:25:56 +00:00
|
|
|
|
|
2010-12-30 12:01:37 +00:00
|
|
|
|
class audittrail_objects_proxy(object_proxy):
|
2010-03-18 09:12:15 +00:00
|
|
|
|
""" Uses Object proxy for auditing changes on object of subscribed Rules"""
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2011-11-21 16:10:51 +00:00
|
|
|
|
def get_value_text(self, cr, uid, pool, resource_pool, method, field, value):
|
2010-03-18 09:12:15 +00:00
|
|
|
|
"""
|
2011-11-21 14:34:50 +00:00
|
|
|
|
Gets textual values for the fields.
|
|
|
|
|
If the field is a many2one, it returns the name.
|
|
|
|
|
If it's a one2many or a many2many, it returns a list of name.
|
2011-11-08 16:55:34 +00:00
|
|
|
|
In other cases, it just returns the value.
|
2011-11-17 17:50:34 +00:00
|
|
|
|
:param cr: the current row, from the database cursor,
|
|
|
|
|
:param uid: the current user’s ID for security checks,
|
|
|
|
|
:param pool: current db's pooler object.
|
|
|
|
|
:param resource_pool: pooler object of the model which values are being changed.
|
|
|
|
|
:param field: for which the text value is to be returned.
|
|
|
|
|
:param value: value of the field.
|
2011-11-21 14:34:50 +00:00
|
|
|
|
:param recursive: True or False, True will repeat the process recursively
|
2011-11-17 17:50:34 +00:00
|
|
|
|
:return: string value or a list of values(for O2M/M2M)
|
2010-03-18 09:12:15 +00:00
|
|
|
|
"""
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2011-11-08 16:55:34 +00:00
|
|
|
|
field_obj = (resource_pool._all_columns.get(field)).column
|
|
|
|
|
if field_obj._type in ('one2many','many2many'):
|
|
|
|
|
data = pool.get(field_obj._obj).name_get(cr, uid, value)
|
2011-11-17 07:09:05 +00:00
|
|
|
|
#return the modifications on x2many fields as a list of names
|
2011-11-21 14:34:50 +00:00
|
|
|
|
res = map(lambda x:x[1], data)
|
2011-11-08 16:55:34 +00:00
|
|
|
|
elif field_obj._type == 'many2one':
|
2011-11-17 17:50:34 +00:00
|
|
|
|
#return the modifications on a many2one field as its value returned by name_get()
|
2011-11-21 14:34:50 +00:00
|
|
|
|
res = value and value[1] or value
|
|
|
|
|
else:
|
|
|
|
|
res = value
|
|
|
|
|
return res
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2012-03-05 18:40:03 +00:00
|
|
|
|
def create_log_line(self, cr, uid, log_id, model, lines=None):
|
2010-03-18 09:12:15 +00:00
|
|
|
|
"""
|
|
|
|
|
Creates lines for changed fields with its old and new values
|
|
|
|
|
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
2011-11-17 17:50:34 +00:00
|
|
|
|
@param model: Object which values are being changed
|
2010-03-18 09:12:15 +00:00
|
|
|
|
@param lines: List of values for line is to be created
|
|
|
|
|
"""
|
2012-03-05 18:40:03 +00:00
|
|
|
|
if lines is None:
|
|
|
|
|
lines = []
|
2008-11-07 13:25:56 +00:00
|
|
|
|
pool = pooler.get_pool(cr.dbname)
|
2010-06-24 06:03:46 +00:00
|
|
|
|
obj_pool = pool.get(model.model)
|
|
|
|
|
model_pool = pool.get('ir.model')
|
|
|
|
|
field_pool = pool.get('ir.model.fields')
|
|
|
|
|
log_line_pool = pool.get('audittrail.log.line')
|
2008-11-07 13:25:56 +00:00
|
|
|
|
for line in lines:
|
2011-09-06 06:22:41 +00:00
|
|
|
|
field_obj = obj_pool._all_columns.get(line['name'])
|
2011-09-05 09:38:41 +00:00
|
|
|
|
assert field_obj, _("'%s' field does not exist in '%s' model" %(line['name'], model.model))
|
|
|
|
|
field_obj = field_obj.column
|
|
|
|
|
old_value = line.get('old_value', '')
|
|
|
|
|
new_value = line.get('new_value', '')
|
2011-11-08 16:55:34 +00:00
|
|
|
|
search_models = [model.id]
|
2010-06-24 06:03:46 +00:00
|
|
|
|
if obj_pool._inherits:
|
2011-09-05 09:38:41 +00:00
|
|
|
|
search_models += model_pool.search(cr, uid, [('model', 'in', obj_pool._inherits.keys())])
|
|
|
|
|
field_id = field_pool.search(cr, uid, [('name', '=', line['name']), ('model_id', 'in', search_models)])
|
|
|
|
|
if field_obj._type == 'many2one':
|
2011-09-06 06:54:16 +00:00
|
|
|
|
old_value = old_value and old_value[0] or old_value
|
|
|
|
|
new_value = new_value and new_value[0] or new_value
|
2010-06-24 06:03:46 +00:00
|
|
|
|
vals = {
|
|
|
|
|
"log_id": log_id,
|
2011-09-05 09:38:41 +00:00
|
|
|
|
"field_id": field_id and field_id[0] or False,
|
2010-06-24 06:03:46 +00:00
|
|
|
|
"old_value": old_value,
|
|
|
|
|
"new_value": new_value,
|
2011-11-09 10:27:29 +00:00
|
|
|
|
"old_value_text": line.get('old_value_text', ''),
|
|
|
|
|
"new_value_text": line.get('new_value_text', ''),
|
2011-09-05 09:38:41 +00:00
|
|
|
|
"field_description": field_obj.string
|
2010-06-24 06:03:46 +00:00
|
|
|
|
}
|
|
|
|
|
line_id = log_line_pool.create(cr, uid, vals)
|
2008-11-07 13:25:56 +00:00
|
|
|
|
return True
|
2008-09-16 11:45:13 +00:00
|
|
|
|
|
2012-03-06 17:46:20 +00:00
|
|
|
|
def log_fct(self, cr, uid_orig, model, method, fct_src, *args, **kw):
|
2010-03-18 09:12:15 +00:00
|
|
|
|
"""
|
2011-11-08 16:55:34 +00:00
|
|
|
|
Logging function: This function is performing the logging operation
|
2011-11-17 07:25:18 +00:00
|
|
|
|
@param model: Object whose values are being changed
|
2011-11-21 16:20:32 +00:00
|
|
|
|
@param method: method to log: create, read, write, unlink, action or workflow action
|
2010-03-18 09:12:15 +00:00
|
|
|
|
@param fct_src: execute method of Object proxy
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
|
|
|
|
@return: Returns result as per method of Object proxy
|
2010-03-18 09:12:15 +00:00
|
|
|
|
"""
|
2011-09-29 22:23:47 +00:00
|
|
|
|
pool = pooler.get_pool(cr.dbname)
|
2010-06-24 06:03:46 +00:00
|
|
|
|
resource_pool = pool.get(model)
|
|
|
|
|
model_pool = pool.get('ir.model')
|
2012-08-31 13:51:36 +00:00
|
|
|
|
model_ids = model_pool.search(cr, SUPERUSER_ID, [('model', '=', model)])
|
2010-06-24 06:03:46 +00:00
|
|
|
|
model_id = model_ids and model_ids[0] or False
|
2010-09-14 14:58:33 +00:00
|
|
|
|
assert model_id, _("'%s' Model does not exist..." %(model))
|
2012-08-31 13:51:36 +00:00
|
|
|
|
model = model_pool.browse(cr, SUPERUSER_ID, model_id)
|
2011-11-21 16:20:32 +00:00
|
|
|
|
|
|
|
|
|
# fields to log. currently only used by log on read()
|
2011-11-21 16:10:51 +00:00
|
|
|
|
field_list = []
|
2011-11-21 16:20:32 +00:00
|
|
|
|
old_values = new_values = {}
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2011-09-06 12:27:35 +00:00
|
|
|
|
if method == 'create':
|
2012-03-06 17:46:20 +00:00
|
|
|
|
res = fct_src(cr, uid_orig, model.model, method, *args, **kw)
|
2011-11-18 20:27:13 +00:00
|
|
|
|
if res:
|
2011-11-21 16:10:51 +00:00
|
|
|
|
res_ids = [res]
|
|
|
|
|
new_values = self.get_data(cr, uid_orig, pool, res_ids, model, method)
|
2011-11-21 14:34:50 +00:00
|
|
|
|
elif method == 'read':
|
2012-03-06 17:46:20 +00:00
|
|
|
|
res = fct_src(cr, uid_orig, model.model, method, *args, **kw)
|
2011-11-21 16:20:32 +00:00
|
|
|
|
# build the res_ids and the old_values dict. Here we don't use get_data() to
|
|
|
|
|
# avoid performing an additional read()
|
2011-11-21 16:10:51 +00:00
|
|
|
|
res_ids = []
|
|
|
|
|
for record in res:
|
|
|
|
|
res_ids.append(record['id'])
|
|
|
|
|
old_values[(model.id, record['id'])] = {'value': record, 'text': record}
|
2011-11-21 16:20:32 +00:00
|
|
|
|
# log only the fields read
|
|
|
|
|
field_list = args[1]
|
2011-11-21 14:34:50 +00:00
|
|
|
|
elif method == 'unlink':
|
2010-03-17 05:03:01 +00:00
|
|
|
|
res_ids = args[0]
|
2011-11-21 16:10:51 +00:00
|
|
|
|
old_values = self.get_data(cr, uid_orig, pool, res_ids, model, method)
|
2012-03-06 17:46:20 +00:00
|
|
|
|
res = fct_src(cr, uid_orig, model.model, method, *args, **kw)
|
2011-11-21 16:20:32 +00:00
|
|
|
|
else: # method is write, action or workflow action
|
2011-03-21 10:16:37 +00:00
|
|
|
|
res_ids = []
|
|
|
|
|
if args:
|
|
|
|
|
res_ids = args[0]
|
2011-09-06 06:22:41 +00:00
|
|
|
|
if isinstance(res_ids, (long, int)):
|
2011-03-21 10:16:37 +00:00
|
|
|
|
res_ids = [res_ids]
|
2010-06-24 06:03:46 +00:00
|
|
|
|
if res_ids:
|
2011-11-21 16:20:32 +00:00
|
|
|
|
# store the old values into a dictionary
|
2011-11-18 20:27:13 +00:00
|
|
|
|
old_values = self.get_data(cr, uid_orig, pool, res_ids, model, method)
|
2011-11-21 16:20:32 +00:00
|
|
|
|
# process the original function, workflow trigger...
|
2012-03-06 17:46:20 +00:00
|
|
|
|
res = fct_src(cr, uid_orig, model.model, method, *args, **kw)
|
2011-11-17 07:09:05 +00:00
|
|
|
|
if method == 'copy':
|
|
|
|
|
res_ids = [res]
|
2010-06-24 06:03:46 +00:00
|
|
|
|
if res_ids:
|
2011-11-21 16:20:32 +00:00
|
|
|
|
# check the new values and store them into a dictionary
|
2011-11-18 20:27:13 +00:00
|
|
|
|
new_values = self.get_data(cr, uid_orig, pool, res_ids, model, method)
|
2011-11-21 16:20:32 +00:00
|
|
|
|
# compare the old and new values and create audittrail log if needed
|
2011-11-21 16:10:51 +00:00
|
|
|
|
self.process_data(cr, uid_orig, pool, res_ids, model, method, old_values, new_values, field_list)
|
2011-11-18 20:27:13 +00:00
|
|
|
|
return res
|
|
|
|
|
|
2012-11-16 10:49:36 +00:00
|
|
|
|
def get_data(self, cr, uid, pool, res_ids, model, method):
|
2011-11-18 20:27:13 +00:00
|
|
|
|
"""
|
2011-11-21 14:34:50 +00:00
|
|
|
|
This function simply read all the fields of the given res_ids, and also recurisvely on
|
|
|
|
|
all records of a x2m fields read that need to be logged. Then it returns the result in
|
|
|
|
|
convenient structure that will be used as comparison basis.
|
2011-11-18 20:27:13 +00:00
|
|
|
|
|
|
|
|
|
:param cr: the current row, from the database cursor,
|
2011-11-21 14:34:50 +00:00
|
|
|
|
:param uid: the current user’s ID. This parameter is currently not used as every
|
|
|
|
|
operation to get data is made as super admin. Though, it could be usefull later.
|
2011-11-18 20:27:13 +00:00
|
|
|
|
:param pool: current db's pooler object.
|
|
|
|
|
:param res_ids: Id's of resource to be logged/compared.
|
|
|
|
|
:param model: Object whose values are being changed
|
2011-11-21 14:34:50 +00:00
|
|
|
|
:param method: method to log: create, read, unlink, write, actions, workflow actions
|
2011-11-18 20:27:13 +00:00
|
|
|
|
:return: dict mapping a tuple (model_id, resource_id) with its value and textual value
|
|
|
|
|
{ (model_id, resource_id): { 'value': ...
|
|
|
|
|
'textual_value': ...
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
data = {}
|
|
|
|
|
resource_pool = pool.get(model.model)
|
2011-11-21 14:34:50 +00:00
|
|
|
|
# read all the fields of the given resources in super admin mode
|
2012-08-31 13:51:36 +00:00
|
|
|
|
for resource in resource_pool.read(cr, SUPERUSER_ID, res_ids):
|
2011-11-18 20:27:13 +00:00
|
|
|
|
values = {}
|
2011-11-21 14:34:50 +00:00
|
|
|
|
values_text = {}
|
2011-11-18 20:27:13 +00:00
|
|
|
|
resource_id = resource['id']
|
2011-11-21 14:34:50 +00:00
|
|
|
|
# loop on each field on the res_ids we just have read
|
2011-11-18 20:27:13 +00:00
|
|
|
|
for field in resource:
|
|
|
|
|
if field in ('__last_update', 'id'):
|
|
|
|
|
continue
|
|
|
|
|
values[field] = resource[field]
|
2011-11-21 14:34:50 +00:00
|
|
|
|
# get the textual value of that field for this record
|
2012-08-31 13:51:36 +00:00
|
|
|
|
values_text[field] = self.get_value_text(cr, SUPERUSER_ID, pool, resource_pool, method, field, resource[field])
|
2011-11-21 14:34:50 +00:00
|
|
|
|
|
2011-11-18 20:27:13 +00:00
|
|
|
|
field_obj = resource_pool._all_columns.get(field).column
|
|
|
|
|
if field_obj._type in ('one2many','many2many'):
|
2011-11-21 14:34:50 +00:00
|
|
|
|
# check if an audittrail rule apply in super admin mode
|
2012-08-31 13:51:36 +00:00
|
|
|
|
if self.check_rules(cr, SUPERUSER_ID, field_obj._obj, method):
|
2011-11-21 14:34:50 +00:00
|
|
|
|
# check if the model associated to a *2m field exists, in super admin mode
|
2012-08-31 13:51:36 +00:00
|
|
|
|
x2m_model_ids = pool.get('ir.model').search(cr, SUPERUSER_ID, [('model', '=', field_obj._obj)])
|
2011-11-18 20:27:13 +00:00
|
|
|
|
x2m_model_id = x2m_model_ids and x2m_model_ids[0] or False
|
|
|
|
|
assert x2m_model_id, _("'%s' Model does not exist..." %(field_obj._obj))
|
2012-08-31 13:51:36 +00:00
|
|
|
|
x2m_model = pool.get('ir.model').browse(cr, SUPERUSER_ID, x2m_model_id)
|
2012-10-31 07:02:07 +00:00
|
|
|
|
field_resource_ids = list(set(resource[field]))
|
|
|
|
|
if model.model == x2m_model.model:
|
|
|
|
|
# we need to remove current resource_id from the many2many to prevent an infinit loop
|
|
|
|
|
if resource_id in field_resource_ids:
|
|
|
|
|
field_resource_ids.remove(resource_id)
|
2012-11-16 10:49:36 +00:00
|
|
|
|
data.update(self.get_data(cr, SUPERUSER_ID, pool, field_resource_ids, x2m_model, method))
|
|
|
|
|
|
2011-11-21 14:34:50 +00:00
|
|
|
|
data[(model.id, resource_id)] = {'text':values_text, 'value': values}
|
2011-11-18 20:27:13 +00:00
|
|
|
|
return data
|
|
|
|
|
|
2012-11-16 10:49:36 +00:00
|
|
|
|
def prepare_audittrail_log_line(self, cr, uid, pool, model, resource_id, method, old_values, new_values, field_list=None):
|
2011-11-21 13:33:20 +00:00
|
|
|
|
"""
|
2012-10-02 11:12:31 +00:00
|
|
|
|
This function compares the old data (i.e before the method was executed) and the new data
|
2011-11-21 13:33:20 +00:00
|
|
|
|
(after the method was executed) and returns a structure with all the needed information to
|
|
|
|
|
log those differences.
|
|
|
|
|
|
|
|
|
|
:param cr: the current row, from the database cursor,
|
2011-11-21 14:34:50 +00:00
|
|
|
|
:param uid: the current user’s ID. This parameter is currently not used as every
|
|
|
|
|
operation to get data is made as super admin. Though, it could be usefull later.
|
2011-11-21 13:33:20 +00:00
|
|
|
|
:param pool: current db's pooler object.
|
|
|
|
|
:param model: model object which values are being changed
|
2011-11-21 14:34:50 +00:00
|
|
|
|
:param resource_id: ID of record to which values are being changed
|
2011-11-21 13:33:20 +00:00
|
|
|
|
:param method: method to log: create, read, unlink, write, actions, workflow actions
|
|
|
|
|
:param old_values: dict of values read before execution of the method
|
|
|
|
|
:param new_values: dict of values read after execution of the method
|
2011-11-21 16:10:51 +00:00
|
|
|
|
:param field_list: optional argument containing the list of fields to log. Currently only
|
|
|
|
|
used when performing a read, it could be usefull later on if we want to log the write
|
|
|
|
|
on specific fields only.
|
2011-11-21 13:33:20 +00:00
|
|
|
|
|
2011-11-21 16:10:51 +00:00
|
|
|
|
:return: dictionary with
|
2011-11-21 13:33:20 +00:00
|
|
|
|
* keys: tuples build as ID of model object to log and ID of resource to log
|
|
|
|
|
* values: list of all the changes in field values for this couple (model, resource)
|
|
|
|
|
return {
|
2011-11-21 14:34:50 +00:00
|
|
|
|
(model.id, resource_id): []
|
2011-11-21 13:33:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-02 11:12:31 +00:00
|
|
|
|
The reason why the structure returned is build as above is because when modifying an existing
|
2012-03-30 09:08:37 +00:00
|
|
|
|
record, we may have to log a change done in a x2many field of that object
|
2011-11-21 13:33:20 +00:00
|
|
|
|
"""
|
2012-03-05 18:40:03 +00:00
|
|
|
|
if field_list is None:
|
|
|
|
|
field_list = []
|
2011-11-21 14:34:50 +00:00
|
|
|
|
key = (model.id, resource_id)
|
2011-11-18 20:27:13 +00:00
|
|
|
|
lines = {
|
2011-11-21 13:33:20 +00:00
|
|
|
|
key: []
|
2011-11-18 20:27:13 +00:00
|
|
|
|
}
|
2011-11-21 14:34:50 +00:00
|
|
|
|
# loop on all the fields
|
|
|
|
|
for field_name, field_definition in pool.get(model.model)._all_columns.items():
|
2012-11-01 05:31:46 +00:00
|
|
|
|
if field_name in ('__last_update', 'id'):
|
|
|
|
|
continue
|
2011-11-21 16:10:51 +00:00
|
|
|
|
#if the field_list param is given, skip all the fields not in that list
|
|
|
|
|
if field_list and field_name not in field_list:
|
|
|
|
|
continue
|
2011-11-21 14:34:50 +00:00
|
|
|
|
field_obj = field_definition.column
|
2011-11-18 20:27:13 +00:00
|
|
|
|
if field_obj._type in ('one2many','many2many'):
|
2011-11-21 13:33:20 +00:00
|
|
|
|
# checking if an audittrail rule apply in super admin mode
|
2012-08-31 13:51:36 +00:00
|
|
|
|
if self.check_rules(cr, SUPERUSER_ID, field_obj._obj, method):
|
2011-11-21 13:33:20 +00:00
|
|
|
|
# checking if the model associated to a *2m field exists, in super admin mode
|
2012-08-31 13:51:36 +00:00
|
|
|
|
x2m_model_ids = pool.get('ir.model').search(cr, SUPERUSER_ID, [('model', '=', field_obj._obj)])
|
2011-11-18 20:27:13 +00:00
|
|
|
|
x2m_model_id = x2m_model_ids and x2m_model_ids[0] or False
|
|
|
|
|
assert x2m_model_id, _("'%s' Model does not exist..." %(field_obj._obj))
|
2012-08-31 13:51:36 +00:00
|
|
|
|
x2m_model = pool.get('ir.model').browse(cr, SUPERUSER_ID, x2m_model_id)
|
2011-11-21 14:34:50 +00:00
|
|
|
|
# the resource_ids that need to be checked are the sum of both old and previous values (because we
|
2011-11-21 16:10:51 +00:00
|
|
|
|
# need to log also creation or deletion in those lists).
|
|
|
|
|
x2m_old_values_ids = old_values.get(key, {'value': {}})['value'].get(field_name, [])
|
|
|
|
|
x2m_new_values_ids = new_values.get(key, {'value': {}})['value'].get(field_name, [])
|
|
|
|
|
# We use list(set(...)) to remove duplicates.
|
|
|
|
|
res_ids = list(set(x2m_old_values_ids + x2m_new_values_ids))
|
2012-10-31 07:02:07 +00:00
|
|
|
|
if model.model == x2m_model.model:
|
|
|
|
|
# we need to remove current resource_id from the many2many to prevent an infinit loop
|
|
|
|
|
if resource_id in res_ids:
|
|
|
|
|
res_ids.remove(resource_id)
|
2012-11-16 10:49:36 +00:00
|
|
|
|
for res_id in res_ids:
|
|
|
|
|
lines.update(self.prepare_audittrail_log_line(cr, SUPERUSER_ID, pool, x2m_model, res_id, method, old_values, new_values, field_list))
|
2011-11-18 20:27:13 +00:00
|
|
|
|
# if the value value is different than the old value: record the change
|
2011-11-21 14:34:50 +00:00
|
|
|
|
if key not in old_values or key not in new_values or old_values[key]['value'][field_name] != new_values[key]['value'][field_name]:
|
2011-11-18 20:27:13 +00:00
|
|
|
|
data = {
|
2011-11-21 14:34:50 +00:00
|
|
|
|
'name': field_name,
|
|
|
|
|
'new_value': key in new_values and new_values[key]['value'].get(field_name),
|
|
|
|
|
'old_value': key in old_values and old_values[key]['value'].get(field_name),
|
|
|
|
|
'new_value_text': key in new_values and new_values[key]['text'].get(field_name),
|
|
|
|
|
'old_value_text': key in old_values and old_values[key]['text'].get(field_name)
|
2011-11-18 20:27:13 +00:00
|
|
|
|
}
|
2011-11-21 13:33:20 +00:00
|
|
|
|
lines[key].append(data)
|
2011-11-18 20:27:13 +00:00
|
|
|
|
return lines
|
|
|
|
|
|
2012-03-05 18:40:03 +00:00
|
|
|
|
def process_data(self, cr, uid, pool, res_ids, model, method, old_values=None, new_values=None, field_list=None):
|
2011-11-18 20:27:13 +00:00
|
|
|
|
"""
|
2011-11-21 13:33:20 +00:00
|
|
|
|
This function processes and iterates recursively to log the difference between the old
|
|
|
|
|
data (i.e before the method was executed) and the new data and creates audittrail log
|
|
|
|
|
accordingly.
|
2011-11-18 20:27:13 +00:00
|
|
|
|
|
|
|
|
|
:param cr: the current row, from the database cursor,
|
|
|
|
|
:param uid: the current user’s ID,
|
|
|
|
|
:param pool: current db's pooler object.
|
|
|
|
|
:param res_ids: Id's of resource to be logged/compared.
|
2011-11-21 13:33:20 +00:00
|
|
|
|
:param model: model object which values are being changed
|
|
|
|
|
:param method: method to log: create, read, unlink, write, actions, workflow actions
|
|
|
|
|
:param old_values: dict of values read before execution of the method
|
|
|
|
|
:param new_values: dict of values read after execution of the method
|
2011-11-21 16:10:51 +00:00
|
|
|
|
:param field_list: optional argument containing the list of fields to log. Currently only
|
|
|
|
|
used when performing a read, it could be usefull later on if we want to log the write
|
|
|
|
|
on specific fields only.
|
2011-11-21 13:33:20 +00:00
|
|
|
|
:return: True
|
2011-11-18 20:27:13 +00:00
|
|
|
|
"""
|
2012-10-02 11:12:31 +00:00
|
|
|
|
if field_list is None:
|
|
|
|
|
field_list = []
|
2011-11-21 14:34:50 +00:00
|
|
|
|
# loop on all the given ids
|
|
|
|
|
for res_id in res_ids:
|
2011-11-21 13:33:20 +00:00
|
|
|
|
# compare old and new values and get audittrail log lines accordingly
|
2011-11-21 16:10:51 +00:00
|
|
|
|
lines = self.prepare_audittrail_log_line(cr, uid, pool, model, res_id, method, old_values, new_values, field_list)
|
2011-11-18 20:27:13 +00:00
|
|
|
|
|
|
|
|
|
# if at least one modification has been found
|
|
|
|
|
for model_id, resource_id in lines:
|
2012-11-16 10:49:36 +00:00
|
|
|
|
name = pool.get(model.model).name_get(cr, uid, [resource_id])[0][1]
|
2011-11-18 20:27:13 +00:00
|
|
|
|
vals = {
|
|
|
|
|
'method': method,
|
|
|
|
|
'object_id': model_id,
|
|
|
|
|
'user_id': uid,
|
|
|
|
|
'res_id': resource_id,
|
2012-04-12 11:44:01 +00:00
|
|
|
|
'name': name,
|
2011-11-18 20:27:13 +00:00
|
|
|
|
}
|
2011-11-21 16:10:51 +00:00
|
|
|
|
if (model_id, resource_id) not in old_values and method not in ('copy', 'read'):
|
2011-11-18 20:27:13 +00:00
|
|
|
|
# the resource was not existing so we are forcing the method to 'create'
|
|
|
|
|
# (because it could also come with the value 'write' if we are creating
|
|
|
|
|
# new record through a one2many field)
|
|
|
|
|
vals.update({'method': 'create'})
|
2011-11-21 16:10:51 +00:00
|
|
|
|
if (model_id, resource_id) not in new_values and method not in ('copy', 'read'):
|
2011-11-18 20:27:13 +00:00
|
|
|
|
# the resource is not existing anymore so we are forcing the method to 'unlink'
|
|
|
|
|
# (because it could also come with the value 'write' if we are deleting the
|
|
|
|
|
# record through a one2many field)
|
|
|
|
|
vals.update({'method': 'unlink'})
|
2011-11-21 13:33:20 +00:00
|
|
|
|
# create the audittrail log in super admin mode, only if a change has been detected
|
|
|
|
|
if lines[(model_id, resource_id)]:
|
2012-08-31 13:51:36 +00:00
|
|
|
|
log_id = pool.get('audittrail.log').create(cr, SUPERUSER_ID, vals)
|
2012-11-16 10:49:36 +00:00
|
|
|
|
model = pool.get('ir.model').browse(cr, uid, model_id)
|
|
|
|
|
self.create_log_line(cr, SUPERUSER_ID, log_id, model, lines[(model_id, resource_id)])
|
2010-06-24 06:03:46 +00:00
|
|
|
|
return True
|
|
|
|
|
|
2011-09-29 22:23:47 +00:00
|
|
|
|
def check_rules(self, cr, uid, model, method):
|
2011-11-17 07:25:18 +00:00
|
|
|
|
"""
|
|
|
|
|
Checks if auditrails is installed for that db and then if one rule match
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID,
|
2011-11-18 20:27:13 +00:00
|
|
|
|
@param model: value of _name of the object which values are being changed
|
2011-11-17 07:25:18 +00:00
|
|
|
|
@param method: method to log: create, read, unlink,write,actions,workflow actions
|
|
|
|
|
@return: True or False
|
|
|
|
|
"""
|
2011-09-29 22:23:47 +00:00
|
|
|
|
pool = pooler.get_pool(cr.dbname)
|
|
|
|
|
if 'audittrail.rule' in pool.models:
|
2012-08-31 13:51:36 +00:00
|
|
|
|
model_ids = pool.get('ir.model').search(cr, SUPERUSER_ID, [('model', '=', model)])
|
2010-06-24 06:03:46 +00:00
|
|
|
|
model_id = model_ids and model_ids[0] or False
|
2011-09-29 22:23:47 +00:00
|
|
|
|
if model_id:
|
2012-08-31 13:51:36 +00:00
|
|
|
|
rule_ids = pool.get('audittrail.rule').search(cr, SUPERUSER_ID, [('object_id', '=', model_id), ('state', '=', 'subscribed')])
|
|
|
|
|
for rule in pool.get('audittrail.rule').read(cr, SUPERUSER_ID, rule_ids, ['user_id','log_read','log_write','log_create','log_unlink','log_action','log_workflow']):
|
2011-11-17 07:25:18 +00:00
|
|
|
|
if len(rule['user_id']) == 0 or uid in rule['user_id']:
|
2011-09-29 22:23:47 +00:00
|
|
|
|
if rule.get('log_'+method,0):
|
|
|
|
|
return True
|
2012-06-21 21:43:11 +00:00
|
|
|
|
elif method not in ('default_get','read','fields_view_get','fields_get','search','search_count','name_search','name_get','get','request_get', 'get_sc', 'unlink', 'write', 'create', 'read_group', 'import_data'):
|
2011-09-29 22:23:47 +00:00
|
|
|
|
if rule['log_action']:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def execute_cr(self, cr, uid, model, method, *args, **kw):
|
|
|
|
|
fct_src = super(audittrail_objects_proxy, self).execute_cr
|
|
|
|
|
if self.check_rules(cr,uid,model,method):
|
2012-03-06 17:46:20 +00:00
|
|
|
|
return self.log_fct(cr, uid, model, method, fct_src, *args, **kw)
|
|
|
|
|
return fct_src(cr, uid, model, method, *args, **kw)
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2012-03-06 17:46:20 +00:00
|
|
|
|
def exec_workflow_cr(self, cr, uid, model, method, *args, **kw):
|
2011-09-29 22:23:47 +00:00
|
|
|
|
fct_src = super(audittrail_objects_proxy, self).exec_workflow_cr
|
|
|
|
|
if self.check_rules(cr,uid,model,'workflow'):
|
2012-03-06 17:46:20 +00:00
|
|
|
|
return self.log_fct(cr, uid, model, method, fct_src, *args, **kw)
|
2012-03-07 07:49:07 +00:00
|
|
|
|
return fct_src(cr, uid, model, method, *args, **kw)
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2009-07-14 16:15:47 +00:00
|
|
|
|
audittrail_objects_proxy()
|
2008-11-22 00:14:01 +00:00
|
|
|
|
|
2008-11-21 23:52:29 +00:00
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
2008-11-22 00:14:01 +00:00
|
|
|
|
|