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.

150 lines
4.8 KiB

  1. # Copyright 2013-2017 Therp BV <http://therp.nl>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. """
  4. For the model defined here _auto is set to False to prevent creating a
  5. database file. The model is based on a SQL view based on
  6. res_partner_relation_type where each type is included in the
  7. result set twice, so it appears that the connection type and the inverse
  8. type are separate records..
  9. The original function _auto_init is still called because this function
  10. normally (if _auto == True) not only creates the db tables, but it also takes
  11. care of registering all fields in ir_model_fields. This is needed to make
  12. the field labels translatable.
  13. """
  14. from psycopg2.extensions import AsIs
  15. from odoo import api, fields, models
  16. from odoo.tools import drop_view_if_exists
  17. class ResPartnerRelationTypeSelection(models.Model):
  18. """Virtual relation types"""
  19. _name = 'res.partner.relation.type.selection'
  20. _description = 'All relation types'
  21. _auto = False # Do not try to create table in _auto_init(..)
  22. _foreign_keys = []
  23. _log_access = False
  24. _order = 'name asc'
  25. @api.model
  26. def get_partner_types(self):
  27. """Partner types are defined by model res.partner.relation.type."""
  28. # pylint: disable=no-self-use
  29. rprt_model = self.env['res.partner.relation.type']
  30. return rprt_model.get_partner_types()
  31. type_id = fields.Many2one(
  32. comodel_name='res.partner.relation.type',
  33. string='Type',
  34. )
  35. name = fields.Char('Name')
  36. contact_type_this = fields.Selection(
  37. selection='get_partner_types',
  38. string='Current record\'s partner type',
  39. )
  40. is_inverse = fields.Boolean(
  41. string="Is reverse type?",
  42. help="Inverse relations are from right to left partner.",
  43. )
  44. contact_type_other = fields.Selection(
  45. selection='get_partner_types',
  46. string='Other record\'s partner type',
  47. )
  48. partner_category_this = fields.Many2one(
  49. comodel_name='res.partner.category',
  50. string='Current record\'s category',
  51. )
  52. partner_category_other = fields.Many2one(
  53. comodel_name='res.partner.category',
  54. string='Other record\'s category',
  55. )
  56. allow_self = fields.Boolean(
  57. string='Reflexive',
  58. )
  59. is_symmetric = fields.Boolean(
  60. string='Symmetric',
  61. )
  62. def _get_additional_view_fields(self):
  63. """Allow inherit models to add fields to view.
  64. If fields are added, the resulting string must have each field
  65. prepended by a comma, like so:
  66. return ', typ.allow_self, typ.left_partner_category'
  67. """
  68. return ''
  69. def _get_additional_tables(self):
  70. """Allow inherit models to add tables (JOIN's) to view.
  71. Example:
  72. return 'JOIN type_extention ext ON (bas.type_id = ext.id)'
  73. """
  74. return ''
  75. @api.model_cr_context
  76. def _auto_init(self):
  77. cr = self._cr
  78. drop_view_if_exists(cr, self._table)
  79. cr.execute(
  80. """\
  81. CREATE OR REPLACE VIEW %(table)s AS
  82. WITH selection_type AS (
  83. SELECT
  84. id * 2 AS id,
  85. id AS type_id,
  86. name AS name,
  87. False AS is_inverse,
  88. contact_type_left AS contact_type_this,
  89. contact_type_right AS contact_type_other,
  90. partner_category_left AS partner_category_this,
  91. partner_category_right AS partner_category_other
  92. FROM %(underlying_table)s
  93. UNION SELECT
  94. (id * 2) + 1,
  95. id,
  96. name_inverse,
  97. True,
  98. contact_type_right,
  99. contact_type_left,
  100. partner_category_right,
  101. partner_category_left
  102. FROM %(underlying_table)s
  103. WHERE not is_symmetric
  104. )
  105. SELECT
  106. bas.*,
  107. typ.allow_self,
  108. typ.is_symmetric
  109. %(additional_view_fields)s
  110. FROM selection_type bas
  111. JOIN res_partner_relation_type typ ON (bas.type_id = typ.id)
  112. %(additional_tables)s
  113. """,
  114. {'table': AsIs(self._table),
  115. 'underlying_table': AsIs('res_partner_relation_type'),
  116. 'additional_view_fields':
  117. AsIs(self._get_additional_view_fields()),
  118. 'additional_tables':
  119. AsIs(self._get_additional_tables())})
  120. return super(ResPartnerRelationTypeSelection, self)._auto_init()
  121. @api.multi
  122. def name_get(self):
  123. """Get name or name_inverse from underlying model."""
  124. return [
  125. (this.id,
  126. this.is_inverse and this.type_id.name_inverse or
  127. this.type_id.display_name)
  128. for this in self]
  129. @api.model
  130. def name_search(self, name='', args=None, operator='ilike', limit=100):
  131. """Search for name or inverse name in underlying model."""
  132. # pylint: disable=no-value-for-parameter
  133. return self.search(
  134. ['|',
  135. ('type_id.name', operator, name),
  136. ('type_id.name_inverse', operator, name)] + (args or []),
  137. limit=limit).name_get()