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
|
|
|
|
|
2009-09-24 10:46:21 +00:00
|
|
|
|
from osv import fields, osv
|
2011-06-15 13:23:47 +00:00
|
|
|
|
from osv.osv import object_proxy
|
2009-11-30 06:07:41 +00:00
|
|
|
|
from tools.translate import _
|
2008-11-07 13:25:56 +00:00
|
|
|
|
import pooler
|
2010-03-17 08:56:10 +00:00
|
|
|
|
import time
|
2010-06-24 06:03:46 +00:00
|
|
|
|
import tools
|
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"),
|
2011-10-03 05:47:33 +00:00
|
|
|
|
"state": fields.selection((("draft", "Draft"), ("subscribed", "Subscribed")), "State", 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 = {
|
2010-06-24 06:03:46 +00:00
|
|
|
|
'state': lambda *a: 'draft',
|
|
|
|
|
'log_create': lambda *a: 1,
|
|
|
|
|
'log_unlink': lambda *a: 1,
|
|
|
|
|
'log_write': lambda *a: 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
|
|
|
|
}
|
2010-06-24 06:03:46 +00:00
|
|
|
|
action_id = obj_action.create(cr, uid, val)
|
|
|
|
|
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)
|
2010-03-17 09:45:10 +00:00
|
|
|
|
res = obj_model.ir_set(cr, uid, '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:
|
|
|
|
|
obj_action.unlink(cr, uid, w_id)
|
|
|
|
|
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-08 16:55:34 +00:00
|
|
|
|
def get_value_text(self, cr, uid, pool, resource_pool, method, field, value, recursive=True):
|
2010-03-18 09:12:15 +00:00
|
|
|
|
"""
|
2011-11-08 16:55:34 +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.
|
|
|
|
|
In other cases, it just returns the value.
|
2010-03-18 09:12:15 +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'):
|
|
|
|
|
if recursive:
|
|
|
|
|
self.log_fct(cr, uid, field_obj._obj, method, None, value, 'child_relation_log')
|
|
|
|
|
data = pool.get(field_obj._obj).name_get(cr, uid, value)
|
|
|
|
|
#return the modifications on *2many fields as a list of names
|
|
|
|
|
return map(lambda x:x[1], data)
|
|
|
|
|
elif field_obj._type == 'many2one':
|
|
|
|
|
#return the modifications on a many2one field as the name of the value
|
|
|
|
|
return value and value[1] or value
|
|
|
|
|
return value
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
|
|
|
|
def create_log_line(self, cr, uid, log_id, model, lines=[]):
|
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,
|
2010-06-24 06:03:46 +00:00
|
|
|
|
@param model: Object who's values are being changed
|
2010-03-18 09:12:15 +00:00
|
|
|
|
@param lines: List of values for line is to be created
|
|
|
|
|
"""
|
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
|
2011-11-08 16:55:34 +00:00
|
|
|
|
|
2011-10-03 05:47:33 +00:00
|
|
|
|
def start_log_process(self, cr, user_id, model, method, resource_data, pool, resource_pool):
|
2011-09-07 12:38:13 +00:00
|
|
|
|
key1 = '%s_value'%(method == 'create' and 'new' or 'old')
|
|
|
|
|
key2 = '%s_value_text'%(method == 'create' and 'new' or 'old')
|
|
|
|
|
uid = 1
|
|
|
|
|
vals = { 'method': method, 'object_id': model.id,'user_id': user_id}
|
|
|
|
|
for resource, fields in resource_data.iteritems():
|
|
|
|
|
vals.update({'res_id': resource})
|
|
|
|
|
log_id = pool.get('audittrail.log').create(cr, uid, vals)
|
|
|
|
|
lines = []
|
|
|
|
|
for field_key, value in fields.iteritems():
|
|
|
|
|
if field_key in ('__last_update', 'id'):continue
|
2011-10-03 05:47:33 +00:00
|
|
|
|
ret_val = self.get_value_text(cr, uid, pool, resource_pool, method, field_key, value, method != 'read')
|
2011-09-07 12:38:13 +00:00
|
|
|
|
line = {
|
|
|
|
|
'name': field_key,
|
|
|
|
|
key1: value,
|
|
|
|
|
key2: ret_val and ret_val or value
|
|
|
|
|
}
|
|
|
|
|
lines.append(line)
|
|
|
|
|
self.create_log_line(cr, uid, log_id, model, lines)
|
|
|
|
|
return True
|
|
|
|
|
|
2011-09-29 22:23:47 +00:00
|
|
|
|
def log_fct(self, cr, uid, model, method, fct_src, *args):
|
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
|
|
|
|
|
:param model: Object which values are being changed
|
|
|
|
|
:param method: method to log: create, read, write, unlink
|
|
|
|
|
:param fct_src: execute method of Object proxy
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2011-11-08 16:55:34 +00:00
|
|
|
|
:return: Returns result as per method of Object proxy
|
2010-03-18 09:12:15 +00:00
|
|
|
|
"""
|
2011-02-24 16:52:21 +00:00
|
|
|
|
uid_orig = uid
|
|
|
|
|
uid = 1
|
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')
|
|
|
|
|
log_pool = pool.get('audittrail.log')
|
2011-10-03 05:47:33 +00:00
|
|
|
|
model_ids = model_pool.search(cr, 1, [('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))
|
2010-06-24 06:03:46 +00:00
|
|
|
|
model = model_pool.browse(cr, uid, model_id)
|
2011-11-08 16:55:34 +00:00
|
|
|
|
relational_table_log = True if (args and args[-1] == 'child_relation_log') else False
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2011-09-06 12:27:35 +00:00
|
|
|
|
if method == 'create':
|
2011-09-07 12:38:13 +00:00
|
|
|
|
resource_data = {}
|
2011-09-05 12:08:46 +00:00
|
|
|
|
fields_to_read = []
|
|
|
|
|
if relational_table_log:
|
|
|
|
|
res_id = args[0]
|
|
|
|
|
else:
|
2011-10-03 05:47:33 +00:00
|
|
|
|
res_id = fct_src(cr, uid_orig, model.model, method, *args)
|
2011-09-05 12:08:46 +00:00
|
|
|
|
fields_to_read = args[0].keys()
|
2011-09-07 12:38:13 +00:00
|
|
|
|
if res_id:
|
|
|
|
|
resource = resource_pool.read(cr, uid, res_id, fields_to_read)
|
|
|
|
|
if not isinstance(resource,list):
|
|
|
|
|
resource = [resource]
|
|
|
|
|
map(lambda x: resource_data.setdefault(x['id'], x), resource)
|
2011-10-03 05:47:33 +00:00
|
|
|
|
self.start_log_process(cr, uid_orig, model, method, resource_data, pool, resource_pool)
|
2010-03-17 05:03:01 +00:00
|
|
|
|
return res_id
|
|
|
|
|
|
2011-09-07 12:38:13 +00:00
|
|
|
|
elif method in('read', 'unlink'):
|
2010-03-17 05:03:01 +00:00
|
|
|
|
res_ids = args[0]
|
|
|
|
|
old_values = {}
|
2011-09-07 12:38:13 +00:00
|
|
|
|
if method == 'read':
|
2011-10-03 05:47:33 +00:00
|
|
|
|
res = fct_src(cr, uid_orig, model.model, method, *args)
|
2011-09-07 12:38:13 +00:00
|
|
|
|
map(lambda x: old_values.setdefault(x['id'], x), res)
|
2010-03-17 05:03:01 +00:00
|
|
|
|
else:
|
2011-09-07 12:38:13 +00:00
|
|
|
|
res = resource_pool.read(cr, uid, res_ids)
|
|
|
|
|
map(lambda x:old_values.setdefault(x['id'], x), res)
|
2011-10-03 05:47:33 +00:00
|
|
|
|
self.start_log_process(cr, uid_orig, model, method, old_values, pool, resource_pool)
|
2011-09-07 12:38:13 +00:00
|
|
|
|
if not relational_table_log and method == 'unlink':
|
2011-10-03 05:47:33 +00:00
|
|
|
|
res = fct_src(cr, uid_orig, model.model, method, *args)
|
2010-06-24 06:03:46 +00:00
|
|
|
|
return res
|
|
|
|
|
else:
|
2011-03-21 10:16:37 +00:00
|
|
|
|
res_ids = []
|
|
|
|
|
res = True
|
|
|
|
|
if args:
|
|
|
|
|
res_ids = args[0]
|
|
|
|
|
old_values = {}
|
|
|
|
|
fields = []
|
2011-09-06 06:22:41 +00:00
|
|
|
|
if len(args) > 1 and isinstance(args[1], dict):
|
2011-03-21 10:16:37 +00:00
|
|
|
|
fields = args[1].keys()
|
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-09-07 12:38:13 +00:00
|
|
|
|
x2m_old_values = {}
|
|
|
|
|
old_values = {}
|
|
|
|
|
def inline_process_old_data(res_ids, model, model_id=False):
|
|
|
|
|
resource_pool = pool.get(model)
|
|
|
|
|
resource_data = resource_pool.read(cr, uid, res_ids)
|
|
|
|
|
_old_values = {}
|
|
|
|
|
for resource in resource_data:
|
|
|
|
|
_old_values_text = {}
|
|
|
|
|
_old_value = {}
|
|
|
|
|
resource_id = resource['id']
|
|
|
|
|
for field in resource.keys():
|
|
|
|
|
if field in ('__last_update', 'id'):continue
|
|
|
|
|
field_obj = (resource_pool._all_columns.get(field)).column
|
|
|
|
|
if field_obj._type in ('one2many','many2many'):
|
2011-10-03 05:47:33 +00:00
|
|
|
|
if self.check_rules(cr, uid, field_obj._obj, method):
|
|
|
|
|
x2m_model_ids = model_pool.search(cr, 1, [('model', '=', field_obj._obj)])
|
|
|
|
|
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))
|
|
|
|
|
x2m_model = model_pool.browse(cr, uid, x2m_model_id)
|
2011-09-07 12:38:13 +00:00
|
|
|
|
x2m_old_values.update(inline_process_old_data(resource[field], field_obj._obj, x2m_model))
|
2011-10-03 05:47:33 +00:00
|
|
|
|
ret_val = self.get_value_text(cr, uid, pool, resource_pool, method, field, resource[field], False)
|
2011-09-07 12:38:13 +00:00
|
|
|
|
_old_value[field] = resource[field]
|
|
|
|
|
_old_values_text[field] = ret_val and ret_val or resource[field]
|
|
|
|
|
_old_values[resource_id] = {'text':_old_values_text, 'value': _old_value}
|
|
|
|
|
if model_id:
|
|
|
|
|
_old_values[resource_id].update({'model_id':model_id})
|
|
|
|
|
return _old_values
|
|
|
|
|
old_values.update(inline_process_old_data(res_ids, model.model))
|
2011-09-29 22:23:47 +00:00
|
|
|
|
res = fct_src(cr, uid_orig, model.model, method, *args)
|
2010-06-24 06:03:46 +00:00
|
|
|
|
if res_ids:
|
2011-09-07 12:38:13 +00:00
|
|
|
|
def inline_process_new_data(res_ids, model, dict_to_use={}):
|
|
|
|
|
resource_pool = pool.get(model.model)
|
|
|
|
|
resource_data = resource_pool.read(cr, uid, res_ids)
|
|
|
|
|
vals = {'method': method,'object_id': model.id,'user_id': uid_orig }
|
|
|
|
|
for resource in resource_data:
|
|
|
|
|
resource_id = resource['id']
|
|
|
|
|
vals.update({'res_id': resource_id})
|
2011-11-09 10:27:29 +00:00
|
|
|
|
if resource_id not in dict_to_use:
|
|
|
|
|
vals.update({'method': 'create'})
|
2011-09-07 12:38:13 +00:00
|
|
|
|
lines = []
|
|
|
|
|
for field in resource.keys():
|
|
|
|
|
if field in ('__last_update', 'id'):continue
|
|
|
|
|
field_obj = (resource_pool._all_columns.get(field)).column
|
|
|
|
|
if field_obj._type in ('one2many','many2many'):
|
2011-10-03 05:47:33 +00:00
|
|
|
|
if self.check_rules(cr, uid, field_obj._obj, method):
|
|
|
|
|
x2m_model_ids = model_pool.search(cr, 1, [('model', '=', field_obj._obj)])
|
|
|
|
|
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))
|
|
|
|
|
x2m_model = model_pool.browse(cr, uid, x2m_model_id)
|
2011-09-07 12:38:13 +00:00
|
|
|
|
inline_process_new_data(resource[field], x2m_model, x2m_old_values)
|
2011-10-03 05:47:33 +00:00
|
|
|
|
ret_val = self.get_value_text(cr, uid, pool, resource_pool, method, field, resource[field], False)
|
2011-09-07 12:38:13 +00:00
|
|
|
|
line = {
|
|
|
|
|
'name': field,
|
|
|
|
|
'new_value': resource[field],
|
|
|
|
|
'old_value': resource_id in dict_to_use and dict_to_use[resource_id]['value'].get(field),
|
|
|
|
|
'new_value_text': ret_val and ret_val or resource[field],
|
|
|
|
|
'old_value_text': resource_id in dict_to_use and dict_to_use[resource_id]['text'].get(field)
|
|
|
|
|
}
|
2011-11-09 10:27:29 +00:00
|
|
|
|
if line.get('new_value_text') != line.get('old_value_text'):
|
|
|
|
|
lines.append(line)
|
|
|
|
|
if lines:
|
|
|
|
|
log_id = log_pool.create(cr, uid, vals)
|
|
|
|
|
self.create_log_line(cr, uid, log_id, model, lines)
|
2011-09-07 12:38:13 +00:00
|
|
|
|
return True
|
|
|
|
|
inline_process_new_data(res_ids, model, old_values)
|
2010-03-17 05:03:01 +00:00
|
|
|
|
return res
|
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):
|
|
|
|
|
pool = pooler.get_pool(cr.dbname)
|
|
|
|
|
# Check if auditrails is installed for that db and then if one rule match
|
|
|
|
|
if 'audittrail.rule' in pool.models:
|
|
|
|
|
model_ids = pool.get('ir.model').search(cr, 1, [('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:
|
|
|
|
|
rule_ids = pool.get('audittrail.rule').search(cr, 1, [('object_id', '=', model_id), ('state', '=', 'subscribed')])
|
|
|
|
|
for rule in pool.get('audittrail.rule').read(cr, 1, rule_ids, ['user_id','log_read','log_write','log_create','log_unlink','log_action','log_workflow']):
|
|
|
|
|
if len(rule['user_id'])==0 or uid in rule['user_id']:
|
|
|
|
|
if rule.get('log_'+method,0):
|
|
|
|
|
return True
|
|
|
|
|
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'):
|
|
|
|
|
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):
|
2011-10-03 05:47:33 +00:00
|
|
|
|
return self.log_fct(cr, uid, model, method, fct_src, *args)
|
|
|
|
|
return fct_src(cr, uid, model, method, *args)
|
2010-06-24 06:03:46 +00:00
|
|
|
|
|
2011-09-29 22:23:47 +00:00
|
|
|
|
def exec_workflow_cr(self, cr, uid, model, method, *args, **argv):
|
|
|
|
|
fct_src = super(audittrail_objects_proxy, self).exec_workflow_cr
|
|
|
|
|
if self.check_rules(cr,uid,model,'workflow'):
|
2011-10-03 05:47:33 +00:00
|
|
|
|
return self.log_fct(cr, uid, model, method, fct_src, *args)
|
|
|
|
|
return fct_src(cr, uid, model, method, *args)
|
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
|
|
|
|
|