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.

147 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(comodel_name="res.partner.relation.type", string="Type")
  32. name = fields.Char("Name")
  33. contact_type_this = fields.Selection(
  34. selection="get_partner_types", string="Current record's partner type"
  35. )
  36. is_inverse = fields.Boolean(
  37. string="Is reverse type?",
  38. help="Inverse relations are from right to left partner.",
  39. )
  40. contact_type_other = fields.Selection(
  41. selection="get_partner_types", string="Other record's partner type"
  42. )
  43. partner_category_this = fields.Many2one(
  44. comodel_name="res.partner.category", string="Current record's category"
  45. )
  46. partner_category_other = fields.Many2one(
  47. comodel_name="res.partner.category", string="Other record's category"
  48. )
  49. allow_self = fields.Boolean(string="Reflexive")
  50. is_symmetric = fields.Boolean(string="Symmetric")
  51. def _get_additional_view_fields(self):
  52. """Allow inherit models to add fields to view.
  53. If fields are added, the resulting string must have each field
  54. prepended by a comma, like so:
  55. return ', typ.allow_self, typ.left_partner_category'
  56. """
  57. return ""
  58. def _get_additional_tables(self):
  59. """Allow inherit models to add tables (JOIN's) to view.
  60. Example:
  61. return 'JOIN type_extention ext ON (bas.type_id = ext.id)'
  62. """
  63. return ""
  64. def _auto_init(self):
  65. cr = self._cr
  66. drop_view_if_exists(cr, self._table)
  67. cr.execute(
  68. """\
  69. CREATE OR REPLACE VIEW %(table)s AS
  70. WITH selection_type AS (
  71. SELECT
  72. id * 2 AS id,
  73. id AS type_id,
  74. name AS name,
  75. False AS is_inverse,
  76. contact_type_left AS contact_type_this,
  77. contact_type_right AS contact_type_other,
  78. partner_category_left AS partner_category_this,
  79. partner_category_right AS partner_category_other
  80. FROM %(underlying_table)s
  81. UNION SELECT
  82. (id * 2) + 1,
  83. id,
  84. name_inverse,
  85. True,
  86. contact_type_right,
  87. contact_type_left,
  88. partner_category_right,
  89. partner_category_left
  90. FROM %(underlying_table)s
  91. WHERE not is_symmetric
  92. )
  93. SELECT
  94. bas.*,
  95. typ.allow_self,
  96. typ.is_symmetric
  97. %(additional_view_fields)s
  98. FROM selection_type bas
  99. JOIN res_partner_relation_type typ ON (bas.type_id = typ.id)
  100. %(additional_tables)s
  101. """,
  102. {
  103. "table": AsIs(self._table),
  104. "underlying_table": AsIs("res_partner_relation_type"),
  105. "additional_view_fields": AsIs(self._get_additional_view_fields()),
  106. "additional_tables": AsIs(self._get_additional_tables()),
  107. },
  108. )
  109. return super(ResPartnerRelationTypeSelection, self)._auto_init()
  110. def name_get(self):
  111. """Get name or name_inverse from underlying model."""
  112. return [
  113. (
  114. this.id,
  115. this.is_inverse
  116. and this.type_id.name_inverse
  117. or this.type_id.display_name,
  118. )
  119. for this in self
  120. ]
  121. @api.model
  122. def name_search(self, name="", args=None, operator="ilike", limit=100):
  123. """Search for name or inverse name in underlying model."""
  124. # pylint: disable=no-value-for-parameter
  125. return self.search(
  126. [
  127. "|",
  128. ("type_id.name", operator, name),
  129. ("type_id.name_inverse", operator, name),
  130. ]
  131. + (args or []),
  132. limit=limit,
  133. ).name_get()