Commit 9e0b7d71 authored by Todd Etzel's avatar Todd Etzel

Don't raise exceptions during export on column_formatters with macros when...

Don't raise exceptions during export on column_formatters with macros when there is a valid column_formatters_export, column_export_exclude_list or column_export_list configuration available
parent c0f904a5
......@@ -2015,7 +2015,30 @@ class BaseModelView(BaseView, ActionsMixin):
# Macros in column_formatters are not supported.
# Macros will have a function name 'inner'
# This causes non-macro functions named 'inner' not work.
# Skip this check when the field with the macro is excluded...
skip_checks = []
if self.column_export_exclude_list:
skip_checks.extend(self.column_export_exclude_list)
# or there is a macro-less column_formatters_export available.
for col, func in iteritems(self.column_formatters_export):
# we can skip checking columns not being exported
# when the export list is explicitly defined
if self.column_export_list and \
col not in self.column_export_list:
skip_checks.append(col)
continue
if func.__name__ != 'inner':
skip_checks.append(col)
# main macro check loop, skipping the above "safe" skip list
for col, func in iteritems(self.column_formatters):
if col in skip_checks:
continue
if func.__name__ == 'inner':
raise NotImplementedError(
'Macros not implemented. Override with '
......
......@@ -656,3 +656,73 @@ def test_export_csv():
rv = client.get('/admin/macro_exception/export/csv/')
data = rv.data.decode('utf-8')
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