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.

161 lines
5.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 ChangesetFieldRule(models.Model):
  24. _name = 'changeset.field.rule'
  25. _description = 'Changeset Field Rules'
  26. _rec_name = 'field_id'
  27. field_id = fields.Many2one(
  28. comodel_name='ir.model.fields',
  29. string='Field',
  30. domain="[('model_id.model', '=', 'res.partner'), "
  31. " ('ttype', 'in', ('char', 'selection', 'date', 'datetime', "
  32. " 'float', 'integer', 'text', 'boolean', "
  33. " 'many2one')), "
  34. " ('readonly', '=', False)]",
  35. ondelete='cascade',
  36. required=True,
  37. )
  38. action = fields.Selection(
  39. selection='_selection_action',
  40. string='Action',
  41. required=True,
  42. help="Auto: always apply a change.\n"
  43. "Validate: manually applied by an administrator.\n"
  44. "Never: change never applied.",
  45. )
  46. source_model_id = fields.Many2one(
  47. comodel_name='ir.model',
  48. string='Source Model',
  49. ondelete='cascade',
  50. domain=lambda self: [('id', 'in', self._domain_source_models().ids)],
  51. help="If a source model is defined, the rule will be applied only "
  52. "when the change is made from this origin. "
  53. "Rules without source model are global and applies to all "
  54. "backends.\n"
  55. "Rules with a source model have precedence over global rules, "
  56. "but if a field has no rule with a source model, the global rule "
  57. "is used."
  58. )
  59. _sql_constraints = [
  60. ('model_field_uniq',
  61. 'unique (source_model_id, field_id)',
  62. 'A rule already exists for this field.'),
  63. ]
  64. @api.model
  65. def _domain_source_models(self):
  66. """ Returns the models for which we can define rules.
  67. Example for submodules (replace by the xmlid of the model):
  68. ::
  69. models = super(ChangesetFieldRule, self)._domain_source_models()
  70. return models | self.env.ref('base.model_res_users')
  71. Rules without model are global and apply for all models.
  72. """
  73. return self.env.ref('base.model_res_users')
  74. @api.model
  75. def _selection_action(self):
  76. return [('auto', 'Auto'),
  77. ('validate', 'Validate'),
  78. ('never', 'Never'),
  79. ]
  80. @ormcache(skiparg=1)
  81. @api.model
  82. def _get_rules(self, source_model_name):
  83. """ Cache rules
  84. Keep only the id of the rules, because if we keep the recordsets
  85. in the ormcache, we won't be able to browse them once their
  86. cursor is closed.
  87. The public method ``get_rules`` return the rules with the recordsets
  88. when called.
  89. """
  90. model_rules = self.search(
  91. ['|', ('source_model_id.model', '=', source_model_name),
  92. ('source_model_id', '=', False)],
  93. # using 'ASC' means that 'NULLS LAST' is the default
  94. order='source_model_id ASC',
  95. )
  96. # model's rules have precedence over global ones so we iterate
  97. # over rules which have a source model first, then we complete
  98. # them with the global rules
  99. result = {}
  100. for rule in model_rules:
  101. # we already have a model's rule
  102. if result.get(rule.field_id.name):
  103. continue
  104. result[rule.field_id.name] = rule.id
  105. return result
  106. @api.model
  107. def get_rules(self, source_model_name):
  108. """ Return the rules for a model
  109. When a model is specified, it will return the rules for this
  110. model. Fields that have no rule for this model will use the
  111. global rules (those without source).
  112. The source model is the model which ask for a change, it will be
  113. for instance ``res.users``, ``lefac.backend`` or
  114. ``magellan.backend``.
  115. The second argument (``source_model_name``) is optional but
  116. cannot be an optional keyword argument otherwise it would not be
  117. in the key for the cache. The callers have to pass ``None`` if
  118. they want only global rules.
  119. """
  120. rules = {}
  121. cached_rules = self._get_rules(source_model_name)
  122. for field, rule_id in cached_rules.iteritems():
  123. rules[field] = self.browse(rule_id)
  124. return rules
  125. @api.model
  126. def create(self, vals):
  127. record = super(ChangesetFieldRule, self).create(vals)
  128. self.clear_caches()
  129. return record
  130. @api.multi
  131. def write(self, vals):
  132. result = super(ChangesetFieldRule, self).write(vals)
  133. self.clear_caches()
  134. return result
  135. @api.multi
  136. def unlink(self):
  137. result = super(ChangesetFieldRule, self).unlink()
  138. self.clear_caches()
  139. return result