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.

97 lines
3.8 KiB

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