Skip to content

Commit a754a98

Browse files
committed
further abstraction for pyramid and Flask decendants
1 parent b898e66 commit a754a98

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

pyexcel_webio/__init__.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
else:
1616
from io import BytesIO
1717

18-
18+
1919
FILE_TYPE_MIME_TABLE = {
2020
"csv": "text/csv",
2121
"tsv": "text/tab-separated-values",
@@ -35,7 +35,7 @@ class ExcelInput(object):
3535
"""
3636
def load_single_sheet(self, sheet_name=None, **keywords):
3737
"""Abstract method
38-
38+
3939
:param form_field_name: the file field name in the html form for file upload
4040
:param sheet_name: For an excel book, there could be multiple sheets. If it is left
4141
unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
@@ -158,6 +158,33 @@ def save_book_to_database(self, session=None, tables=None, initializers=None, ma
158158
book.save_to_database(session, tables, initializers=initializers, mapdicts=mapdicts)
159159

160160

161+
162+
class ExcelInputInMultiDict(ExcelInput):
163+
""" A generic interface for an upload excel file appearing in a dictionary
164+
"""
165+
def get_file_tuple(self, field_name):
166+
raise NotImplementedError("Please implement this function")
167+
168+
def load_single_sheet(self, field_name=None, sheet_name=None, **keywords):
169+
file_type, file_handle = self.get_file_tuple(field_name)
170+
if file_type is not None and file_handle is not None:
171+
return pe.get_sheet(file_type=file_type,
172+
file_content=file_handle.read(),
173+
sheet_name=sheet_name,
174+
**keywords)
175+
else:
176+
return None
177+
178+
def load_book(self, field_name=None, **keywords):
179+
file_type, file_handle = self.get_file_tuple(field_name)
180+
if file_type is not None and file_handle is not None:
181+
return pe.get_book(file_type=file_type,
182+
file_content=file_handle.read(),
183+
**keywords)
184+
else:
185+
return None
186+
187+
161188
def dummy_func(content, content_type=None, status=200):
162189
return None
163190

@@ -216,4 +243,4 @@ def make_response_from_a_table(session, table, file_type, status=200, **keywords
216243

217244
def make_response_from_tables(session, tables, file_type, status=200, **keywords):
218245
book = pe.get_book(session=session, tables=tables, **keywords)
219-
return make_response(book, file_type, status, **keywords)
246+
return make_response(book, file_type, status, **keywords)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
setup(
1616
name='pyexcel-webio',
1717
author="C. W.",
18-
version='0.0.2',
18+
version='0.0.3',
1919
author_email="wangc_2011@hotmail.com",
2020
url="https://github.com/chfw/pyexcel-webio",
2121
description='A generic request and response interface for pyexcel web extensions.',

0 commit comments

Comments
 (0)