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.

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