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.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # (c) 2015 Antiun Ingeniería S.L. - Sergio Teruel
  3. # (c) 2015 Antiun Ingeniería S.L. - Carlos Dauden
  4. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  5. from openerp import api, fields, models, _
  6. class CustomInfoTemplate(models.Model):
  7. _name = "custom.info.template"
  8. name = fields.Char()
  9. model_id = fields.Many2one(comodel_name='ir.model', string='Data Model')
  10. info_ids = fields.One2many(
  11. comodel_name='custom.info.template.line',
  12. inverse_name='template_id',
  13. string='Properties')
  14. class CustomInfoTemplateLine(models.Model):
  15. _name = "custom.info.template.line"
  16. name = fields.Char()
  17. template_id = fields.Many2one(
  18. comodel_name='custom.info.template',
  19. string='Template')
  20. info_value_ids = fields.One2many(
  21. comodel_name="custom.info.value",
  22. inverse_name="custom_info_name_id",
  23. string="Property Values")
  24. class CustomInfoValue(models.Model):
  25. _name = "custom.info.value"
  26. _rec_name = 'value'
  27. model = fields.Char(select=True)
  28. res_id = fields.Integer(select=True)
  29. custom_info_name_id = fields.Many2one(
  30. comodel_name='custom.info.template.line',
  31. string='Property Name')
  32. value = fields.Char()
  33. class CustomInfo(models.AbstractModel):
  34. _name = "custom.info"
  35. custom_info_template_id = fields.Many2one(
  36. comodel_name='custom.info.template',
  37. string='Property Template')
  38. custom_info_ids = fields.One2many(
  39. comodel_name='custom.info.value',
  40. inverse_name='res_id',
  41. domain=lambda self: [('model', '=', self._name)],
  42. auto_join=True,
  43. string='Custom Properties')
  44. @api.onchange('custom_info_template_id')
  45. def _onchange_custom_info_template_id(self):
  46. if not self.custom_info_template_id:
  47. self.custom_info_ids = False
  48. else:
  49. info_list = self.custom_info_ids.mapped('custom_info_name_id')
  50. for info_name in self.custom_info_template_id.info_ids:
  51. if info_name not in info_list:
  52. self.custom_info_ids |= self.custom_info_ids.new({
  53. 'model': self._name,
  54. 'custom_info_name_id': info_name.id,
  55. })