[FIX]Fixed the issue of search on tranlsated field, search on tranlsated field fails...
authorMohammed Shekha <msh@openerp.com>
Wed, 5 Jun 2013 09:53:42 +0000 (15:23 +0530)
committerMohammed Shekha <msh@openerp.com>
Wed, 5 Jun 2013 09:53:42 +0000 (15:23 +0530)
bzr revid: msh@openerp.com-20130605095342-314zfmttymx1k86a

openerp/osv/expression.py

index 58f3514..b2184f7 100644 (file)
@@ -1020,37 +1020,32 @@ class expression(object):
                 elif field.translate:
                     need_wildcard = operator in ('like', 'ilike', 'not like', 'not ilike')
                     sql_operator = {'=like': 'like', '=ilike': 'ilike'}.get(operator, operator)
+                    instr = ' %s'
                     if need_wildcard:
                         right = '%%%s%%' % right
 
-                    subselect = '( SELECT res_id'          \
-                             '    FROM ir_translation'  \
-                             '   WHERE name = %s'       \
-                             '     AND lang = %s'       \
-                             '     AND type = %s'
-                    instr = ' %s'
+                    select = 'WITH temp_'+working_model._table+' (id, name) as (' \
+                        'SELECT wt.id, coalesce(it.value,wt.name) ' \
+                        'FROM '+working_model._table+' wt ' \
+                        'LEFT JOIN ir_translation it ON (it.name = %s and ' \
+                                        'it.lang = %s and ' \
+                                        'it.type = %s and ' \
+                                        'it.res_id = wt.id and ' \
+                                        "it.value != '')" \
+                        ') '
                     #Covering in,not in operators with operands (%s,%s) ,etc.
                     if sql_operator in ['in', 'not in']:
                         instr = ','.join(['%s'] * len(right))
-                        subselect += '     AND value ' + sql_operator + ' ' + " (" + instr + ")"   \
-                             ') UNION ('                \
-                             '  SELECT id'              \
-                             '    FROM "' + working_model._table + '"'       \
-                             '   WHERE "' + left + '" ' + sql_operator + ' ' + " (" + instr + "))"
+                        select += 'SELECT id FROM temp_'+working_model._table+' WHERE "name" '+sql_operator+' ('+instr+') order by name'
                     else:
-                        subselect += '     AND value ' + sql_operator + instr +   \
-                             ') UNION ('                \
-                             '  SELECT id'              \
-                             '    FROM "' + working_model._table + '"'       \
-                             '   WHERE "' + left + '" ' + sql_operator + instr + ")"
+                        select += 'SELECT id FROM temp_'+working_model._table+' WHERE "name" '+sql_operator+' '+instr+' order by name'
 
                     params = [working_model._name + ',' + left,
                               context.get('lang', False) or 'en_US',
                               'model',
                               right,
-                              right,
                              ]
-                    push(create_substitution_leaf(leaf, ('id', 'inselect', (subselect, params)), working_model))
+                    push(create_substitution_leaf(leaf, ('id', 'inselect', (select, params)), working_model))
 
                 else:
                     push_result(leaf)