Commit 6e8226a4 authored by Artem Serga's avatar Artem Serga

#4 - Fix filters, presented as string

parent 68b681cf
...@@ -394,8 +394,20 @@ class ModelView(BaseModelView): ...@@ -394,8 +394,20 @@ class ModelView(BaseModelView):
""" """
Return list of enabled filters Return list of enabled filters
""" """
join_tables = []
if isinstance(name, basestring): if isinstance(name, basestring):
attr = getattr(self.model, name, None) model = self.model
for attribute in name.split('.'):
value = getattr(model, attribute)
if (
hasattr(value, 'property')
and hasattr(value.property, 'direction')
):
model = value.property.mapper.class_
table = model.__table__
if self._need_join(table):
join_tables.append(table)
attr = value
else: else:
attr = name attr = name
...@@ -423,9 +435,11 @@ class ModelView(BaseModelView): ...@@ -423,9 +435,11 @@ class ModelView(BaseModelView):
visible_name) visible_name)
if flt: if flt:
if self._need_join(column.table): table = column.table
self._filter_joins[column.table.name] = column.table if join_tables:
self._filter_joins[table.name.name] = join_tables
elif self._need_join(table.name):
self._filter_joins[table.name.name] = [table.name]
filters.extend(flt) filters.extend(flt)
return filters return filters
...@@ -449,9 +463,10 @@ class ModelView(BaseModelView): ...@@ -449,9 +463,10 @@ class ModelView(BaseModelView):
if flt: if flt:
# If there's relation to other table, do it # If there's relation to other table, do it
if self._need_join(column.table): if join_tables:
self._filter_joins[column.table.name] = column.table self._filter_joins[column.table.name] = join_tables
elif self._need_join(column.table):
self._filter_joins[column.table.name] = [column.table]
return flt return flt
def is_valid_filter(self, filter): def is_valid_filter(self, filter):
...@@ -581,10 +596,10 @@ class ModelView(BaseModelView): ...@@ -581,10 +596,10 @@ class ModelView(BaseModelView):
# Figure out join # Figure out join
tbl = flt.column.table.name tbl = flt.column.table.name
join = self._filter_joins.get(tbl) join_tables = self._filter_joins.get(tbl, [])
if join is not None: for table in join_tables:
query = query.join(join) query = query.join(table)
joins.add(tbl) joins.add(table)
# Apply filter # Apply filter
query = flt.apply(query, value) query = flt.apply(query, value)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment