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.

168 lines
6.7 KiB

  1. # -*- coding: UTF-8 -*-
  2. '''
  3. Created on 23 may 2014
  4. @author: Ronald Portier, Therp
  5. rportier@therp.nl
  6. http://www.therp.nl
  7. For the model defined here _auto is set to False to prevent creating a
  8. database file. All i/o operations are overridden to use a sql SELECT that
  9. takes data from res_partner_connection_type where each type is included in the
  10. result set twice, so it appears that the connection type and the inverse
  11. type are separate records..
  12. The original function _auto_init is still called because this function
  13. normally (if _auto == True) not only creates the db tables, but it also takes
  14. care of registering all fields in ir_model_fields. This is needed to make
  15. the field labels translatable.
  16. example content for last lines of _statement:
  17. select id, record_type,
  18. customer_id, customer_name, customer_city, customer_zip, customer_street,
  19. caller_id, caller_name, caller_phone, caller_fax, caller_email
  20. from FULL_LIST as ResPartnerRelationTypeSelection where record_type = 'c'
  21. ORDER BY ResPartnerRelationTypeSelection.customer_name asc,
  22. ResPartnerRelationTypeSelection.caller_name asc;
  23. '''
  24. from openerp.osv import fields
  25. from openerp.osv import orm
  26. from openerp.tools import drop_view_if_exists
  27. from openerp.addons.partner_relations.model.res_partner_relation_type\
  28. import ResPartnerRelationType
  29. class ResPartnerRelationTypeSelection(orm.Model):
  30. '''Virtual relation types'''
  31. _RECORD_TYPES = [
  32. ('a', 'Type'),
  33. ('b', 'Inverse type'),
  34. ]
  35. _auto = False # Do not try to create table in _auto_init(..)
  36. _log_access = False
  37. def get_type_from_selection_id(self, cr, uid, selection_id):
  38. '''Selection id ic computed from id of underlying type and the
  39. kind of record. This function does the inverse computation to give
  40. back the original type id, and about the record type.'''
  41. type_id = selection_id / 10
  42. is_reverse = (selection_id % 10) > 0
  43. return (type_id, is_reverse)
  44. def _auto_init(self, cr, context=None):
  45. drop_view_if_exists(cr, self._table)
  46. # TODO: we lose field value's translations here.
  47. # probably we need to patch ir_translation.get_source for that
  48. # to get res_partner_relation_type's translations
  49. cr.execute(
  50. '''create or replace view %s as
  51. select
  52. id * 10 as id,
  53. id as type_id,
  54. cast('a' as char(1)) as record_type,
  55. name as name,
  56. contact_type_left as contact_type_this,
  57. contact_type_right as contact_type_other,
  58. partner_category_left as partner_category_this,
  59. partner_category_right as partner_category_other
  60. from res_partner_relation_type
  61. union select
  62. id * 10 + 1,
  63. id,
  64. cast('b' as char(1)),
  65. name_inverse,
  66. contact_type_right,
  67. contact_type_left,
  68. partner_category_right,
  69. partner_category_left
  70. from res_partner_relation_type''' % self._table)
  71. return super(ResPartnerRelationTypeSelection, self)._auto_init(
  72. cr, context=context)
  73. def _search_partner_category_this(self, cr, uid, obj, field_name, args,
  74. context=None):
  75. category_ids = []
  76. for arg in args:
  77. if isinstance(arg, tuple) and arg[0] == field_name\
  78. and (arg[1] == '=' or arg[1] == 'in'):
  79. # TODO don't we have an api function to eval that?
  80. for delta in arg[2]:
  81. if delta[0] == 6:
  82. category_ids.extend(delta[2])
  83. if category_ids:
  84. return [
  85. '|',
  86. ('partner_category_this', '=', False),
  87. ('partner_category_this', 'in', category_ids),
  88. ]
  89. else:
  90. return [('partner_category_this', '=', False)]
  91. _name = 'res.partner.relation.type.selection'
  92. _description = 'All relation types'
  93. _foreign_keys = []
  94. _columns = {
  95. 'record_type': fields.selection(_RECORD_TYPES, 'Record type', size=16),
  96. 'type_id': fields.many2one(
  97. 'res.partner.relation.type', 'Type'),
  98. 'name': fields.char('Name', size=64),
  99. 'contact_type_this': fields.selection(
  100. ResPartnerRelationType._get_partner_types.im_func,
  101. 'Current record\'s partner type'),
  102. 'contact_type_other': fields.selection(
  103. ResPartnerRelationType._get_partner_types.im_func,
  104. 'Other record\'s partner type'),
  105. 'partner_category_this': fields.many2one(
  106. 'res.partner.category', 'Current record\'s category'),
  107. 'partner_category_other': fields.many2one(
  108. 'res.partner.category', 'Other record\'s category'),
  109. # search field to handle many2many deltas from the client
  110. 'search_partner_category_this': fields.function(
  111. lambda self, cr, uid, ids, context=None: dict(
  112. [(i, False) for i in ids]),
  113. fnct_search=_search_partner_category_this,
  114. type='many2many', obj='res.partner.category',
  115. string='Current record\'s category'),
  116. }
  117. _order = 'name asc'
  118. def name_get(self, cr, uid, ids, context=None):
  119. 'translate name using translations from res.partner.relation.type'
  120. result = super(ResPartnerRelationTypeSelection, self).name_get(
  121. cr, uid, ids, context=context)
  122. ir_translation = self.pool['ir.translation']
  123. return [
  124. (i, ir_translation._get_source(
  125. cr, uid,
  126. 'res.partner.relation.type,name_inverse'
  127. if self.get_type_from_selection_id(cr, uid, i)[1]
  128. else 'res.partner.relation.type,name',
  129. 'model', context.get('lang'), name))
  130. for i, name in result]
  131. def name_search(self, cr, uid, name='', args=None, operator='ilike',
  132. context=None, limit=100):
  133. 'search for translated names in res.partner.relation.type'
  134. res_partner_relation_type = self.pool['res.partner.relation.type']
  135. relation_ids = res_partner_relation_type.search(
  136. cr, uid, [('name', operator, name)],
  137. context=context)
  138. inverse_relation_ids = res_partner_relation_type.search(
  139. cr, uid, [('name_inverse', operator, name)],
  140. context=context)
  141. all_ids = self.search(
  142. cr, uid,
  143. [
  144. ('id', 'in',
  145. map(lambda x: x * 10, relation_ids) +
  146. map(lambda x: x * 10 + 1, inverse_relation_ids)),
  147. ] + (args or []),
  148. context=context, limit=limit)
  149. return self.name_get(cr, uid, all_ids, context=context)