|
12 | 12 | import pyexcel as pe
|
13 | 13 | import pyexcel_webio as webio
|
14 | 14 |
|
| 15 | + |
15 | 16 | class ExcelRequest(webio.ExcelInputInMultiDict, Request):
|
| 17 | + """ |
| 18 | + Mix in pyexcel's webio function signatures to Flask request |
| 19 | + """ |
16 | 20 | def get_file_tuple(self, field_name):
|
17 | 21 | filehandle = self.files[field_name]
|
18 | 22 | filename = filehandle.filename
|
19 | 23 | extension = filename.split(".")[1]
|
20 | 24 | return extension, filehandle
|
21 | 25 |
|
| 26 | + |
| 27 | +# Plug-in the custom request to Flask |
22 | 28 | Flask.request_class = ExcelRequest
|
23 |
| -webio.ExcelResponse = Response |
24 | 29 |
|
25 |
| -def add_file_name(response, file_name, file_type): |
| 30 | + |
| 31 | +def make_response(content, content_type, status, file_name=None): |
| 32 | + """ |
| 33 | + Custom response function that is called by pyexcel-webio |
| 34 | + """ |
| 35 | + response = Response(content, content_type=content_type, status=status) |
26 | 36 | if file_name:
|
27 |
| - response.headers["Content-Disposition"] = "attachment; filename=%s.%s" % (file_name, file_type) |
| 37 | + response.headers["Content-Disposition"] = "attachment; filename=%s" % (file_name) |
28 | 38 | return response
|
29 | 39 |
|
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 | 40 |
|
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) |
| 41 | +webio.ExcelResponse = make_response |
50 | 42 |
|
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 | 43 |
|
54 |
| -__VERSION__ = '0.0.4' |
| 44 | +from pyexcel_webio import ( |
| 45 | + make_response, |
| 46 | + make_response_from_array, |
| 47 | + make_response_from_dict, |
| 48 | + make_response_from_records, |
| 49 | + make_response_from_book_dict, |
| 50 | + make_response_from_a_table, |
| 51 | + make_response_from_query_sets, |
| 52 | + make_response_from_tables |
| 53 | +) |
0 commit comments