audit_decorator.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2010 Smile (<http://www.smile.fr>). All Rights Reserved
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models
  22. def _get_args(self, method, args, kwargs):
  23. # avoid hasattr(self, '_ids') because __getattr__() is overridden
  24. if '_ids' in self.__dict__:
  25. cr, uid, context = self.env.args
  26. ids = self._ids
  27. vals = method in ('create', 'write') and args[0] or {}
  28. else:
  29. cr, uid = args[:2]
  30. ids = method in ('write', 'unlink') and args[2] or []
  31. vals = (method == 'create' and args[2]) or (method == 'write' and args[3]) or {}
  32. context = kwargs.get('context')
  33. if isinstance(ids, (int, long)):
  34. ids = [ids]
  35. if self._name == 'res.users':
  36. vals = self._remove_reified_groups(vals)
  37. return cr, uid, ids, vals, context
  38. def audit_decorator():
  39. def audit_wrapper(self, *args, **kwargs):
  40. origin = audit_wrapper.origin
  41. while hasattr(origin, 'origin'):
  42. origin = origin.origin
  43. method = origin.__name__
  44. cr, uid, ids, vals, context = _get_args(self, method, args, kwargs)
  45. rule_id = None
  46. if getattr(self, 'audit_rule', None):
  47. rule_obj = self.pool['audit.rule']
  48. rule_id = rule_obj._check_audit_rule(cr).get(self._name, {}).get(method)
  49. if rule_id:
  50. old_values = None
  51. if method != 'create':
  52. records = self.browse(cr, uid, ids, context)
  53. old_values = records.read(vals.keys(), load='_classic_write')
  54. if method == 'unlink':
  55. rule_obj.log(cr, uid, rule_id, method, old_values)
  56. result = audit_wrapper.origin(self, *args, **kwargs)
  57. if rule_id:
  58. new_values = None
  59. if method != 'unlink':
  60. if method == 'create':
  61. if isinstance(result, models.Model):
  62. ids = result.ids
  63. elif isinstance(result, int):
  64. ids = [result]
  65. else:
  66. ids = []
  67. records = self.browse(cr, uid, ids, context)
  68. records.invalidate_cache()
  69. new_values = records.read(vals.keys(), load='_classic_write')
  70. rule_obj.log(cr, uid, rule_id, method, old_values, new_values)
  71. return result
  72. return audit_wrapper