Commit 9bfd1966 authored by Serge S. Koval's avatar Serge S. Koval

Merge pull request #1265 from pawl/refactor_get_column_names

SQLA - fix column names error for export and details view
parents 4f43473f c9db0ffb
...@@ -494,43 +494,42 @@ class ModelView(BaseModelView): ...@@ -494,43 +494,42 @@ class ModelView(BaseModelView):
return result return result
def get_list_columns(self): def get_column_names(self, only_columns, excluded_columns):
""" """
Returns a list of tuples with the model field name and formatted Returns a list of tuples with the model field name and formatted
field name. If `column_list` was set, returns it. Otherwise calls field name.
`scaffold_list_columns` to generate the list from the model.
"""
if self.column_list is None:
columns = self.scaffold_list_columns()
# Filter excluded columns Overridden to handle special columns like InstrumentedAttribute.
if self.column_exclude_list:
columns = [c for c in columns
if c not in self.column_exclude_list]
return [(c, self.get_column_name(c)) for c in columns] :param only_columns:
else: List of columns to include in the results. If not set,
columns = [] `scaffold_list_columns` will generate the list from the model.
:param excluded_columns:
List of columns to exclude from the results.
"""
if excluded_columns:
only_columns = [c for c in only_columns if c not in excluded_columns]
for c in self.column_list: formatted_columns = []
column, path = tools.get_field_with_path(self.model, c) for c in only_columns:
column, path = tools.get_field_with_path(self.model, c)
if path: if path:
# column is in another table, use full path # column is a relation (InstrumentedAttribute), use full path
column_name = text_type(c) column_name = text_type(c)
else:
# column is in same table, use only model attribute name
if getattr(column, 'key', None) is not None:
column_name = column.key
else: else:
# column is in same table, use only model attribute name column_name = text_type(c)
if getattr(column, 'key', None) is not None:
column_name = column.key
else:
column_name = text_type(c)
visible_name = self.get_column_name(column_name) visible_name = self.get_column_name(column_name)
# column_name must match column_name in `get_sortable_columns` # column_name must match column_name in `get_sortable_columns`
columns.append((column_name, visible_name)) formatted_columns.append((column_name, visible_name))
return columns return formatted_columns
def init_search(self): def init_search(self):
""" """
......
...@@ -930,23 +930,6 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -930,23 +930,6 @@ class BaseModelView(BaseView, ActionsMixin):
else: else:
return self._prettify_name(field) return self._prettify_name(field)
def get_list_columns(self):
"""
Returns a list of tuples with the model field name and formatted
field name. If `column_list` was set, returns it. Otherwise calls
`scaffold_list_columns` to generate the list from the model.
"""
columns = self.column_list
if columns is None:
columns = self.scaffold_list_columns()
# Filter excluded columns
if self.column_exclude_list:
columns = [c for c in columns if c not in self.column_exclude_list]
return [(c, self.get_column_name(c)) for c in columns]
def get_list_row_actions(self): def get_list_row_actions(self):
""" """
Return list of row action objects, each is instance of :class:`~flask_admin.model.template.BaseListRowAction` Return list of row action objects, each is instance of :class:`~flask_admin.model.template.BaseListRowAction`
...@@ -970,45 +953,63 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -970,45 +953,63 @@ class BaseModelView(BaseView, ActionsMixin):
return actions + (self.column_extra_row_actions or []) return actions + (self.column_extra_row_actions or [])
def get_details_columns(self): def get_column_names(self, only_columns, excluded_columns):
""" """
Returns a list of the model field names in the details view. If Returns a list of tuples with the model field name and formatted
`column_details_list` was set, returns it. Otherwise calls field name.
`scaffold_list_columns` to generate the list from the model.
:param only_columns:
List of columns to include in the results. If not set,
`scaffold_list_columns` will generate the list from the model.
:param excluded_columns:
List of columns to exclude from the results if `only_columns`
is not set.
""" """
columns = self.column_details_list if excluded_columns:
only_columns = [c for c in only_columns if c not in excluded_columns]
if columns is None: return [(c, self.get_column_name(c)) for c in only_columns]
columns = self.scaffold_list_columns()
# Filter excluded columns def get_list_columns(self):
if self.column_details_exclude_list: """
columns = [c for c in columns Uses `get_column_names` to get a list of tuples with the model
if c not in self.column_details_exclude_list] field name and formatted name for the columns in `column_list`
and not in `column_exclude_list`. If `column_list` is not set,
the columns from `scaffold_list_columns` will be used.
"""
return self.get_column_names(
only_columns=self.column_list or self.scaffold_list_columns(),
excluded_columns=self.column_exclude_list,
)
return [(c, self.get_column_name(c)) for c in columns] def get_details_columns(self):
"""
Uses `get_column_names` to get a list of tuples with the model
field name and formatted name for the columns in `column_details_list`
and not in `column_details_exclude_list`. If `column_details_list`
is not set, the columns from `scaffold_list_columns` will be used.
"""
only_columns = self.column_details_list or self.scaffold_list_columns()
return self.get_column_names(
only_columns=only_columns,
excluded_columns=self.column_details_exclude_list,
)
def get_export_columns(self): def get_export_columns(self):
""" """
Returns a list of the model field names in the export view. If Uses `get_column_names` to get a list of tuples with the model
`column_export_list` was set, returns it. Otherwise, if field name and formatted name for the columns in `column_export_list`
`column_list` was set, returns it. Otherwise calls and not in `column_export_exclude_list`. If `column_export_list` is
`scaffold_list_columns` to generate the list from the model. not set, it will attempt to use the columns from `column_list`
or finally the columns from `scaffold_list_columns` will be used.
""" """
columns = self.column_export_list only_columns = (self.column_export_list or self.column_list or
self.scaffold_list_columns())
if columns is None:
columns = self.column_list
if columns is None:
columns = self.scaffold_list_columns()
# Filter excluded columns return self.get_column_names(
if self.column_export_exclude_list: only_columns=only_columns,
columns = [c for c in columns excluded_columns=self.column_export_exclude_list,
if c not in self.column_export_exclude_list] )
return [(c, self.get_column_name(c)) for c in columns]
def scaffold_sortable_columns(self): def scaffold_sortable_columns(self):
""" """
......
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