Skip to content

Commit 2d5fd9a

Browse files
committed
Merge pull request #8 from abehrens/file_name_override
credit goes to @abehrens, adding file_name option to make_response
2 parents 158d12e + 5257b14 commit 2d5fd9a

File tree

8 files changed

+78
-30
lines changed

8 files changed

+78
-30
lines changed

README.rst

100644100755
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ Here are some example codes::
8585

8686
from flask import Flask, request, jsonify
8787
from flask.ext import excel
88-
88+
8989
app=Flask(__name__)
90-
90+
9191
@app.route("/upload", methods=['GET', 'POST'])
9292
def upload_file():
9393
if request.method == 'POST':
@@ -100,11 +100,15 @@ Here are some example codes::
100100
<p><input type=file name=file><input type=submit value=Upload>
101101
</form>
102102
'''
103-
103+
104104
@app.route("/download", methods=['GET'])
105105
def download_file():
106106
return excel.make_response_from_array([[1,2], [3, 4]], "csv")
107-
107+
108+
@app.route("/export", methods=['GET'])
109+
def export_records():
110+
return excel.make_response_from_array([[1,2], [3, 4]], "csv", file_name="export_data")
111+
108112
if __name__ == "__main__":
109113
app.run()
110114

VERSION

100644100755
File mode changed.

doc/source/index.rst

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ A minimal application may look like this::
101101
def download_file():
102102
return excel.make_response_from_array([[1,2], [3, 4]], "csv")
103103

104+
@app.route("/export", methods=['GET'])
105+
def export_records():
106+
return excel.make_response_from_array([[1,2], [3, 4]], "csv", file_name="export_data")
107+
104108
# insert database related code here
105109
106110
if __name__ == "__main__":
@@ -351,51 +355,57 @@ Response methods
351355
* 'ods'
352356

353357
:param status: unless a different status is to be returned.
354-
355-
.. method:: make_response_from_array(array, file_type, status=200)
358+
:param file_name: provide a custom file name for the response, excluding the file extension
359+
360+
.. method:: make_response_from_array(array, file_type, status=200, file_name=None)
356361

357362
:param array: a list of lists
358363
:param file_type: same as :meth:`~flask_excel.make_response`
359364
:param status: same as :meth:`~flask_excel.make_response`
360-
361-
.. method:: make_response_from_dict(dict, file_type, status=200)
365+
:param file_name: same as :meth:`~flask_excel.make_response`
366+
367+
.. method:: make_response_from_dict(dict, file_type, status=200, file_name=None)
362368

363369
:param dict: a dictinary of lists
364370
:param file_type: same as :meth:`~flask_excel.make_response`
365371
:param status: same as :meth:`~flask_excel.make_response`
366-
367-
.. method:: make_response_from_records(records, file_type, status=200)
372+
:param file_name: same as :meth:`~flask_excel.make_response`
373+
374+
.. method:: make_response_from_records(records, file_type, status=200, file_name=None)
368375

369376
:param records: a list of dictionaries
370377
:param file_type: same as :meth:`~flask_excel.make_response`
371378
:param status: same as :meth:`~flask_excel.make_response`
372-
373-
374-
.. method:: make_response_from_book_dict(book_dict, file_type, status=200)
379+
:param file_name: same as :meth:`~flask_excel.make_response`
380+
381+
.. method:: make_response_from_book_dict(book_dict, file_type, status=200, file_name=None)
375382

376383
:param book_dict: a dictionary of two dimensional arrays
377384
:param file_type: same as :meth:`~flask_excel.make_response`
378385
:param status: same as :meth:`~flask_excel.make_response`
386+
:param file_name: same as :meth:`~flask_excel.make_response`
379387

380-
.. method:: make_response_from_a_table(session, table, file_type status=200)
388+
.. method:: make_response_from_a_table(session, table, file_type status=200, file_name=None)
381389

382390
Produce a single sheet Excel book of *file_type*
383391

384392
:param session: SQLAlchemy session
385393
:param table: a SQLAlchemy table
386394
:param file_type: same as :meth:`~flask_excel.make_response`
387395
:param status: same as :meth:`~flask_excel.make_response`
396+
:param file_name: same as :meth:`~flask_excel.make_response`
388397

389-
.. method:: make_response_from_query_sets(query_sets, column_names, file_type status=200)
398+
.. method:: make_response_from_query_sets(query_sets, column_names, file_type status=200, file_name=None)
390399

391400
Produce a single sheet Excel book of *file_type* from your custom database queries
392401

393402
:param query_sets: a query set
394403
:param column_names: a nominated column names. It could not be None, otherwise no data is returned.
395404
:param file_type: same as :meth:`~flask_excel.make_response`
396405
:param status: same as :meth:`~flask_excel.make_response`
406+
:param file_name: same as :meth:`~flask_excel.make_response`
397407

398-
.. method:: make_response_from_tables(session, tables, file_type status=200)
408+
.. method:: make_response_from_tables(session, tables, file_type status=200, file_name=None)
399409

400410
Produce a multiple sheet Excel book of *file_type*. It becomes the same
401411
as :meth:`~flask_excel.make_response_from_a_table` if you pass *tables*
@@ -405,8 +415,9 @@ Response methods
405415
:param tables: SQLAlchemy tables
406416
:param file_type: same as :meth:`~flask_excel.make_response`
407417
:param status: same as :meth:`~flask_excel.make_response`
418+
:param file_name: same as :meth:`~flask_excel.make_response`
419+
408420

409-
410421
Indices and tables
411422
--------------------
412423

document.bat

100644100755
File mode changed.

examples/tiny_example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def upload_file():
2525
def download_file():
2626
return excel.make_response_from_array([[1,2], [3, 4]], "csv")
2727

28+
@app.route("/export", methods=['GET'])
29+
def export_records():
30+
return excel.make_response_from_array([[1,2], [3, 4]], "csv", file_name="export_data")
31+
2832
# insert database related code here
2933

3034
if __name__ == "__main__":

flask_excel/__init__.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,37 @@ def get_file_tuple(self, field_name):
1818
filename = filehandle.filename
1919
extension = filename.split(".")[1]
2020
return extension, filehandle
21-
2221

2322
Flask.request_class = ExcelRequest
2423
webio.ExcelResponse = Response
2524

26-
from pyexcel_webio import (
27-
make_response,
28-
make_response_from_array,
29-
make_response_from_dict,
30-
make_response_from_records,
31-
make_response_from_book_dict,
32-
make_response_from_a_table,
33-
make_response_from_query_sets,
34-
make_response_from_tables
35-
)
36-
37-
__VERSION__ = '0.0.3'
25+
def add_file_name(response, file_name, file_type):
26+
if file_name:
27+
response.headers["Content-Disposition"] = "attachment; filename=%s.%s" % (file_name, file_type)
28+
return response
29+
30+
def make_response(pyexcel_instance, file_type, status=200, file_name=None, **keywords):
31+
return add_file_name(webio.make_response(pyexcel_instance, file_type, status=status, **keywords), file_name, file_type)
32+
33+
def make_response_from_array(array, file_type, status=200, file_name=None, **keywords):
34+
return add_file_name(webio.make_response_from_array(array, file_type, status=status, **keywords), file_name, file_type)
35+
36+
def make_response_from_dict(adict, file_type, status=200, file_name=None, **keywords):
37+
return add_file_name(webio.make_response_from_dict(adict, file_type, status=status, **keywords), file_name, file_type)
38+
39+
def make_response_from_records(records, file_type, status=200, file_name=None, **keywords):
40+
return add_file_name(webio.make_response_from_records(records, file_type, status=status, **keywords), file_name, file_type)
41+
42+
def make_response_from_book_dict(adict, file_type, status=200, file_name=None, **keywords):
43+
return add_file_name(webio.make_response_from_book_dict(adict, file_type, status=status, **keywords), file_name, file_type)
44+
45+
def make_response_from_a_table(session, table, file_type, status=200, file_name=None, **keywords):
46+
return add_file_name(webio.make_response_from_a_table(session, table, file_type, status=status, **keywords), file_name, file_type)
47+
48+
def make_response_from_query_sets(query_sets, column_names, file_type, status=200, file_name=None, **keywords):
49+
return add_file_name(webio.make_response_from_query_sets(query_sets, column_names, file_type, status=status, **keywords), file_name, file_type)
50+
51+
def make_response_from_tables(session, tables, file_type, status=200, file_name=None, **keywords):
52+
return add_file_name(webio.make_response_from_tables(session, tables, file_type, status=status, **keywords), file_name, file_type)
53+
54+
__VERSION__ = '0.0.4'

tests/test_upload_n_download_excel.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ def test_download(self):
4444
array = sheet.to_array()
4545
assert array == self.data
4646

47+
def test_override_file_name(self):
48+
for file_type in FILE_TYPE_MIME_TABLE.keys():
49+
file_name = 'override_file_name'
50+
response = self.app.post('/file_name/%s/%s' % (file_type, file_name))
51+
assert response.content_type == FILE_TYPE_MIME_TABLE[file_type]
52+
assert response.headers.get("Content-Disposition", None) == ("attachment; filename=%s.%s" % (file_name, file_type))
53+

tests/testapp.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pyexcel.ext.xls
55
import pyexcel.ext.ods3
66
import pyexcel.ext.xlsx
7+
import pyexcel as pe
78
from flask.ext.sqlalchemy import SQLAlchemy
89
from datetime import datetime
910

@@ -75,6 +76,10 @@ def switch(file_type):
7576
sheet = request.get_sheet(field_name='file')
7677
return excel.make_response(sheet, file_type)
7778

79+
@app.route("/file_name/<file_type>/<file_name>", methods=['POST'])
80+
def swtich_file_name(file_type, file_name):
81+
return excel.make_response(pe.Sheet(["a", "b", "c"]), file_type, file_name=file_name)
82+
7883
@app.route("/exchange/<struct_type>", methods=['POST'])
7984
def upload_array(struct_type):
8085
if struct_type == "array":

0 commit comments

Comments
 (0)