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.

79 lines
2.5 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. from openerp.tools.cache import ormcache
  23. class RevisionFieldRule(models.Model):
  24. _name = 'revision.field.rule'
  25. _description = 'Revision Field Rules'
  26. _rec_name = 'field_id'
  27. model_id = fields.Many2one(comodel_name='ir.model',
  28. string='Model',
  29. ondelete='cascade',
  30. default=lambda self: self._default_model_id())
  31. field_id = fields.Many2one(comodel_name='ir.model.fields',
  32. string='Field',
  33. ondelete='cascade')
  34. action = fields.Selection(
  35. selection='_selection_action',
  36. string='Action',
  37. help="Auto: always apply a change.\n"
  38. "Validate: manually applied by an administrator.\n"
  39. "Never: change never applied.",
  40. )
  41. @api.model
  42. def _default_model_id(self):
  43. return self.env.ref('base.model_res_partner').id
  44. @api.model
  45. def _selection_action(self):
  46. return [('auto', 'Auto'),
  47. ('validate', 'Validate'),
  48. ('never', 'Never'),
  49. ]
  50. @ormcache()
  51. @api.model
  52. def get_rules(self, model_name):
  53. rules = self.search([('model_id', '=', model_name)])
  54. return {rule.field_id.name: rule for rule in rules}
  55. @api.model
  56. def create(self, vals):
  57. record = super(RevisionFieldRule, self).create(vals)
  58. self.clear_caches()
  59. return record
  60. @api.multi
  61. def write(self, vals):
  62. result = super(RevisionFieldRule, self).write(vals)
  63. self.clear_caches()
  64. return result
  65. @api.multi
  66. def unlink(self):
  67. result = super(RevisionFieldRule, self).unlink()
  68. self.clear_caches()
  69. return result