Skip to content

Commit

Permalink
Merge pull request #2465 from onaio/2464-fix-attribute-error
Browse files Browse the repository at this point in the history
Fix attribute error when uploading xls datasets
  • Loading branch information
FrankApiyo authored Aug 9, 2023
2 parents 7c2bd92 + f1825db commit ee769d3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
32 changes: 32 additions & 0 deletions onadata/apps/api/tests/viewsets/test_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4987,6 +4987,38 @@ def test_form_publishing_floip(self):
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(xforms + 1, XForm.objects.count())

def test_xlsx_import(self):
"""Ensure XLSX imports work as expected and dates are formatted correctly"""
with HTTMock(enketo_mock):
xls_path = os.path.join(
settings.PROJECT_ROOT,
"apps",
"main",
"tests",
"fixtures",
"double_image_form.xlsx",
)
self._publish_xls_form_to_project(xlsform_path=xls_path)
view = XFormViewSet.as_view({"post": "data_import"})
xls_import = fixtures_path("double_image_field_form_data.xlsx")
post_data = {"xls_file": xls_import}
request = self.factory.post("/", data=post_data, **self.extra)
response = view(request, pk=self.xform.id)

# check that date columns are formatted correctly
self.assertEqual(
self.xform.instances.values("json___submission_time")[::1],
[
{"json___submission_time": "2023-02-03T10:27:41"},
{"json___submission_time": "2023-02-03T10:27:42"},
{"json___submission_time": "2023-03-13T08:42:57"},
],
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get("Cache-Control"), None)
self.assertEqual(response.data.get("additions"), 3)
self.assertEqual(response.data.get("updates"), 0)

def test_xls_import(self):
with HTTMock(enketo_mock):
xls_path = os.path.join(
Expand Down
3 changes: 2 additions & 1 deletion onadata/apps/api/viewsets/xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,9 @@ def data_import(self, request, *args, **kwargs):
)
else:
csv_file.seek(0)
file_name = getattr(csv_file, "name", xls_file and xls_file.name)
upload_to = os.path.join(
request.user.username, "csv_imports", csv_file.name
request.user.username, "csv_imports", file_name
)
file_name = default_storage.save(upload_to, csv_file)
task = submit_csv_async.delay(
Expand Down
Binary file not shown.
Binary file not shown.
12 changes: 6 additions & 6 deletions onadata/libs/utils/csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def dict_merge(part_a, part_b):
if not isinstance(part_b, dict):
return part_b
result = deepcopy(part_a)
for (k, v) in iteritems(part_b):
for k, v in iteritems(part_b):
if k in result and isinstance(result[k], dict):
result[k] = dict_merge(result[k], v)
else:
Expand Down Expand Up @@ -628,16 +628,16 @@ def submission_xls_to_csv(xls_file): # noqa
# convert excel dates(floats) to datetime
for date_column_index in date_columns:
try:
row_values[date_column_index] = (
row_values[date_column_index].strftime("%Y-%m-%d").isoformat()
)
row_values[date_column_index - 1] = row_values[
date_column_index - 1
].isoformat()
except (ValueError, TypeError):
pass

# convert excel boolean to true/false
for boolean_column_index in boolean_columns:
row_values[boolean_column_index] = bool(
row_values[boolean_column_index] == EXCEL_TRUE
row_values[boolean_column_index - 1] = bool(
row_values[boolean_column_index - 1] == EXCEL_TRUE
)

csv_writer.writerow(row_values)
Expand Down

0 comments on commit ee769d3

Please sign in to comment.