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.

119 lines
4.2 KiB

11 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2013-2017 Therp BV <http://therp.nl>.
  3. # License AGPL-3.0 or later <http://www.gnu.org/licenses/agpl.html>.
  4. from psycopg2.extensions import AsIs
  5. from openerp.osv.orm import Model
  6. from openerp.osv import fields
  7. from openerp.tools import drop_view_if_exists
  8. from .res_partner_relation_type_selection import _RECORD_TYPES
  9. class ResPartnerRelationAll(Model):
  10. _auto = False
  11. _log_access = False
  12. _name = 'res.partner.relation.all'
  13. _description = 'All (non-inverse + inverse) relations between partners'
  14. _additional_view_fields = []
  15. """append to this list if you added fields to res_partner_relation that
  16. you need in this model and related fields are not adequate (ie for sorting)
  17. You must use the same name as in res_partner_relation.
  18. Don't overwrite this list in your declatarion but append in _auto_init:
  19. def _auto_init(self, cr, context=None):
  20. self._additional_view_fields.append('my_field')
  21. return super(ResPartnerRelationAll, self)._auto_init(
  22. cr, context=context)
  23. _columns = {
  24. 'my_field': ....
  25. }
  26. """
  27. def _auto_init(self, cr, context=None):
  28. """Create view instead of table."""
  29. drop_view_if_exists(cr, self._table)
  30. additional_view_fields = ','.join(self._additional_view_fields)
  31. additional_view_fields = (',' + additional_view_fields)\
  32. if additional_view_fields else ''
  33. cr.execute(
  34. """create or replace view %s as
  35. select
  36. id * 10 as id,
  37. id as relation_id,
  38. type_id,
  39. cast('a' as char(1)) as record_type,
  40. left_partner_id as this_partner_id,
  41. right_partner_id as other_partner_id,
  42. date_start,
  43. date_end,
  44. active,
  45. type_id * 10 as type_selection_id
  46. %s
  47. from res_partner_relation
  48. union select
  49. id * 10 + 1,
  50. id,
  51. type_id,
  52. cast('b' as char(1)),
  53. right_partner_id,
  54. left_partner_id,
  55. date_start,
  56. date_end,
  57. active,
  58. type_id * 10 + 1
  59. %s
  60. from res_partner_relation
  61. """,
  62. params=(
  63. AsIs(self._table),
  64. AsIs(additional_view_fields),
  65. AsIs(additional_view_fields),
  66. )
  67. )
  68. return super(ResPartnerRelationAll, self)._auto_init(
  69. cr, context=context)
  70. _columns = {
  71. 'record_type': fields.selection(
  72. _RECORD_TYPES,
  73. 'Record type',
  74. readonly=True,
  75. ),
  76. 'relation_id': fields.many2one(
  77. 'res.partner.relation', 'Relation', readonly=True),
  78. 'type_id': fields.many2one(
  79. 'res.partner.relation.type', 'Relation type', readonly=True),
  80. 'type_selection_id': fields.many2one(
  81. 'res.partner.relation.type.selection', 'Relation type',
  82. readonly=True),
  83. 'this_partner_id': fields.many2one(
  84. 'res.partner', 'Current partner', readonly=True),
  85. 'other_partner_id': fields.many2one(
  86. 'res.partner', 'Other partner', readonly=True),
  87. 'date_start': fields.date('Starting date'),
  88. 'date_end': fields.date('Ending date'),
  89. 'active': fields.boolean('Active'),
  90. }
  91. def name_get(self, cr, uid, ids, context=None):
  92. """Create name from both partners and relation."""
  93. return dict([
  94. (this.id, '%s %s %s' % (
  95. this.this_partner_id.name,
  96. this.type_selection_id.name_get()[0][1],
  97. this.other_partner_id.name,
  98. ))
  99. for this in self.browse(cr, uid, ids, context=context)])
  100. def write(self, cr, uid, ids, vals, context=None):
  101. """divert non-problematic writes to underlying table"""
  102. # pylint: disable=W8106
  103. return self.pool['res.partner.relation'].write(
  104. cr, uid,
  105. [i / 10 for i in ids],
  106. dict([(k, vals[k])
  107. for k in vals
  108. if not self._columns[k].readonly]),
  109. context=context)