@@ -209,6 +209,18 @@ def dummy_func(content, content_type=None, status=200, file_name=None):
209
209
ExcelResponse = dummy_func
210
210
211
211
212
+ def _make_response (io_stream , file_type ,
213
+ status = 200 , file_name = None ):
214
+ io_stream .seek (0 )
215
+ if file_name :
216
+ if not file_name .endswith (file_type ):
217
+ file_name = "%s.%s" % (file_name , file_type )
218
+ return ExcelResponse (io_stream .read (),
219
+ content_type = FILE_TYPE_MIME_TABLE [file_type ],
220
+ status = status , file_name = file_name )
221
+
222
+
223
+
212
224
def make_response (pyexcel_instance , file_type ,
213
225
status = 200 , file_name = None , ** keywords ):
214
226
"""
@@ -232,17 +244,11 @@ def make_response(pyexcel_instance, file_type,
232
244
"""
233
245
io = pe .get_io (file_type )
234
246
pyexcel_instance .save_to_memory (file_type , io , ** keywords )
235
- io .seek (0 )
236
- if file_name :
237
- if not file_name .endswith (file_type ):
238
- file_name = "%s.%s" % (file_name , file_type )
239
- return ExcelResponse (io .read (),
240
- content_type = FILE_TYPE_MIME_TABLE [file_type ],
241
- status = status , file_name = file_name )
247
+ return _make_response (io , file_type , status , file_name )
242
248
243
249
244
250
def make_response_from_array (array , file_type ,
245
- status = 200 , ** keywords ):
251
+ status = 200 , file_name = None , ** keywords ):
246
252
"""
247
253
Make a http response from an array
248
254
@@ -251,12 +257,12 @@ def make_response_from_array(array, file_type,
251
257
:param status: same as :meth:`~pyexcel_webio.make_response`
252
258
:returns: http response
253
259
"""
254
- return make_response ( pe .Sheet (array ),
255
- file_type , status , ** keywords )
260
+ io = pe .save_as (array = array , dest_file_type = file_type , ** keywords )
261
+ return _make_response ( io , file_type , status , file_name )
256
262
257
263
258
264
def make_response_from_dict (adict , file_type ,
259
- status = 200 , ** keywords ):
265
+ status = 200 , file_name = None , ** keywords ):
260
266
"""
261
267
Make a http response from a dictionary of lists
262
268
@@ -265,13 +271,12 @@ def make_response_from_dict(adict, file_type,
265
271
:param status: same as :meth:`~pyexcel_webio.make_response`
266
272
:returns: http response
267
273
"""
268
- sheet = pe .get_sheet (adict = adict )
269
- return make_response (sheet ,
270
- file_type , status , ** keywords )
274
+ io = pe .save_as (adict = adict , dest_file_type = file_type , ** keywords )
275
+ return _make_response (io , file_type , status , file_name )
271
276
272
277
273
278
def make_response_from_records (records , file_type ,
274
- status = 200 , ** keywords ):
279
+ status = 200 , file_name = None , ** keywords ):
275
280
"""
276
281
Make a http response from a list of dictionaries
277
282
@@ -280,13 +285,12 @@ def make_response_from_records(records, file_type,
280
285
:param status: same as :meth:`~pyexcel_webio.make_response`
281
286
:returns: http response
282
287
"""
283
- sheet = pe .get_sheet (records = records )
284
- return make_response (sheet ,
285
- file_type , status , ** keywords )
288
+ io = pe .save_as (records = records , dest_file_type = file_type , ** keywords )
289
+ return _make_response (io , file_type , status , file_name )
286
290
287
291
288
292
def make_response_from_book_dict (adict ,
289
- file_type , status = 200 ,
293
+ file_type , status = 200 , file_name = None ,
290
294
** keywords ):
291
295
"""
292
296
Make a http response from a dictionary of two dimensional
@@ -297,12 +301,12 @@ def make_response_from_book_dict(adict,
297
301
:param status: same as :meth:`~pyexcel_webio.make_response`
298
302
:returns: http response
299
303
"""
300
- book = pe .get_book (bookdict = adict )
301
- return make_response ( book , file_type , status , ** keywords )
304
+ io = pe .save_book_as (bookdict = adict , dest_file_type = file_type , ** keywords )
305
+ return _make_response ( io , file_type , status , file_name )
302
306
303
307
304
308
def make_response_from_query_sets (query_sets , column_names ,
305
- file_type , status = 200 ,
309
+ file_type , status = 200 , file_name = None ,
306
310
** keywords ):
307
311
"""
308
312
Make a http response from a dictionary of two dimensional
@@ -315,8 +319,9 @@ def make_response_from_query_sets(query_sets, column_names,
315
319
:param status: same as :meth:`~pyexcel_webio.make_response`
316
320
:returns: a http response
317
321
"""
318
- sheet = pe .get_sheet (query_sets = query_sets , column_names = column_names )
319
- return make_response (sheet , file_type , status , ** keywords )
322
+ io = pe .save_as (query_sets = query_sets , column_names = column_names ,
323
+ dest_file_type = file_type , ** keywords )
324
+ return _make_response (io , file_type , status , file_name )
320
325
321
326
322
327
def make_response_from_a_table (session , table ,
@@ -331,8 +336,9 @@ def make_response_from_a_table(session, table,
331
336
:param status: same as :meth:`~pyexcel_webio.make_response`
332
337
:returns: a http response
333
338
"""
334
- sheet = pe .get_sheet (session = session , table = table , ** keywords )
335
- return make_response (sheet , file_type , status , file_name = file_name , ** keywords )
339
+ io = pe .save_as (session = session , table = table ,
340
+ dest_file_type = file_type , ** keywords )
341
+ return _make_response (io , file_type , status , file_name )
336
342
337
343
338
344
def make_response_from_tables (session , tables ,
@@ -347,5 +353,5 @@ def make_response_from_tables(session, tables,
347
353
:param status: same as :meth:`~pyexcel_webio.make_response`
348
354
:returns: a http response
349
355
"""
350
- book = pe .get_book (session = session , tables = tables , ** keywords )
351
- return make_response ( book , file_type , status , file_name = file_name , ** keywords )
356
+ io = pe .save_book_as (session = session , tables = tables , dest_file_type = file_type , ** keywords )
357
+ return _make_response ( io , file_type , status , file_name )
0 commit comments