Commit 05262c19 authored by Paul Brown's avatar Paul Brown

Merge pull request #1194 from toddetzel/master

More Flexible Exports Without Exceptions
parents c0f904a5 433206a0
...@@ -2015,11 +2015,16 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -2015,11 +2015,16 @@ class BaseModelView(BaseView, ActionsMixin):
# Macros in column_formatters are not supported. # Macros in column_formatters are not supported.
# Macros will have a function name 'inner' # Macros will have a function name 'inner'
# This causes non-macro functions named 'inner' not work. # This causes non-macro functions named 'inner' not work.
for col, func in iteritems(self.column_formatters): for col, func in iteritems(self.column_formatters_export):
# skip checking columns not being exported
if col not in [col for col, _ in self._export_columns]:
continue
if func.__name__ == 'inner': if func.__name__ == 'inner':
raise NotImplementedError( raise NotImplementedError(
'Macros not implemented. Override with ' 'Macros are not implemented in export. Exclude column in'
'column_formatters_export. Column: %s' % (col,) ' column_formatters_export, column_export_list, or '
' column_export_exclude_list. Column: %s' % (col,)
) )
# Grab parameters from URL # Grab parameters from URL
......
...@@ -656,3 +656,73 @@ def test_export_csv(): ...@@ -656,3 +656,73 @@ def test_export_csv():
rv = client.get('/admin/macro_exception/export/csv/') rv = client.get('/admin/macro_exception/export/csv/')
data = rv.data.decode('utf-8') data = rv.data.decode('utf-8')
eq_(rv.status_code, 500) eq_(rv.status_code, 500)
# We should be able to specify column_formatters_export
# and not get an exception if a column_formatter is using a macro
def export_formatter(v, c, m, p):
return m.col1 if m else ''
view = MockModelView(
Model, view_data, can_export=True, column_list=['col1', 'col2'],
column_formatters=dict(col1=macro('render_macro')),
column_formatters_export=dict(col1=export_formatter),
endpoint="macro_exception_formatter_override"
)
admin.add_view(view)
rv = client.get('/admin/macro_exception_formatter_override/export/csv/')
data = rv.data.decode('utf-8')
eq_(rv.status_code, 200)
ok_("Col1,Col2\r\n"
"col1_1,1\r\n"
"col1_2,2\r\n"
",3\r\n" == data)
# We should not get an exception if a column_formatter is
# using a macro but it is on the column_export_exclude_list
view = MockModelView(
Model, view_data, can_export=True, column_list=['col1', 'col2'],
column_formatters=dict(col1=macro('render_macro')),
column_export_exclude_list=['col1'],
endpoint="macro_exception_exclude_override"
)
admin.add_view(view)
rv = client.get('/admin/macro_exception_exclude_override/export/csv/')
data = rv.data.decode('utf-8')
eq_(rv.status_code, 200)
ok_("Col2\r\n"
"1\r\n"
"2\r\n"
"3\r\n" == data)
# When we use column_export_list to hide the macro field
# we should not get an exception
view = MockModelView(
Model, view_data, can_export=True, column_list=['col1', 'col2'],
column_formatters=dict(col1=macro('render_macro')),
column_export_list=['col2'],
endpoint="macro_exception_list_override"
)
admin.add_view(view)
rv = client.get('/admin/macro_exception_list_override/export/csv/')
data = rv.data.decode('utf-8')
eq_(rv.status_code, 200)
ok_("Col2\r\n"
"1\r\n"
"2\r\n"
"3\r\n" == data)
# If they define a macro on the column_formatters_export list
# then raise an exception
view = MockModelView(
Model, view_data, can_export=True, column_list=['col1', 'col2'],
column_formatters=dict(col1=macro('render_macro')),
endpoint="macro_exception_macro_override"
)
admin.add_view(view)
rv = client.get('/admin/macro_exception_macro_override/export/csv/')
data = rv.data.decode('utf-8')
eq_(rv.status_code, 500)
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