Reviewers: ,
Please review this at http://codereview.tryton.org/369001/
Affected files:
M trytond/model/modelsql.py
Index: trytond/model/modelsql.py
===================================================================
--- a/trytond/model/modelsql.py
+++ b/trytond/model/modelsql.py
@@ -1363,6 +1363,45 @@
qu1 = '(' + qu1 + ')'
return qu1, qu2
+ def column_get(self, field_name):
+ field = self._columns.get(field_name, False)
+ if not field:
+ if not field_name in self._inherit_fields:
+ raise Exception('ValidateError', 'Field "%s" doesn\'t ' \
+ 'exist on "%s"' % (field_name, self._name))
+ pool=Pool()
+ table = pool.get(self._inherit_fields[field_name][0])
+ field = table.column_get(field_name)
+ return field
+
+ def get_table_for_field(self, field):
+ if field in self._inherit_fields:
+ pool=Pool()
+ parent = pool.get(self._inherit_fields[field][0])
+ return parent.get_table_for_field(field)
+ else:
+ return self
+
+ def get_inherits_join(self, field):
+ if field not in self._inherit_fields:
+ return []
+ pool = Pool()
+ itable = pool.get(self._inherit_fields[field][0])
+ table_query = ''
+ table_arg = []
+ if itable.table_query():
+ table_query, table_args = self.table_query()
+ table_query = '(' + table_query + ') AS '
+ table_join = 'LEFT JOIN ' + table_query + \
+ '"' + itable._table + '" ON ' \
+ '"%s".id = "%s"."%s"' % (itable._table, self._table,
+ self._inherits[itable._name])
+ inherits=[(table_join,table_arg)]
+ inherits.extend(itable.get_inherits_join(field))
+ return inherits
+
+
+
def __search_domain_calc(self, domain, tables, tables_args):
pool = Pool()
domain = domain[:]
@@ -1378,26 +1417,18 @@
table = self
fargs = domain[i][0].split('.', 1)
if fargs[0] in self._inherit_fields:
- itable = pool.get(self._inherit_fields[fargs[0]][0])
- table_query = ''
- table_arg = []
- if itable.table_query():
- table_query, table_args = self.table_query()
- table_query = '(' + table_query + ') AS '
- table_join = 'LEFT JOIN ' + table_query + \
- '"' + itable._table + '" ON ' \
- '"%s".id = "%s"."%s"' % (itable._table,
self._table,
- self._inherits[itable._name])
- if table_join not in tables:
- tables.append(table_join)
- tables_args.extend(table_arg)
- field = table._columns.get(fargs[0], False)
+ inherit_joins = self.get_inherits_join(fargs[0])
+ for (table_join, table_arg) in inherit_joins:
+ if table_join not in tables:
+ tables.append(table_join)
+ tables_args.extend(table_arg)
+ field = self.column_get(fargs[0])
if not field:
if not fargs[0] in self._inherit_fields:
raise Exception('ValidateError', 'Field "%s"
doesn\'t ' \
'exist on "%s"' % (fargs[0], self._name))
table = pool.get(self._inherit_fields[fargs[0]][0])
- field = table._columns.get(fargs[0], False)
+ field = table._columns.get(fargs[0],False)
if len(fargs) > 1:
if field._type == 'many2one':
target_obj = pool.get(field.model_name)
@@ -1718,9 +1749,10 @@
qu1, qu2 = [], []
for arg in domain:
- table = self
- if len(arg) > 3:
+ table = self.get_table_for_field(arg[0])
+ if table==self and len(arg) > 3:
table = arg[3]
+ column=table.column_get(arg[0])
if arg[1] in ('inselect', 'notinselect'):
clause = 'IN'
if arg[1] == 'notinselect':
@@ -1731,7 +1763,7 @@
elif arg[1] in ('in', 'not in'):
if len(arg[2]) > 0:
todel = []
- if table._columns[arg[0]]._type != 'boolean':
+ if column._type != 'boolean':
for xitem in range(len(arg[2])):
if (arg[2][xitem] is False
or arg[2][xitem] is None):
@@ -1739,7 +1771,7 @@
arg2 = arg[2][:]
for xitem in todel[::-1]:
del arg2[xitem]
- arg2 =
[FIELDS[table._columns[arg[0]]._type].sql_format(x)
+ arg2 = [FIELDS[column._type].sql_format(x)
for x in arg2]
if len(arg2):
if reduce(lambda x, y: (x
@@ -1758,7 +1790,7 @@
('%s',) * len(arg2))))
qu2 += arg2
if todel:
- if table._columns[arg[0]]._type == 'boolean':
+ if column._type == 'boolean':
if arg[1] == 'in':
qu1[-1] = '(' + qu1[-1] + ' OR ' \
'"%s"."%s" = %%s)' % \
@@ -1779,7 +1811,7 @@
'"%s"."%s" IS NOT NULL)' % \
(table._table, arg[0])
elif todel:
- if table._columns[arg[0]]._type == 'boolean':
+ if column._type == 'boolean':
if arg[1] == 'in':
qu1.append('("%s"."%s" = %%s)' % \
(table._table, arg[0]))
@@ -1804,7 +1836,7 @@
qu2.append(True)
else:
if (arg[2] is False or arg[2] is None) and (arg[1] == '='):
- if table._columns[arg[0]]._type == 'boolean':
+ if column._type == 'boolean':
qu1.append('("%s"."%s" = %%s)' % \
(table._table, arg[0]))
qu2.append(False)
@@ -1818,7 +1850,7 @@
if arg[0] == 'id':
qu1.append('("%s"."%s" %s %%s)' % \
(table._table, arg[0], arg[1]))
- qu2.append(FIELDS[table._columns[arg[0]]._type].\
+ qu2.append(FIELDS[column._type].\
sql_format(arg[2]))
else:
add_null = False
@@ -1828,21 +1860,23 @@
add_null = True
else:
qu2.append(FIELDS[
- table._columns[arg[0]]._type
+ column._type
].sql_format(arg[2]))
elif arg[1] in ('not like', 'not ilike'):
if not arg[2]:
qu2.append('')
else:
qu2.append(FIELDS[
- table._columns[arg[0]]._type
+ column._type
].sql_format(arg[2]))
add_null = True
else:
- if arg[0] in table._columns:
+ if column:
qu2.append(FIELDS[
- table._columns[arg[0]]._type
+ column._type
].sql_format(arg[2]))
+ else:
+ continue
qu1.append('("%s"."%s" %s %%s)' % (table._table,
arg[0], arg[1]))
if add_null:
--
[email protected] mailing list