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.

103 lines
4.0 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. vals = {
  50. "property_id": prop.id,
  51. "res_id": self.id,
  52. }
  53. if prop.default_value:
  54. if prop.field_type != 'id':
  55. vals["value_%s" % prop.field_type] = prop.default_value
  56. else:
  57. vals["value_id"] = self.env['custom.info.option'].search([
  58. ('property_ids', '=', prop.id),
  59. ('name', '=', prop.default_value)
  60. ], limit=1).id
  61. newvalue = self.custom_info_ids.new(vals)
  62. newvalue._onchange_property_set_default_value()
  63. newvalue._compute_value()
  64. values += newvalue
  65. self.custom_info_ids = values
  66. # Default values implied new templates? Then this is recursive
  67. if self.all_custom_info_templates() != tmpls:
  68. self._onchange_custom_info_template_id()
  69. @api.multi
  70. def unlink(self):
  71. """Remove linked custom info this way, as can't be handled
  72. automatically.
  73. """
  74. info_values = self.mapped('custom_info_ids')
  75. res = super().unlink()
  76. if res:
  77. info_values.unlink()
  78. return res
  79. @api.multi
  80. @api.returns("custom.info.value")
  81. def get_custom_info_value(self, properties):
  82. """Get ``custom.info.value`` records for the given property."""
  83. return self.env["custom.info.value"].search([
  84. ("model", "=", self._name),
  85. ("res_id", "in", self.ids),
  86. ("property_id", "in", properties.ids),
  87. ])
  88. @api.multi
  89. def all_custom_info_templates(self):
  90. """Get all custom info templates involved in these owners."""
  91. return (self.mapped("custom_info_template_id") |
  92. self.mapped("custom_info_ids.value_id.template_id"))