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.

83 lines
2.9 KiB

  1. # Copyright 2014-2018 Therp BV <https://therp.nl>.
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. import logging
  4. from lxml import etree
  5. from odoo import _
  6. from odoo.osv.orm import transfer_modifiers_to_node
  7. _logger = logging.getLogger(__name__) # pylint: disable=invalid-name
  8. NAME_PREFIX = 'relation_ids_tab'
  9. class Tab(object):
  10. """Encapsulate the information on a tab in the database."""
  11. def __init__(self, tab_record):
  12. """Create tab from tab_record.
  13. In this version tab_record can be assumed to be a partner.relation.tab.
  14. """
  15. self.tab_record = tab_record
  16. self.name = tab_record.code
  17. def get_fieldname(self):
  18. return '%s_%s' % (NAME_PREFIX, self.tab_record.id)
  19. def get_visible_fieldname(self):
  20. return '%s_visible' % self.get_fieldname()
  21. def create_page(self):
  22. tab_page = etree.Element('page')
  23. self._set_page_attrs(tab_page)
  24. field = etree.Element(
  25. 'field',
  26. name=self.get_fieldname(),
  27. context='{'
  28. '"default_this_partner_id": id,'
  29. '"default_tab_id": %d,'
  30. '"active_test": False}' % self.tab_record.id)
  31. tab_page.append(field)
  32. tree = etree.Element('tree', editable='bottom')
  33. field.append(tree)
  34. # Now add fields for the editable tree view in the tab.
  35. type_field = etree.Element(
  36. 'field',
  37. name='type_selection_id',
  38. widget='many2one')
  39. type_field.set('domain', repr([('tab_id', '=', self.tab_record.id)]))
  40. type_field.set('options', repr({'no_create': True}))
  41. tree.append(type_field)
  42. other_partner_field = etree.Element(
  43. 'field',
  44. string=_('Partner'),
  45. name='other_partner_id',
  46. widget='many2one')
  47. other_partner_field.set('options', repr({'no_create': True}))
  48. tree.append(other_partner_field)
  49. tree.append(etree.Element('field', name='date_start'))
  50. tree.append(etree.Element('field', name='date_end'))
  51. return tab_page
  52. def _set_page_attrs(self, tab_page):
  53. tab_page.set('string', self.tab_record.name)
  54. attrs = {'invisible': [(self.get_visible_fieldname(), '=', False)]}
  55. tab_page.set('attrs', repr(attrs))
  56. transfer_modifiers_to_node(attrs, tab_page)
  57. def compute_visibility(self, partner):
  58. """Compute visibility, dependent on partner and conditions."""
  59. tab = self.tab_record
  60. if tab.partner_ids:
  61. return partner in tab.partner_ids
  62. if tab.contact_type:
  63. is_company_tab = tab.contact_type == 'c'
  64. if partner.is_company != is_company_tab:
  65. return False
  66. if tab.partner_category_id:
  67. if tab.partner_category_id not in partner.category_id:
  68. return False
  69. return True