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.

102 lines
4.0 KiB

  1. # Copyright 2014-2018 Therp BV <https://therp.nl>.
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. # pylint: disable=no-member
  4. import logging
  5. from lxml import etree
  6. from odoo import api, fields, models
  7. _logger = logging.getLogger(__name__) # pylint: disable=invalid-name
  8. class ResPartner(models.Model):
  9. _inherit = 'res.partner'
  10. @api.multi
  11. def browse(self, arg=None, prefetch=None):
  12. if "update_relation_tab" in self.env.context:
  13. for tab in self._get_tabs():
  14. fieldname = tab.get_fieldname()
  15. if fieldname not in self._fields:
  16. # Check this for performance reasons.
  17. self.add_field(tab)
  18. return super(ResPartner, self).browse(arg=arg, prefetch=prefetch)
  19. @api.model
  20. def fields_view_get(
  21. self, view_id=None, view_type='form', toolbar=False,
  22. submenu=False):
  23. """Override to add relation tabs to form."""
  24. result = super(ResPartner, self).fields_view_get(
  25. view_id=view_id, view_type=view_type, toolbar=toolbar,
  26. submenu=submenu)
  27. if view_type != 'form' or self.env.context.get('check_view_ids'):
  28. return result
  29. view = etree.fromstring(result['arch'])
  30. extra_fields = self._add_tab_pages(view)
  31. view_model = self.env['ir.ui.view']
  32. result['arch'], original_fields = view_model.postprocess_and_fields(
  33. self._name, view, result['view_id'])
  34. for fieldname in extra_fields:
  35. result['fields'][fieldname] = original_fields[fieldname]
  36. return result
  37. def _add_tab_pages(self, view):
  38. """Adds the relevant tabs to the partner's formview."""
  39. # pylint: disable=no-member
  40. def add_invisible_extra_field(view, extra_fields, fieldname):
  41. """Add invisible field to view."""
  42. view.append(
  43. etree.Element('field', name=fieldname, invisible='True'))
  44. extra_fields.append(fieldname)
  45. last_page_nodes = view.xpath('//page[last()]')
  46. if not last_page_nodes:
  47. # Nothing to do if form contains no pages/tabs.
  48. return []
  49. extra_fields = []
  50. if not view.xpath('//field[@name="id"]'):
  51. add_invisible_extra_field(view, extra_fields, 'id')
  52. last_page = last_page_nodes[0]
  53. for tab in self._get_tabs(): # get all tabs
  54. self.add_field(tab)
  55. add_invisible_extra_field(
  56. view, extra_fields, tab.get_visible_fieldname())
  57. extra_fields.append(tab.get_fieldname())
  58. tab_page = tab.create_page()
  59. last_page.addnext(tab_page)
  60. last_page = tab_page # Keep ordering of tabs
  61. return extra_fields
  62. @api.depends('is_company', 'category_id')
  63. def _compute_tabs_visibility(self):
  64. """Compute for all tabs wether they should be visible."""
  65. if "update_relation_tab" in self.env.context:
  66. for tab in self._get_tabs(): # get all tabs
  67. for this in self:
  68. this[tab.get_visible_fieldname()] = \
  69. tab.compute_visibility(this)
  70. def _get_tabs(self):
  71. tab_model = self.env['res.partner.tab']
  72. return tab_model.get_tabs()
  73. def add_field(self, tab):
  74. """Add tab field to model.
  75. Will replace existing field if already present.
  76. """
  77. # Visible field determines wether first field will be visible.
  78. # This is because domains on many2many no longer work in 9.0
  79. # and above.
  80. visible_field = fields.Boolean(compute='_compute_tabs_visibility')
  81. self._add_field(tab.get_visible_fieldname(), visible_field)
  82. if visible_field not in self._field_computed:
  83. self._field_computed[visible_field] = [visible_field]
  84. tab_field = fields.One2many(
  85. comodel_name='res.partner.relation.all',
  86. inverse_name='this_partner_id',
  87. domain=[('tab_id', '=', tab.tab_record.id)],
  88. string=tab.tab_record.name)
  89. self._add_field(tab.get_fieldname(), tab_field)