Skip to content

Commit a4a29a1

Browse files
committed
Merge pull request #152 from open-data/fix/misc-exception-handling
Improved Exception Handling on File Upload
1 parent cb9f93a commit a4a29a1

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

ckanext/recombinant/read_excel.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from werkzeug.datastructures import FileStorage as FlaskFileStorage
99
from cgi import FieldStorage
1010

11+
from ckan.plugins.toolkit import _
12+
1113
from ckanext.recombinant.datatypes import canonicalize
1214
from ckanext.recombinant.errors import BadExcelData
1315

@@ -17,7 +19,8 @@
1719

1820

1921
def read_excel(f: Union[str, FlaskFileStorage, FieldStorage],
20-
expected_sheet_names: List[str],
22+
expected_sheet_names: List[str] = [],
23+
bad_sheet_names: List[str] = [],
2124
file_contents: Optional[str] = None) -> Iterator[Any]:
2225
"""
2326
Return a generator that opens the excel file f (name or file object)
@@ -36,6 +39,12 @@ def read_excel(f: Union[str, FlaskFileStorage, FieldStorage],
3639
for sheetname in wb.sheetnames:
3740
if sheetname == 'reference':
3841
return
42+
if sheetname in bad_sheet_names:
43+
raise BadExcelData(_('Invalid file for this data type. ' +
44+
'Sheet must be labeled "{0}", ' +
45+
'but you supplied a sheet labeled "{1}"').format(
46+
'"/"'.join(sorted(expected_sheet_names)),
47+
sheetname))
3948
if sheetname not in expected_sheet_names:
4049
# NOTE: some Excel extensions and Macros create fully hidden
4150
# worksheets that act as a sort of database/index cache

ckanext/recombinant/views.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,16 @@ def _process_upload_file(lc: LocalCKAN,
750750
(resource['name'], resource['id'])
751751
for resource in dataset['resources'])
752752

753+
bad_types: List[str] = h.recombinant_get_types()
754+
bad_types.remove(dataset['type'])
755+
bad_sheet_names = []
756+
for bt in bad_types:
757+
brs = get_geno(bt).get('resources', [])
758+
bad_sheet_names += [br['resource_name'] for br in brs]
759+
753760
# type_ignore_reason: incomplete typing
754-
upload_data = read_excel(upload_file, expected_sheet_names.keys()) # type: ignore
761+
upload_data = read_excel(upload_file, expected_sheet_names.keys(), # type: ignore
762+
bad_sheet_names)
755763
total_records = 0
756764
# type_ignore_reason: incomplete typing
757765
backend: DatastorePostgresqlBackend = DatastoreBackend.\

0 commit comments

Comments
 (0)