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.

129 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\
  25. import ResPartnerRelationTypeSelection
  26. class ResPartnerRelationAll(Model):
  27. _auto = False
  28. _log_access = False
  29. _name = 'res.partner.relation.all'
  30. _description = 'All (non-inverse + inverse) relations between partners'
  31. _additional_view_fields = []
  32. '''append to this list if you added fields to res_partner_relation that
  33. you need in this model and related fields are not adequate (ie for sorting)
  34. You must use the same name as in res_partner_relation.
  35. Don't overwrite this list in your declatarion but append in _auto_init:
  36. def _auto_init(self, cr, context=None):
  37. self._additional_view_fields.append('my_field')
  38. return super(ResPartnerRelationAll, self)._auto_init(
  39. cr, context=context)
  40. _columns = {
  41. 'my_field': ....
  42. }
  43. '''
  44. def _auto_init(self, cr, context=None):
  45. drop_view_if_exists(cr, self._table)
  46. additional_view_fields = ','.join(self._additional_view_fields)
  47. additional_view_fields = (',' + additional_view_fields)\
  48. if additional_view_fields else ''
  49. cr.execute(
  50. '''create or replace view %s as
  51. select
  52. id * 10 as id,
  53. id as relation_id,
  54. type_id,
  55. cast('a' as char(1)) as record_type,
  56. left_partner_id as this_partner_id,
  57. right_partner_id as other_partner_id,
  58. date_start,
  59. date_end,
  60. active,
  61. type_id * 10 as type_selection_id
  62. %s
  63. from res_partner_relation
  64. union select
  65. id * 10 + 1,
  66. id,
  67. type_id,
  68. cast('b' as char(1)),
  69. right_partner_id,
  70. left_partner_id,
  71. date_start,
  72. date_end,
  73. active,
  74. type_id * 10 + 1
  75. %s
  76. from res_partner_relation''' % (
  77. self._table,
  78. additional_view_fields,
  79. additional_view_fields,
  80. )
  81. )
  82. return super(ResPartnerRelationAll, self)._auto_init(
  83. cr, context=context)
  84. _columns = {
  85. 'record_type': fields.selection(
  86. ResPartnerRelationTypeSelection._RECORD_TYPES, 'Record type',
  87. readonly=True),
  88. 'relation_id': fields.many2one(
  89. 'res.partner.relation', 'Relation', readonly=True),
  90. 'type_id': fields.many2one(
  91. 'res.partner.relation.type', 'Relation type', readonly=True),
  92. 'type_selection_id': fields.many2one(
  93. 'res.partner.relation.type.selection', 'Relation type',
  94. readonly=True),
  95. 'this_partner_id': fields.many2one(
  96. 'res.partner', 'Current partner', readonly=True),
  97. 'other_partner_id': fields.many2one(
  98. 'res.partner', 'Other partner', readonly=True),
  99. 'date_start': fields.date('Starting date'),
  100. 'date_end': fields.date('Ending date'),
  101. 'active': fields.boolean('Active'),
  102. }
  103. def name_get(self, cr, uid, ids, context=None):
  104. return dict([
  105. (this.id, '%s %s %s' % (
  106. this.this_partner_id.name,
  107. this.type_selection_id.name_get()[0][1],
  108. this.other_partner_id.name,
  109. ))
  110. for this in self.browse(cr, uid, ids, context=context)])
  111. def write(self, cr, uid, ids, vals, context=None):
  112. '''divert non-problematic writes to underlying table'''
  113. return self.pool['res.partner.relation'].write(
  114. cr, uid,
  115. [i / 10 for i in ids],
  116. dict([(k, vals[k])
  117. for k in vals
  118. if not self._columns[k].readonly]),
  119. context=context)