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.

86 lines
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. #
  4. # Authors: Guewen Baconnier
  5. # Copyright 2015 Camptocamp SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (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 Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #
  21. from openerp import models, fields, api
  22. class ResPartner(models.Model):
  23. _inherit = 'res.partner'
  24. @api.multi
  25. def write(self, values):
  26. for record in self:
  27. values = record._add_revision(values)
  28. return super(ResPartner, self).write(values)
  29. @api.multi
  30. def _add_revision(self, values):
  31. """ Add a revision on a partner
  32. By default, when a partner is modified by a user or by the
  33. system, the changes are applied and a validated revision is
  34. created. Callers which want to delegate the write of some
  35. fields to the revision must explicitly ask for it by providing a
  36. key ``__revision_rules`` in the environment's context.
  37. :param values: the values being written on the partner
  38. :type values: dict
  39. :returns: dict of values that should be wrote on the partner
  40. (fields with a 'Validate' or 'Never' rule are excluded)
  41. """
  42. self.ensure_one()
  43. write_values = values.copy()
  44. changes = []
  45. rules = self.env['revision.behavior'].get_rules(self._model._name)
  46. for field in values:
  47. rule = rules.get(field)
  48. if not rule:
  49. continue
  50. if field in values:
  51. if self[field] == values[field]:
  52. # TODO handle relations, types
  53. continue
  54. change = {
  55. 'current_value': self[field],
  56. 'new_value': values[field],
  57. 'field_id': rule.field_id.id,
  58. }
  59. if not self.env.context.get('__revision_rules'):
  60. # by default always write on partner
  61. change['state'] = 'done'
  62. elif rule.default_behavior == 'auto':
  63. change['state'] = 'done'
  64. elif rule.default_behavior == 'validate':
  65. change['state'] = 'draft'
  66. write_values.pop(field) # change to apply manually
  67. elif rule.default_behavior == 'never':
  68. change['state'] = 'cancel'
  69. write_values.pop(field) # change never applied
  70. changes.append(change)
  71. if changes:
  72. self.env['res.partner.revision'].create({
  73. 'partner_id': self.id,
  74. 'change_ids': [(0, 0, vals) for vals in changes],
  75. 'date': fields.Datetime.now(),
  76. })
  77. return write_values