Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 500 error when fetching data with a date range #903

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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