[FIX] bugfix child_of in expressions (to backport to 5.0 branch if no bug is reported)
[odoo/odoo.git] / bin / osv / expression.py
index 7397a82..2ee1492 100644 (file)
@@ -1,7 +1,28 @@
 #!/usr/bin/env python
 # -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution  
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
+#    $Id$
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
 
 from tools import flatten, reverse_enumerate
+import fields
 
 
 class expression(object):
@@ -13,8 +34,7 @@ class expression(object):
     """
 
     def _is_operator(self, element):
-        return isinstance(element, str) \
-           and element in ['&', '|', '!']
+        return isinstance(element, (str, unicode)) and element in ['&', '|', '!']
 
     def _is_leaf(self, element, internal=False):
         OPS = ('=', '!=', '<>', '<=', '<', '>', '>=', '=like', 'like', 'not like', 'ilike', 'not ilike', 'in', 'not in', 'child_of')
@@ -30,7 +50,7 @@ class expression(object):
             subids = ids[i:i+cr.IN_MAX]
             cr.execute('SELECT "%s"'    \
                        '  FROM "%s"'    \
-                       ' WHERE "%s" in (%s)' % (s, f, w, ','.join(['%d']*len(subids))),
+                       ' WHERE "%s" in (%s)' % (s, f, w, ','.join(['%s']*len(subids))),
                        subids)
             res.extend([r[0] for r in cr.fetchall()])
         return res
@@ -52,7 +72,7 @@ class expression(object):
         if not self.__exp:
             return self
 
-        def _rec_get(ids, table, parent, left='id', prefix=''):
+        def _rec_get(ids, table, parent=None, left='id', prefix=''):
             if table._parent_store and (not table.pool._init):
 # TODO: Improve where joins are implemented for many with '.', replace by:
 # doms += ['&',(prefix+'.parent_left','<',o.parent_right),(prefix+'.parent_left','>=',o.parent_left)]
@@ -70,7 +90,7 @@ class expression(object):
                         return []
                     ids2 = table.search(cr, uid, [(parent, 'in', ids)], context=context)
                     return ids + rg(ids2, table, parent)
-                return [(left, 'in', rg(ids, table, parent))]
+                return [(left, 'in', rg(ids, table, parent or table._parent_name))]
 
         self.__main_table = table
 
@@ -94,7 +114,7 @@ class expression(object):
             field = working_table._columns.get(fargs[0], False)
             if not field:
                 if left == 'id' and operator == 'child_of':
-                    dom = _rec_get(right, working_table, working_table._parent_name)
+                    dom = _rec_get(right, working_table)
                     self.__exp = self.__exp[:i] + dom + self.__exp[i+1:]
                 continue
 
@@ -147,7 +167,7 @@ class expression(object):
                             return ids
                         return self.__execute_recursive_in(cr, field._id1, field._rel, field._id2, ids)
 
-                    dom = _rec_get(ids2, field_obj, working_table._parent_name)
+                    dom = _rec_get(ids2, field_obj)
                     ids2 = field_obj.search(cr, uid, dom, context=context)
                     self.__exp[i] = ('id', 'in', _rec_convert(ids2))
                 else:
@@ -165,13 +185,15 @@ class expression(object):
 
                     self.__operator = 'in'
                     if field._obj != working_table._name:
-                        dom = _rec_get(ids2, field_obj, working_table._parent_name, left=left, prefix=field._obj)
+                        dom = _rec_get(ids2, field_obj, left=left, prefix=field._obj)
                     else:
-                        dom = _rec_get(ids2, working_table, left)
+                        dom = _rec_get(ids2, working_table, parent=left)
                     self.__exp = self.__exp[:i] + dom + self.__exp[i+1:]
                 else:
-                    if isinstance(right, basestring):
-                        res_ids = field_obj.name_search(cr, uid, right, [], operator, limit=None)
+                    if isinstance(right, basestring): # and not isinstance(field, fields.related):
+                        c = context.copy()
+                        c['active_test'] = False
+                        res_ids = field_obj.name_search(cr, uid, right, [], operator, limit=None, context=c)
                         right = map(lambda x: x[0], res_ids)
                         self.__exp[i] = (left, 'in', right)
             else:
@@ -223,7 +245,7 @@ class expression(object):
 
             if len_after:
                 if left == 'id':
-                     instr = ','.join(['%d'] * len_after)
+                    instr = ','.join(['%s'] * len_after)
                 else:
                     instr = ','.join([table._columns[left]._symbol_set[0]] * len_after)
                 query = '(%s.%s %s (%s))' % (table._table, left, operator, instr)
@@ -232,10 +254,10 @@ class expression(object):
                 query = '(%s OR %s IS NULL)' % (query, left)
         else:
             params = []
-            if (right == False or right is None) and operator == '=':
-                query = '%s IS NULL' % left
-            elif (right == False or right is None) and operator in ['<>', '!=']:
-                query = '%s IS NOT NULL' % left
+            if (((right == False) and (type(right)==bool)) or (right is None)) and (operator == '='):
+                query = '%s.%s IS NULL' % (table._table, left)
+            elif (((right == False) and (type(right)==bool)) or right is None) and (operator in ['<>', '!=']):
+                query = '%s.%s IS NOT NULL' % (table._table, left)
             else:
                 if left == 'id':
                     query = '%s.id %s %%s' % (table._table, operator)