Skip to content

Commit

Permalink
Merge pull request #903 from kobotoolbox/fix-custom-handler-data-endp…
Browse files Browse the repository at this point in the history
…oint

Fix 500 error when fetching data with a date range
  • Loading branch information
LMNTL authored Oct 23, 2023
2 parents 72145d7 + b40b1c2 commit 1c41cea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 15 additions & 0 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,3 +824,18 @@ def test_cannot_access_data_of_pending_delete_xform(self):
view = DataViewSet.as_view({'get': 'list'})
response = view(request, pk=self.xform.pk)
assert response.status_code == status.HTTP_404_NOT_FOUND

def test_list_data_with_custom_handler(self):
request = self.factory.get('/', **self.extra)
view = DataViewSet.as_view({'get': 'list'})
response = view(request, pk=self.xform.pk, format='xlsx')
assert response.status_code == status.HTTP_200_OK
assert response.headers['Content-Type'] == 'application/vnd.openxmlformats'

def test_list_data_with_custom_handler_and_date_range(self):
qs_params = '?start=21_10_17_00_00_00&end=23_12_01_00_00_00'
request = self.factory.get(f'/{qs_params}', **self.extra)
view = DataViewSet.as_view({'get': 'list'})
response = view(request, pk=self.xform.pk, format='xlsx')
assert response.status_code == status.HTTP_200_OK
assert response.headers['Content-Type'] == 'application/vnd.openxmlformats'
6 changes: 4 additions & 2 deletions onadata/apps/api/viewsets/xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ def _set_start_end_params(request, query):
try:
if request.GET.get('start'):
query[SUBMISSION_TIME]['$gte'] = format_date_for_mongo(
request.GET['start'], datetime)
request.GET['start']
)

if request.GET.get('end'):
query[SUBMISSION_TIME]['$lte'] = format_date_for_mongo(
request.GET['end'], datetime)
request.GET['end']
)
except ValueError:
raise exceptions.ParseError(
t("Dates must be in the format YY_MM_DD_hh_mm_ss")
Expand Down

0 comments on commit 1c41cea

Please sign in to comment.