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.

101 lines
3.4 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 CustomInfoTemplate(models.Model):
  8. """Defines custom properties expected for a given database object."""
  9. _name = "custom.info.template"
  10. _description = "Custom information template"
  11. _sql_constraints = [
  12. ("name_model",
  13. "UNIQUE (name, model_id)",
  14. "Another template with that name exists for that model."),
  15. ]
  16. name = fields.Char(translate=True)
  17. model_id = fields.Many2one(comodel_name='ir.model', string='Model')
  18. info_ids = fields.One2many(
  19. comodel_name='custom.info.property',
  20. inverse_name='template_id',
  21. string='Properties')
  22. class CustomInfoProperty(models.Model):
  23. """Name of the custom information property."""
  24. _name = "custom.info.property"
  25. _description = "Custom information property"
  26. _sql_constraints = [
  27. ("name_template",
  28. "UNIQUE (name, template_id)",
  29. "Another property with that name exists for that template."),
  30. ]
  31. name = fields.Char(translate=True)
  32. template_id = fields.Many2one(
  33. comodel_name='custom.info.template',
  34. string='Template')
  35. info_value_ids = fields.One2many(
  36. comodel_name="custom.info.value",
  37. inverse_name="property_id",
  38. string="Property Values")
  39. class CustomInfoValue(models.Model):
  40. _name = "custom.info.value"
  41. _description = "Custom information value"
  42. _rec_name = 'value'
  43. _sql_constraints = [
  44. ("property_model_res",
  45. "UNIQUE (property_id, model, res_id)",
  46. "Another property with that name exists for that resource."),
  47. ]
  48. model = fields.Char(index=True, required=True)
  49. res_id = fields.Integer("Resource ID", index=True, required=True)
  50. property_id = fields.Many2one(
  51. comodel_name='custom.info.property',
  52. required=True,
  53. string='Property')
  54. name = fields.Char(related='property_id.name')
  55. value = fields.Char(translate=True)
  56. class CustomInfo(models.AbstractModel):
  57. _name = "custom.info"
  58. _description = "Inheritable abstract model to add custom info in any model"
  59. custom_info_template_id = fields.Many2one(
  60. comodel_name='custom.info.template',
  61. string='Custom Information Template')
  62. custom_info_ids = fields.One2many(
  63. comodel_name='custom.info.value',
  64. inverse_name='res_id',
  65. domain=lambda self: [('model', '=', self._name)],
  66. auto_join=True,
  67. string='Custom Properties')
  68. @api.onchange('custom_info_template_id')
  69. def _onchange_custom_info_template_id(self):
  70. if not self.custom_info_template_id:
  71. self.custom_info_ids = False
  72. else:
  73. info_list = self.custom_info_ids.mapped('property_id')
  74. for info_name in self.custom_info_template_id.info_ids:
  75. if info_name not in info_list:
  76. self.custom_info_ids |= self.custom_info_ids.new({
  77. 'model': self._name,
  78. 'property_id': info_name.id,
  79. })
  80. @api.multi
  81. def unlink(self):
  82. info_values = self.mapped('custom_info_ids')
  83. res = super(CustomInfo, self).unlink()
  84. if res:
  85. info_values.unlink()
  86. return res