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

Add Date Format #1374

Merged
merged 1 commit into from
Apr 20, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -843,6 +843,21 @@ def test_filter_by_submission_time_and_submitted_by_with_data_arg(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 2)

def test_filter_by_submission_time_date_formats(self):
self._make_submissions()
view = DataViewSet.as_view({'get': 'list'})
formid = self.xform.pk

data = {'query': '{"_submission_time":{"$gt":"2018-04-19"}}'}
request = self.factory.get('/', data=data, **self.extra)
response = view(request, pk=formid)
self.assertEqual(response.status_code, 200)

data = {'query': '{"_submission_time":{"$gt":"2018-04-19T14:46:32"}}'}
request = self.factory.get('/', data=data, **self.extra)
response = view(request, pk=formid)
self.assertEqual(response.status_code, 200)

def test_data_with_query_parameter(self):
self._make_submissions()
view = DataViewSet.as_view({'get': 'list'})
Expand Down
11 changes: 8 additions & 3 deletions onadata/apps/viewer/parsed_instance_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import six
import datetime

from onadata.libs.utils.common_tags import MONGO_STRFTIME
from onadata.libs.utils.common_tags import MONGO_STRFTIME, DATE_FORMAT

KNOWN_DATES = ['_submission_time']
NONE_JSON_FIELDS = {
Expand Down Expand Up @@ -49,8 +49,13 @@ def _parse_where(query, known_integers, or_where, or_params):
)
_v = value
if field_key in KNOWN_DATES:
_v = datetime.datetime.strptime(
_v[:19], MONGO_STRFTIME)
raw_date = value
for date_format in (MONGO_STRFTIME, DATE_FORMAT):
try:
_v = datetime.datetime.strptime(raw_date[:19],
date_format)
except ValueError:
pass
if field_key in NONE_JSON_FIELDS:
where_params.extend([unicode(_v)])
else:
Expand Down