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.

126 lines
4.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Antiun Ingeniería S.L. - Sergio Teruel
  3. # © 2015 Antiun Ingeniería S.L. - Carlos Dauden
  4. # © 2015 Antiun Ingeniería S.L. - Jairo Llopis
  5. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  6. from openerp import api, fields, models
  7. class CustomInfoModelLink(models.AbstractModel):
  8. _description = "A model that gets its ``ir.model`` computed"
  9. _name = "custom.info.model_link"
  10. model = fields.Char(
  11. index=True,
  12. readonly=True,
  13. required=True)
  14. model_id = fields.Many2one(
  15. 'ir.model',
  16. 'Model',
  17. compute="_compute_model_id",
  18. store=True)
  19. @api.multi
  20. @api.depends("model")
  21. def _compute_model_id(self):
  22. """Get a related model from its name, for better UI."""
  23. for s in self:
  24. s.model_id = self.env["ir.model"].search([("model", "=", s.model)])
  25. class CustomInfoTemplate(models.Model):
  26. """Defines custom properties expected for a given database object."""
  27. _description = "Custom information template"
  28. _name = "custom.info.template"
  29. _inherit = "custom.info.model_link"
  30. _sql_constraints = [
  31. ("name_model",
  32. "UNIQUE (name, model)",
  33. "Another template with that name exists for that model."),
  34. ]
  35. name = fields.Char(required=True, translate=True)
  36. info_ids = fields.One2many(
  37. 'custom.info.property',
  38. 'template_id',
  39. 'Properties')
  40. class CustomInfoProperty(models.Model):
  41. """Name of the custom information property."""
  42. _description = "Custom information property"
  43. _name = "custom.info.property"
  44. _sql_constraints = [
  45. ("name_template",
  46. "UNIQUE (name, template_id)",
  47. "Another property with that name exists for that template."),
  48. ]
  49. name = fields.Char(required=True, translate=True)
  50. template_id = fields.Many2one(
  51. comodel_name='custom.info.template',
  52. string='Template',
  53. required=True)
  54. info_value_ids = fields.One2many(
  55. comodel_name="custom.info.value",
  56. inverse_name="property_id",
  57. string="Property Values")
  58. class CustomInfoValue(models.Model):
  59. _description = "Custom information value"
  60. _name = "custom.info.value"
  61. _inherit = "custom.info.model_link"
  62. _rec_name = 'value'
  63. _sql_constraints = [
  64. ("property_model_res",
  65. "UNIQUE (property_id, model, res_id)",
  66. "Another property with that name exists for that resource."),
  67. ]
  68. res_id = fields.Integer("Resource ID", index=True, required=True)
  69. property_id = fields.Many2one(
  70. comodel_name='custom.info.property',
  71. required=True,
  72. string='Property')
  73. name = fields.Char(related='property_id.name', readonly=True)
  74. value = fields.Char(translate=True, index=True)
  75. class CustomInfo(models.AbstractModel):
  76. _description = "Inheritable abstract model to add custom info in any model"
  77. _name = "custom.info"
  78. custom_info_template_id = fields.Many2one(
  79. comodel_name='custom.info.template',
  80. string='Custom Information Template')
  81. custom_info_ids = fields.One2many(
  82. comodel_name='custom.info.value',
  83. inverse_name='res_id',
  84. domain=lambda self: [("model", "=", self._name)],
  85. auto_join=True,
  86. string='Custom Properties')
  87. @api.multi
  88. @api.onchange('custom_info_template_id')
  89. def _onchange_custom_info_template_id(self):
  90. if not self.custom_info_template_id:
  91. self.custom_info_ids = False
  92. else:
  93. info_list = self.custom_info_ids.mapped('property_id')
  94. for info_name in self.custom_info_template_id.info_ids:
  95. if info_name not in info_list:
  96. self.custom_info_ids |= self.custom_info_ids.new({
  97. 'model': self._name,
  98. 'property_id': info_name.id,
  99. "res_id": self.id,
  100. })
  101. @api.multi
  102. def unlink(self):
  103. info_values = self.mapped('custom_info_ids')
  104. res = super(CustomInfo, self).unlink()
  105. if res:
  106. info_values.unlink()
  107. return res