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.

69 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  3. # License LGPL-3 - See http://www.gnu.org/licenses/lgpl-3.0.html
  4. from openerp import _, api, fields, models
  5. from openerp.exceptions import ValidationError
  6. class CustomInfoTemplate(models.Model):
  7. """Defines custom properties expected for a given database object."""
  8. _description = "Custom information template"
  9. _name = "custom.info.template"
  10. _order = "model_id, name"
  11. _sql_constraints = [
  12. ("name_model",
  13. "UNIQUE (name, model)",
  14. "Another template with that name exists for that model."),
  15. ]
  16. name = fields.Char(required=True, translate=True)
  17. model = fields.Char(
  18. index=True,
  19. readonly=True,
  20. required=True)
  21. model_id = fields.Many2one(
  22. 'ir.model',
  23. 'Model',
  24. compute="_compute_model_id",
  25. store=True,
  26. ondelete="cascade",
  27. )
  28. property_ids = fields.One2many(
  29. 'custom.info.property',
  30. 'template_id',
  31. 'Properties',
  32. oldname="info_ids",
  33. context={"embed": True},
  34. )
  35. @api.multi
  36. @api.depends("model")
  37. def _compute_model_id(self):
  38. """Get a related model from its name, for better UI."""
  39. for s in self:
  40. s.model_id = self.env["ir.model"].search([("model", "=", s.model)])
  41. @api.multi
  42. @api.constrains("model")
  43. def _check_model(self):
  44. """Ensure model exists."""
  45. for s in self:
  46. if s.model not in self.env:
  47. raise ValidationError(_("Model does not exist."))
  48. # Avoid error when updating base module and a submodule extends a
  49. # model that falls out of this one's dependency graph
  50. with self.env.norecompute():
  51. oldmodels = set(s.mapped("property_ids.info_value_ids.model"))
  52. if oldmodels and {s.model} != oldmodels:
  53. raise ValidationError(
  54. _("You cannot change the model because it is in use."))
  55. @api.multi
  56. def check_access_rule(self, operation):
  57. """You access a template if you access its model."""
  58. for s in self:
  59. model = self.env[s.model]
  60. model.check_access_rights(operation)
  61. model.check_access_rule(operation)
  62. return super(CustomInfoTemplate, self).check_access_rule(operation)