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.

96 lines
3.7 KiB

  1. # Copyright 2015 Sergio Teruel <sergio.teruel@tecnativa.com>
  2. # Copyright 2015 Carlos Dauden <carlos.dauden@tecnativa.com>
  3. # Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  4. # Copyright 2017 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  5. # License LGPL-3 - See http://www.gnu.org/licenses/lgpl-3.0.html
  6. from odoo import api, fields, models
  7. class CustomInfo(models.AbstractModel):
  8. """Models that inherit from this one will get custom information for free!
  9. They will probably want to declare a default model in the context of the
  10. :attr:`custom_info_template_id` field.
  11. See example in :mod:`res_partner`.
  12. """
  13. _description = "Inheritable abstract model to add custom info in any model"
  14. _name = "custom.info"
  15. custom_info_template_id = fields.Many2one(
  16. comodel_name='custom.info.template',
  17. domain=lambda self: [("model", "=", self._name)],
  18. string='Custom Information Template',
  19. )
  20. custom_info_ids = fields.One2many(
  21. comodel_name='custom.info.value', inverse_name='res_id',
  22. domain=lambda self: [("model", "=", self._name)],
  23. auto_join=True, string='Custom Properties',
  24. )
  25. # HACK: Until https://github.com/odoo/odoo/pull/10557 is merged
  26. # https://github.com/OCA/server-tools/pull/492#issuecomment-237594285
  27. @api.multi
  28. def onchange(self, values, field_name, field_onchange): # pragma: no cover
  29. x2many_field = 'custom_info_ids'
  30. if x2many_field in field_onchange:
  31. subfields = getattr(self, x2many_field)._fields.keys()
  32. for subfield in subfields:
  33. field_onchange.setdefault(
  34. u"{}.{}".format(x2many_field, subfield), u"",
  35. )
  36. return super().onchange(
  37. values, field_name, field_onchange,
  38. )
  39. @api.onchange('custom_info_template_id')
  40. def _onchange_custom_info_template_id(self):
  41. tmpls = self.all_custom_info_templates()
  42. props_good = tmpls.mapped("property_ids")
  43. props_enabled = self.mapped("custom_info_ids.property_id")
  44. to_add = props_good - props_enabled
  45. to_remove = props_enabled - props_good
  46. values = self.custom_info_ids
  47. values = values.filtered(lambda r: r.property_id not in to_remove)
  48. for prop in to_add.sorted():
  49. newvalue = self.custom_info_ids.new({
  50. "property_id": prop.id,
  51. "res_id": self.id,
  52. "value": prop.default_value,
  53. })
  54. newvalue._onchange_property_set_default_value()
  55. newvalue._inverse_value()
  56. newvalue._compute_value()
  57. values += newvalue
  58. self.custom_info_ids = values
  59. # Default values implied new templates? Then this is recursive
  60. if self.all_custom_info_templates() != tmpls:
  61. self._onchange_custom_info_template_id()
  62. @api.multi
  63. def unlink(self):
  64. """Remove linked custom info this way, as can't be handled
  65. automatically.
  66. """
  67. info_values = self.mapped('custom_info_ids')
  68. res = super().unlink()
  69. if res:
  70. info_values.unlink()
  71. return res
  72. @api.multi
  73. @api.returns("custom.info.value")
  74. def get_custom_info_value(self, properties):
  75. """Get ``custom.info.value`` records for the given property."""
  76. return self.env["custom.info.value"].search([
  77. ("model", "=", self._name),
  78. ("res_id", "in", self.ids),
  79. ("property_id", "in", properties.ids),
  80. ])
  81. @api.multi
  82. def all_custom_info_templates(self):
  83. """Get all custom info templates involved in these owners."""
  84. return (self.mapped("custom_info_template_id") |
  85. self.mapped("custom_info_ids.value_id.template_id"))