You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

699 lines
30 KiB

9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2015 ABF OSIELL <http://osiell.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import logging
  5. from psycopg2 import ProgrammingError
  6. from openerp import models, fields, api, modules, _, SUPERUSER_ID, sql_db
  7. from openerp.exceptions import ValidationError
  8. FIELDS_BLACKLIST = [
  9. 'id', 'create_uid', 'create_date', 'write_uid', 'write_date',
  10. 'display_name', '__last_update',
  11. ]
  12. # Used for performance, to avoid a dictionary instanciation when we need an
  13. # empty dict to simplify algorithms
  14. EMPTY_DICT = {}
  15. class DictDiffer(object):
  16. """Calculate the difference between two dictionaries as:
  17. (1) items added
  18. (2) items removed
  19. (3) keys same in both but changed values
  20. (4) keys same in both and unchanged values
  21. """
  22. def __init__(self, current_dict, past_dict):
  23. self.current_dict, self.past_dict = current_dict, past_dict
  24. self.set_current = set(current_dict)
  25. self.set_past = set(past_dict)
  26. self.intersect = self.set_current.intersection(self.set_past)
  27. def added(self):
  28. return self.set_current - self.intersect
  29. def removed(self):
  30. return self.set_past - self.intersect
  31. def changed(self):
  32. return set(o for o in self.intersect
  33. if self.past_dict[o] != self.current_dict[o])
  34. def unchanged(self):
  35. return set(o for o in self.intersect
  36. if self.past_dict[o] == self.current_dict[o])
  37. class AuditlogRule(models.Model):
  38. _name = 'auditlog.rule'
  39. _description = "Auditlog - Rule"
  40. name = fields.Char(u"Name", size=32, required=True)
  41. model_id = fields.Many2one(
  42. 'ir.model', u"Model", required=True,
  43. help=u"Select model for which you want to generate log.")
  44. user_ids = fields.Many2many(
  45. 'res.users',
  46. 'audittail_rules_users',
  47. 'user_id', 'rule_id',
  48. string=u"Users",
  49. help=u"if User is not added then it will applicable for all users")
  50. log_read = fields.Boolean(
  51. u"Log Reads",
  52. help=(u"Select this if you want to keep track of read/open on any "
  53. u"record of the model of this rule"))
  54. log_write = fields.Boolean(
  55. u"Log Writes", default=True,
  56. help=(u"Select this if you want to keep track of modification on any "
  57. u"record of the model of this rule"))
  58. log_unlink = fields.Boolean(
  59. u"Log Deletes", default=True,
  60. help=(u"Select this if you want to keep track of deletion on any "
  61. u"record of the model of this rule"))
  62. log_create = fields.Boolean(
  63. u"Log Creates", default=True,
  64. help=(u"Select this if you want to keep track of creation on any "
  65. u"record of the model of this rule"))
  66. log_custom_method = fields.Boolean(
  67. u"Log Methods",
  68. help=(u"Select this if you want to keep track of custom methods on "
  69. u"any record of the model of this rule"))
  70. custom_method_ids = fields.One2many('auditlog.methods', 'rule_id')
  71. log_type = fields.Selection(
  72. [('full', u"Full log"),
  73. ('fast', u"Fast log"),
  74. ],
  75. string=u"Type", required=True, default='full',
  76. help=(u"Full log: make a diff between the data before and after "
  77. u"the operation (log more info like computed fields which were "
  78. u"updated, but it is slower)\n"
  79. u"Fast log: only log the changes made through the create and "
  80. u"write operations (less information, but it is faster)"))
  81. # log_action = fields.Boolean(
  82. # "Log Action",
  83. # help=("Select this if you want to keep track of actions on the "
  84. # "model of this rule"))
  85. # log_workflow = fields.Boolean(
  86. # "Log Workflow",
  87. # help=("Select this if you want to keep track of workflow on any "
  88. # "record of the model of this rule"))
  89. state = fields.Selection(
  90. [('draft', "Draft"), ('subscribed', "Subscribed")],
  91. string=u"State", required=True, default='draft')
  92. action_id = fields.Many2one(
  93. 'ir.actions.act_window', string="Action")
  94. _sql_constraints = [
  95. ('model_uniq', 'unique(model_id)',
  96. ("There is already a rule defined on this model\n"
  97. "You cannot define another: please edit the existing one."))
  98. ]
  99. def _register_hook(self, cr, ids=None):
  100. """Get all rules and apply them to log method calls."""
  101. super(AuditlogRule, self)._register_hook(cr)
  102. if not hasattr(self.pool, '_auditlog_field_cache'):
  103. self.pool._auditlog_field_cache = {}
  104. if not hasattr(self.pool, '_auditlog_model_cache'):
  105. self.pool._auditlog_model_cache = {}
  106. if ids is None:
  107. ids = self.search(cr, SUPERUSER_ID, [('state', '=', 'subscribed')])
  108. return self._patch_methods(cr, SUPERUSER_ID, ids)
  109. @api.multi
  110. def _patch_methods(self):
  111. """Patch ORM methods of models defined in rules to log their calls."""
  112. updated = False
  113. model_cache = self.pool._auditlog_model_cache
  114. try:
  115. with self.env.cr.savepoint():
  116. self.read()
  117. except ProgrammingError:
  118. logging.getLogger(__name__).error(
  119. "Error reading auditlog rules. Logs will not be created. "
  120. "Do you need to upgrade the auditlog module?", exc_info=True)
  121. return False
  122. for rule in self:
  123. if rule.state != 'subscribed':
  124. continue
  125. if not self.pool.get(rule.model_id.model):
  126. # ignore rules for models not loadable currently
  127. continue
  128. model_cache[rule.model_id.model] = rule.model_id.id
  129. model_model = self.env[rule.model_id.model]
  130. # CRUD
  131. # -> create
  132. check_attr = 'auditlog_ruled_create'
  133. if getattr(rule, 'log_create') \
  134. and not hasattr(model_model, check_attr):
  135. model_model._patch_method('create', rule._make_create())
  136. setattr(model_model, check_attr, True)
  137. updated = True
  138. # -> read
  139. check_attr = 'auditlog_ruled_read'
  140. if getattr(rule, 'log_read') \
  141. and not hasattr(model_model, check_attr):
  142. model_model._patch_method('read', rule._make_read())
  143. setattr(model_model, check_attr, True)
  144. updated = True
  145. # -> write
  146. check_attr = 'auditlog_ruled_write'
  147. if getattr(rule, 'log_write') \
  148. and not hasattr(model_model, check_attr):
  149. model_model._patch_method('write', rule._make_write())
  150. setattr(model_model, check_attr, True)
  151. updated = True
  152. # -> unlink
  153. check_attr = 'auditlog_ruled_unlink'
  154. if getattr(rule, 'log_unlink') \
  155. and not hasattr(model_model, check_attr):
  156. model_model._patch_method('unlink', rule._make_unlink())
  157. setattr(model_model, check_attr, True)
  158. updated = True
  159. # Check if custom methods are enabled and patch the different
  160. # rule methods
  161. if getattr(rule, 'log_custom_method'):
  162. for custom_method in rule.custom_method_ids:
  163. check_attr = 'auditlog_ruled_%s' % custom_method.name
  164. if not hasattr(model_model, custom_method.name):
  165. raise ValidationError(
  166. _('Method %s does not exist for model %s.' % (
  167. custom_method.name,
  168. model_model
  169. )))
  170. if not hasattr(model_model, check_attr):
  171. model_model._patch_method(
  172. custom_method.name,
  173. rule._make_custom(
  174. custom_method.message,
  175. custom_method.use_active_ids,
  176. custom_method.context_field_number)
  177. )
  178. setattr(model_model, check_attr, True)
  179. updated = True
  180. return updated
  181. @api.multi
  182. def _revert_methods(self):
  183. """Restore original ORM methods of models defined in rules."""
  184. updated = False
  185. for rule in self:
  186. model_model = self.env[rule.model_id.model]
  187. for method in ['create', 'read', 'write', 'unlink']:
  188. if getattr(rule, 'log_%s' % method) and hasattr(
  189. getattr(model_model, method), 'origin'):
  190. model_model._revert_method(method)
  191. updated = True
  192. if hasattr(rule, 'log_custom_method'):
  193. for custom_method in rule.custom_method_ids:
  194. method = custom_method.name
  195. if hasattr(getattr(model_model, method), 'origin'):
  196. model_model._revert_method(method)
  197. updated = True
  198. if updated:
  199. modules.registry.RegistryManager.signal_registry_change(
  200. self.env.cr.dbname)
  201. # Unable to find a way to declare the `create` method with the new API,
  202. # errors occurs with the `_register_hook()` BaseModel method.
  203. def create(self, cr, uid, vals, context=None):
  204. """Update the registry when a new rule is created."""
  205. res_id = super(AuditlogRule, self).create(
  206. cr, uid, vals, context=context)
  207. if self._register_hook(cr, [res_id]):
  208. modules.registry.RegistryManager.signal_registry_change(cr.dbname)
  209. return res_id
  210. # Unable to find a way to declare the `write` method with the new API,
  211. # errors occurs with the `_register_hook()` BaseModel method.
  212. def write(self, cr, uid, ids, vals, context=None):
  213. """Update the registry when existing rules are updated."""
  214. if isinstance(ids, (int, long)):
  215. ids = [ids]
  216. super(AuditlogRule, self).write(cr, uid, ids, vals, context=context)
  217. if self._register_hook(cr, ids):
  218. modules.registry.RegistryManager.signal_registry_change(cr.dbname)
  219. return True
  220. @api.multi
  221. def unlink(self):
  222. """Unsubscribe rules before removing them."""
  223. self.unsubscribe()
  224. return super(AuditlogRule, self).unlink()
  225. @api.multi
  226. def _make_create(self):
  227. """Instanciate a create method that log its calls."""
  228. self.ensure_one()
  229. log_type = self.log_type
  230. @api.model
  231. @api.returns('self', lambda value: value.id)
  232. def create_full(self, vals, **kwargs):
  233. self = self.with_context(auditlog_disabled=True)
  234. rule_model = self.env['auditlog.rule']
  235. new_record = create_full.origin(self, vals, **kwargs)
  236. new_values = dict(
  237. (d['id'], d) for d in new_record.sudo()
  238. .with_context(prefetch_fields=False).read(list(self._fields)))
  239. rule_model.sudo().create_logs(
  240. self.env.uid, self._name, new_record.ids,
  241. 'create', None, new_values, {'log_type': log_type})
  242. return new_record
  243. @api.model
  244. @api.returns('self', lambda value: value.id)
  245. def create_fast(self, vals, **kwargs):
  246. self = self.with_context(auditlog_disabled=True)
  247. rule_model = self.env['auditlog.rule']
  248. vals2 = dict(vals)
  249. new_record = create_fast.origin(self, vals, **kwargs)
  250. new_values = {new_record.id: vals2}
  251. rule_model.sudo().create_logs(
  252. self.env.uid, self._name, new_record.ids,
  253. 'create', None, new_values, {'log_type': log_type})
  254. return new_record
  255. return create_full if self.log_type == 'full' else create_fast
  256. @api.multi
  257. def _make_read(self):
  258. """Instanciate a read method that log its calls."""
  259. self.ensure_one()
  260. log_type = self.log_type
  261. def read(self, *args, **kwargs):
  262. result = read.origin(self, *args, **kwargs)
  263. # Sometimes the result is not a list but a dictionary
  264. # Also, we can not modify the current result as it will break calls
  265. result2 = result
  266. if not isinstance(result2, list):
  267. result2 = [result]
  268. read_values = dict((d['id'], d) for d in result2)
  269. # Old API
  270. if args and isinstance(args[0], sql_db.Cursor):
  271. cr, uid, ids = args[0], args[1], args[2]
  272. if isinstance(ids, (int, long)):
  273. ids = [ids]
  274. # If the call came from auditlog itself, skip logging:
  275. # avoid logs on `read` produced by auditlog during internal
  276. # processing: read data of relevant records, 'ir.model',
  277. # 'ir.model.fields'... (no interest in logging such operations)
  278. if kwargs.get('context', {}).get('auditlog_disabled'):
  279. return result
  280. env = api.Environment(cr, uid, {'auditlog_disabled': True})
  281. rule_model = env['auditlog.rule']
  282. rule_model.sudo().create_logs(
  283. env.uid, self._name, ids,
  284. 'read', read_values, None, {'log_type': log_type})
  285. # New API
  286. else:
  287. # If the call came from auditlog itself, skip logging:
  288. # avoid logs on `read` produced by auditlog during internal
  289. # processing: read data of relevant records, 'ir.model',
  290. # 'ir.model.fields'... (no interest in logging such operations)
  291. if self.env.context.get('auditlog_disabled'):
  292. return result
  293. self = self.with_context(auditlog_disabled=True)
  294. rule_model = self.env['auditlog.rule']
  295. rule_model.sudo().create_logs(
  296. self.env.uid, self._name, self.ids,
  297. 'read', read_values, None, {'log_type': log_type})
  298. return result
  299. return read
  300. @api.multi
  301. def _make_write(self):
  302. """Instanciate a write method that log its calls."""
  303. self.ensure_one()
  304. log_type = self.log_type
  305. @api.multi
  306. def write_full(self, vals, **kwargs):
  307. self = self.with_context(auditlog_disabled=True)
  308. rule_model = self.env['auditlog.rule']
  309. old_values = dict(
  310. (d['id'], d) for d in self.sudo()
  311. .with_context(prefetch_fields=False).read(list(self._fields)))
  312. result = write_full.origin(self, vals, **kwargs)
  313. new_values = dict(
  314. (d['id'], d) for d in self.sudo()
  315. .with_context(prefetch_fields=False).read(list(self._fields)))
  316. rule_model.sudo().create_logs(
  317. self.env.uid, self._name, self.ids,
  318. 'write', old_values, new_values, {'log_type': log_type})
  319. return result
  320. @api.multi
  321. def write_fast(self, vals, **kwargs):
  322. self = self.with_context(auditlog_disabled=True)
  323. rule_model = self.env['auditlog.rule']
  324. # Log the user input only, no matter if the `vals` is updated
  325. # afterwards as it could not represent the real state
  326. # of the data in the database
  327. vals2 = dict(vals)
  328. old_vals2 = dict.fromkeys(vals2.keys(), False)
  329. old_values = dict((id_, old_vals2) for id_ in self.ids)
  330. new_values = dict((id_, vals2) for id_ in self.ids)
  331. result = write_fast.origin(self, vals, **kwargs)
  332. rule_model.sudo().create_logs(
  333. self.env.uid, self._name, self.ids,
  334. 'write', old_values, new_values, {'log_type': log_type})
  335. return result
  336. return write_full if self.log_type == 'full' else write_fast
  337. @api.multi
  338. def _make_unlink(self):
  339. """Instanciate an unlink method that log its calls."""
  340. self.ensure_one()
  341. log_type = self.log_type
  342. @api.multi
  343. def unlink_full(self, **kwargs):
  344. self = self.with_context(auditlog_disabled=True)
  345. rule_model = self.env['auditlog.rule']
  346. old_values = dict(
  347. (d['id'], d) for d in self.sudo()
  348. .with_context(prefetch_fields=False).read(list(self._fields)))
  349. rule_model.sudo().create_logs(
  350. self.env.uid, self._name, self.ids, 'unlink', old_values, None,
  351. {'log_type': log_type})
  352. return unlink_full.origin(self, **kwargs)
  353. @api.multi
  354. def unlink_fast(self, **kwargs):
  355. self = self.with_context(auditlog_disabled=True)
  356. rule_model = self.env['auditlog.rule']
  357. rule_model.sudo().create_logs(
  358. self.env.uid, self._name, self.ids, 'unlink', None, None,
  359. {'log_type': log_type})
  360. return unlink_fast.origin(self, **kwargs)
  361. return unlink_full if self.log_type == 'full' else unlink_fast
  362. @api.multi
  363. def _make_custom(self, message, use_active_ids, context_field_number):
  364. """Instanciate a read method that log its calls."""
  365. self.ensure_one()
  366. log_type = self.log_type
  367. def custom(self, *args, **kwargs):
  368. result = custom.origin(self, *args, **kwargs)
  369. result2 = result
  370. if not isinstance(result2, list):
  371. result2 = [result]
  372. # Old API
  373. if args and isinstance(args[0], sql_db.Cursor):
  374. cr, uid, ids = args[0], args[1], args[2]
  375. if isinstance(ids, (int, long)):
  376. ids = [ids]
  377. context = kwargs.get('context', {})
  378. # Set specific context if it is defined by our rule
  379. if not context and context_field_number:
  380. if context_field_number - 1 < len(args):
  381. context = args[context_field_number - 1]
  382. if context.get('auditlog_disabled'):
  383. return result
  384. env = api.Environment(cr, uid, {'auditlog_disabled': True})
  385. rule_model = env['auditlog.rule']
  386. # Overwrite the ids and object_model if it is required
  387. # by the auditlog rule
  388. object_model = self._name
  389. if use_active_ids:
  390. if context.get('active_model'):
  391. if context.get('active_ids'):
  392. object_model = context.get(
  393. 'active_model',
  394. object_model)
  395. ids = context.get('active_ids', ids)
  396. rule_model.sudo().create_logs(
  397. env.uid, object_model, ids,
  398. message, None, None, {'log_type': log_type})
  399. # New API
  400. else:
  401. if self.env.context.get('auditlog_disabled'):
  402. return result
  403. self = self.with_context(auditlog_disabled=True)
  404. context = self.env.context
  405. # Overwrite the ids and object_model if it is required
  406. # by the auditlog rule
  407. ids = self.ids
  408. object_model = self._name
  409. if use_active_ids:
  410. if context.get('active_model'):
  411. if context.get('active_ids'):
  412. object_model = context.get(
  413. 'active_model',
  414. object_model)
  415. ids = context.get('active_ids', ids)
  416. rule_model = self.env['auditlog.rule']
  417. rule_model.sudo().create_logs(
  418. self.env.uid, object_model, ids,
  419. message, None, None, {'log_type': log_type})
  420. return result
  421. return custom
  422. def create_logs(self, uid, res_model, res_ids, method,
  423. old_values=None, new_values=None,
  424. additional_log_values=None):
  425. """Create logs. `old_values` and `new_values` are dictionnaries, e.g:
  426. {RES_ID: {'FIELD': VALUE, ...}}
  427. """
  428. if old_values is None:
  429. old_values = EMPTY_DICT
  430. if new_values is None:
  431. new_values = EMPTY_DICT
  432. log_model = self.env['auditlog.log']
  433. http_request_model = self.env['auditlog.http.request']
  434. http_session_model = self.env['auditlog.http.session']
  435. for res_id in res_ids:
  436. model_model = self.env[res_model]
  437. # Do an extra check for active_model situations where res_model
  438. # is not preloaded in auditlog model_cache
  439. if not self.pool._auditlog_model_cache.get(res_model):
  440. self.pool._auditlog_model_cache[res_model] = \
  441. self.env['ir.model'].search([
  442. ('model', '=', res_model)]).id
  443. name = model_model.browse(res_id).name_get()
  444. res_name = name and name[0] and name[0][1]
  445. vals = {
  446. 'name': res_name,
  447. 'model_id': self.pool._auditlog_model_cache[res_model],
  448. 'res_id': res_id,
  449. 'method': method,
  450. 'user_id': uid,
  451. 'http_request_id': http_request_model.current_http_request(),
  452. 'http_session_id': http_session_model.current_http_session(),
  453. }
  454. vals.update(additional_log_values or {})
  455. log = log_model.create(vals)
  456. diff = DictDiffer(
  457. new_values.get(res_id, EMPTY_DICT),
  458. old_values.get(res_id, EMPTY_DICT))
  459. if method is 'create':
  460. self._create_log_line_on_create(log, diff.added(), new_values)
  461. elif method is 'read':
  462. self._create_log_line_on_read(
  463. log, old_values.get(res_id, EMPTY_DICT).keys(), old_values)
  464. elif method is 'write':
  465. self._create_log_line_on_write(
  466. log, diff.changed(), old_values, new_values)
  467. def _get_field(self, model, field_name):
  468. cache = self.pool._auditlog_field_cache
  469. if field_name not in cache.get(model.model, {}):
  470. cache.setdefault(model.model, {})
  471. # - we use 'search()' then 'read()' instead of the 'search_read()'
  472. # to take advantage of the 'classic_write' loading
  473. # - search the field in the current model and those it inherits
  474. field_model = self.env['ir.model.fields']
  475. all_model_ids = [model.id]
  476. all_model_ids.extend(model.inherited_model_ids.ids)
  477. field = field_model.search(
  478. [('model_id', 'in', all_model_ids), ('name', '=', field_name)])
  479. # The field can be a dummy one, like 'in_group_X' on 'res.users'
  480. # As such we can't log it (field_id is required to create a log)
  481. if not field:
  482. cache[model.model][field_name] = False
  483. else:
  484. field_data = field.read(load='_classic_write')[0]
  485. cache[model.model][field_name] = field_data
  486. return cache[model.model][field_name]
  487. def _create_log_line_on_read(
  488. self, log, fields_list, read_values):
  489. """Log field filled on a 'read' operation."""
  490. log_line_model = self.env['auditlog.log.line']
  491. for field_name in fields_list:
  492. if field_name in FIELDS_BLACKLIST:
  493. continue
  494. field = self._get_field(log.model_id, field_name)
  495. # not all fields have an ir.models.field entry (ie. related fields)
  496. if field:
  497. log_vals = self._prepare_log_line_vals_on_read(
  498. log, field, read_values)
  499. log_line_model.create(log_vals)
  500. def _prepare_log_line_vals_on_read(self, log, field, read_values):
  501. """Prepare the dictionary of values used to create a log line on a
  502. 'read' operation.
  503. """
  504. vals = {
  505. 'field_id': field['id'],
  506. 'log_id': log.id,
  507. 'old_value': read_values[log.res_id][field['name']],
  508. 'old_value_text': read_values[log.res_id][field['name']],
  509. 'new_value': False,
  510. 'new_value_text': False,
  511. }
  512. if field['relation'] and '2many' in field['ttype']:
  513. old_value_text = self.env[field['relation']].browse(
  514. vals['old_value']).name_get()
  515. vals['old_value_text'] = old_value_text
  516. return vals
  517. def _create_log_line_on_write(
  518. self, log, fields_list, old_values, new_values):
  519. """Log field updated on a 'write' operation."""
  520. log_line_model = self.env['auditlog.log.line']
  521. for field_name in fields_list:
  522. if field_name in FIELDS_BLACKLIST:
  523. continue
  524. field = self._get_field(log.model_id, field_name)
  525. # not all fields have an ir.models.field entry (ie. related fields)
  526. if field:
  527. log_vals = self._prepare_log_line_vals_on_write(
  528. log, field, old_values, new_values)
  529. log_line_model.create(log_vals)
  530. def _prepare_log_line_vals_on_write(
  531. self, log, field, old_values, new_values):
  532. """Prepare the dictionary of values used to create a log line on a
  533. 'write' operation.
  534. """
  535. vals = {
  536. 'field_id': field['id'],
  537. 'log_id': log.id,
  538. 'old_value': old_values[log.res_id][field['name']],
  539. 'old_value_text': old_values[log.res_id][field['name']],
  540. 'new_value': new_values[log.res_id][field['name']],
  541. 'new_value_text': new_values[log.res_id][field['name']],
  542. }
  543. # for *2many fields, log the name_get
  544. if log.log_type == 'full' and field['relation'] \
  545. and '2many' in field['ttype']:
  546. # Filter IDs to prevent a 'name_get()' call on deleted resources
  547. existing_ids = self.env[field['relation']]._search(
  548. [('id', 'in', vals['old_value'])])
  549. old_value_text = []
  550. if existing_ids:
  551. existing_values = self.env[field['relation']].browse(
  552. existing_ids).name_get()
  553. old_value_text.extend(existing_values)
  554. # Deleted resources will have a 'DELETED' text representation
  555. deleted_ids = set(vals['old_value']) - set(existing_ids)
  556. for deleted_id in deleted_ids:
  557. old_value_text.append((deleted_id, 'DELETED'))
  558. vals['old_value_text'] = old_value_text
  559. new_value_text = self.env[field['relation']].browse(
  560. vals['new_value']).name_get()
  561. vals['new_value_text'] = new_value_text
  562. return vals
  563. def _create_log_line_on_create(
  564. self, log, fields_list, new_values):
  565. """Log field filled on a 'create' operation."""
  566. log_line_model = self.env['auditlog.log.line']
  567. for field_name in fields_list:
  568. if field_name in FIELDS_BLACKLIST:
  569. continue
  570. field = self._get_field(log.model_id, field_name)
  571. # not all fields have an ir.models.field entry (ie. related fields)
  572. if field:
  573. log_vals = self._prepare_log_line_vals_on_create(
  574. log, field, new_values)
  575. log_line_model.create(log_vals)
  576. def _prepare_log_line_vals_on_create(self, log, field, new_values):
  577. """Prepare the dictionary of values used to create a log line on a
  578. 'create' operation.
  579. """
  580. vals = {
  581. 'field_id': field['id'],
  582. 'log_id': log.id,
  583. 'old_value': False,
  584. 'old_value_text': False,
  585. 'new_value': new_values[log.res_id][field['name']],
  586. 'new_value_text': new_values[log.res_id][field['name']],
  587. }
  588. if log.log_type == 'full' and field['relation'] \
  589. and '2many' in field['ttype']:
  590. new_value_text = self.env[field['relation']].browse(
  591. vals['new_value']).name_get()
  592. vals['new_value_text'] = new_value_text
  593. return vals
  594. @api.multi
  595. def subscribe(self):
  596. """Subscribe Rule for auditing changes on model and apply shortcut
  597. to view logs on that model.
  598. """
  599. act_window_model = self.env['ir.actions.act_window']
  600. model_data_model = self.env['ir.model.data']
  601. for rule in self:
  602. # Create a shortcut to view logs
  603. domain = "[('model_id', '=', %s), ('res_id', '=', active_id)]" % (
  604. rule.model_id.id)
  605. vals = {
  606. 'name': _(u"View logs"),
  607. 'res_model': 'auditlog.log',
  608. 'src_model': rule.model_id.model,
  609. 'domain': domain,
  610. }
  611. act_window = act_window_model.sudo().create(vals)
  612. rule.write({'state': 'subscribed', 'action_id': act_window.id})
  613. keyword = 'client_action_relate'
  614. value = 'ir.actions.act_window,%s' % act_window.id
  615. model_data_model.sudo().ir_set(
  616. 'action', keyword, 'View_log_' + rule.model_id.model,
  617. [rule.model_id.model], value, replace=True,
  618. isobject=True, xml_id=False)
  619. return True
  620. @api.multi
  621. def unsubscribe(self):
  622. """Unsubscribe Auditing Rule on model."""
  623. act_window_model = self.env['ir.actions.act_window']
  624. ir_values_model = self.env['ir.values']
  625. # Revert patched methods
  626. self._revert_methods()
  627. for rule in self:
  628. # Remove the shortcut to view logs
  629. act_window = act_window_model.search(
  630. [('name', '=', 'View Log'),
  631. ('res_model', '=', 'auditlog.log'),
  632. ('src_model', '=', rule.model_id.model)])
  633. if act_window:
  634. value = 'ir.actions.act_window,%s' % act_window.id
  635. act_window.unlink()
  636. ir_value = ir_values_model.search(
  637. [('model', '=', rule.model_id.model),
  638. ('value', '=', value)])
  639. if ir_value:
  640. ir_value.unlink()
  641. self.write({'state': 'draft'})
  642. return True