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.

125 lines
4.0 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(translate=True)
  50. template_id = fields.Many2one(
  51. comodel_name='custom.info.template',
  52. string='Template')
  53. info_value_ids = fields.One2many(
  54. comodel_name="custom.info.value",
  55. inverse_name="property_id",
  56. string="Property Values")
  57. class CustomInfoValue(models.Model):
  58. _description = "Custom information value"
  59. _name = "custom.info.value"
  60. _inherit = "custom.info.model_link"
  61. _rec_name = 'value'
  62. _sql_constraints = [
  63. ("property_model_res",
  64. "UNIQUE (property_id, model, res_id)",
  65. "Another property with that name exists for that resource."),
  66. ]
  67. res_id = fields.Integer("Resource ID", index=True, required=True)
  68. property_id = fields.Many2one(
  69. comodel_name='custom.info.property',
  70. required=True,
  71. string='Property')
  72. name = fields.Char(related='property_id.name')
  73. value = fields.Char(translate=True)
  74. class CustomInfo(models.AbstractModel):
  75. _description = "Inheritable abstract model to add custom info in any model"
  76. _name = "custom.info"
  77. custom_info_template_id = fields.Many2one(
  78. comodel_name='custom.info.template',
  79. string='Custom Information Template')
  80. custom_info_ids = fields.One2many(
  81. comodel_name='custom.info.value',
  82. inverse_name='res_id',
  83. domain=lambda self: [("model", "=", self._name)],
  84. auto_join=True,
  85. string='Custom Properties')
  86. @api.multi
  87. @api.onchange('custom_info_template_id')
  88. def _onchange_custom_info_template_id(self):
  89. if not self.custom_info_template_id:
  90. self.custom_info_ids = False
  91. else:
  92. info_list = self.custom_info_ids.mapped('property_id')
  93. for info_name in self.custom_info_template_id.info_ids:
  94. if info_name not in info_list:
  95. self.custom_info_ids |= self.custom_info_ids.new({
  96. 'model': self._name,
  97. 'property_id': info_name.id,
  98. "res_id": self.id,
  99. })
  100. @api.multi
  101. def unlink(self):
  102. info_values = self.mapped('custom_info_ids')
  103. res = super(CustomInfo, self).unlink()
  104. if res:
  105. info_values.unlink()
  106. return res