Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test tsv model export using tablib #2399

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flask_admin/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ class MyModelView(BaseModelView):
A list of available export filetypes. `csv` only is default, but any
filetypes supported by tablib can be used.

Check tablib for https://github.com/kennethreitz/tablib/blob/master/README.rst
Check tablib for https://tablib.readthedocs.io/en/stable/formats.html
for supported types.
"""

Expand Down
25 changes: 25 additions & 0 deletions flask_admin/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,31 @@ def export_formatter(v, c, m, p):
assert rv.status_code == 500


def test_export_tablib(app, admin):
client = app.test_client()

# basic test of tsv export with a few records using tablib
view_data = {
1: Model(1, "col1_1", "col2_1"),
2: Model(2, "col1_2", "col2_2"),
3: Model(3, "col1_3", "col2_3"),
}

view = MockModelView(Model, view_data, can_export=True,
column_list=['col1', 'col2'],
export_types=['tsv'])
admin.add_view(view)

rv = client.get('/admin/model/export/tsv/')
data = rv.data.decode('utf-8')
assert rv.mimetype == 'text/tab-separated-values'
assert rv.status_code == 200
assert "Col1\tCol2\r\n" + \
"col1_1\tcol2_1\r\n" + \
"col1_2\tcol2_2\r\n" + \
"col1_3\tcol2_3\r\n" == data


def test_list_row_actions(app, admin):
client = app.test_client()

Expand Down
1 change: 1 addition & 0 deletions requirements/tests.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ arrow<0.14.0
colour
email-validator
wtforms
tablib
Loading