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.8 KiB

10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2014 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv.orm import Model
  22. from openerp.osv import fields
  23. from openerp.tools import drop_view_if_exists
  24. from res_partner_relation_type_selection import ResPartnerRelationTypeSelection
  25. class ResPartnerRelationAll(Model):
  26. _auto = False
  27. _log_access = False
  28. _name = 'res.partner.relation.all'
  29. _description = 'All (non-inverse + inverse) relations between partners'
  30. _additional_view_fields = []
  31. '''append to this list if you added fields to res_partner_relation that
  32. you need in this model and related fields are not adequate (ie for sorting)
  33. You must use the same name as in res_partner_relation.
  34. Don't overwrite this list in your declatarion but append in _auto_init:
  35. def _auto_init(self, cr, context=None):
  36. self._additional_view_fields.append('my_field')
  37. return super(ResPartnerRelationAll, self)._auto_init(
  38. cr, context=context)
  39. _columns = {
  40. 'my_field': ....
  41. }
  42. '''
  43. def _auto_init(self, cr, context=None):
  44. drop_view_if_exists(cr, self._table)
  45. additional_view_fields = ','.join(self._additional_view_fields)
  46. additional_view_fields = (',' + additional_view_fields)\
  47. if additional_view_fields else ''
  48. cr.execute(
  49. '''create or replace view %s as
  50. select
  51. id * 10 as id,
  52. id as relation_id,
  53. type_id,
  54. cast('a' as char(1)) as record_type,
  55. left_partner_id as this_partner_id,
  56. right_partner_id as other_partner_id,
  57. date_start,
  58. date_end,
  59. active,
  60. type_id * 10 as type_selection_id
  61. %s
  62. from res_partner_relation
  63. union select
  64. id * 10 + 1,
  65. id,
  66. type_id,
  67. cast('b' as char(1)),
  68. right_partner_id,
  69. left_partner_id,
  70. date_start,
  71. date_end,
  72. active,
  73. type_id * 10 + 1
  74. %s
  75. from res_partner_relation''' % (
  76. self._table,
  77. additional_view_fields,
  78. additional_view_fields,
  79. )
  80. )
  81. return super(ResPartnerRelationAll, self)._auto_init(
  82. cr, context=context)
  83. _columns = {
  84. 'record_type': fields.selection(
  85. ResPartnerRelationTypeSelection._RECORD_TYPES, 'Record type',
  86. readonly=True),
  87. 'relation_id': fields.many2one(
  88. 'res.partner.relation', 'Relation', readonly=True),
  89. 'type_id': fields.many2one(
  90. 'res.partner.relation.type', 'Relation type', readonly=True),
  91. 'type_selection_id': fields.many2one(
  92. 'res.partner.relation.type.selection', 'Relation type',
  93. readonly=True),
  94. 'this_partner_id': fields.many2one(
  95. 'res.partner', 'Current partner', readonly=True),
  96. 'other_partner_id': fields.many2one(
  97. 'res.partner', 'Other partner', readonly=True),
  98. 'date_start': fields.date('Starting date'),
  99. 'date_end': fields.date('Ending date'),
  100. 'active': fields.boolean('Active'),
  101. }
  102. def name_get(self, cr, uid, ids, context=None):
  103. return dict([
  104. (this.id, '%s %s %s' % (
  105. this.this_partner_id.name,
  106. this.type_selection_id.name_get()[0][1],
  107. this.other_partner_id.name,
  108. ))
  109. for this in self.browse(cr, uid, ids, context=context)])
  110. def write(self, cr, uid, ids, vals, context=None):
  111. '''divert non-problematic writes to underlying table'''
  112. return self.pool['res.partner.relation'].write(
  113. cr, uid,
  114. [i / 10 for i in ids],
  115. dict([(k, vals[k])
  116. for k in vals
  117. if not self._columns[k].readonly]),
  118. context=context)