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.

128 lines
4.2 KiB

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