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.

90 lines
3.5 KiB

  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 import SUPERUSER_ID
  24. class ResPartnerRelationType(Model):
  25. _inherit = 'res.partner.relation.type'
  26. _columns = {
  27. 'own_tab_left': fields.boolean('Show in own tab'),
  28. 'own_tab_right': fields.boolean('Show in own tab'),
  29. }
  30. _defaults = {
  31. 'own_tab_left': False,
  32. 'own_tab_right': False,
  33. }
  34. def _update_res_partner_fields(self, cr):
  35. field_name_prefix = 'relation_ids_own_tab_'
  36. field_name_format = field_name_prefix + '%s_%s'
  37. res_partner = self.pool['res.partner']
  38. for field_name in res_partner._columns.copy():
  39. if field_name.startswith(field_name_prefix):
  40. del res_partner._columns[field_name]
  41. def add_field(relation, inverse):
  42. field = fields.one2many(
  43. 'res.partner.relation',
  44. '%s_partner_id' % ('left' if not inverse else 'right'),
  45. string=relation['name' if not inverse else 'name_inverse'],
  46. domain=[('type_id', '=', relation.id),
  47. '|',
  48. ('active', '=', True),
  49. ('active', '=', False)])
  50. field_name = field_name_format % (
  51. relation.id,
  52. 'left' if not inverse else 'right')
  53. res_partner._columns[field_name] = field
  54. for relation in self.browse(
  55. cr, SUPERUSER_ID,
  56. self.search(
  57. cr, SUPERUSER_ID,
  58. [
  59. '|',
  60. ('own_tab_left', '=', True),
  61. ('own_tab_right', '=', True),
  62. ])):
  63. if relation.own_tab_left:
  64. add_field(relation, False)
  65. if relation.own_tab_right:
  66. add_field(relation, True)
  67. def _register_hook(self, cr):
  68. self._update_res_partner_fields(cr)
  69. def create(self, cr, uid, vals, context=None):
  70. result = super(ResPartnerRelationType, self).create(
  71. cr, uid, vals, context=context)
  72. if vals.get('own_tab_left') or vals.get('own_tab_right'):
  73. self._update_res_partner_fields(cr)
  74. return result
  75. def write(self, cr, uid, ids, vals, context=None):
  76. result = super(ResPartnerRelationType, self).write(
  77. cr, uid, ids, vals, context=context)
  78. if 'own_tab_left' in vals or 'own_tab_right' in vals:
  79. self._update_res_partner_fields(cr)
  80. return result