Skip to content

Commit

Permalink
Merge pull request openedx-unsupported#127 from edx/thallada/datetime…
Browse files Browse the repository at this point in the history
…-start-end-dates

Allows time component in start and end date parameters
  • Loading branch information
thallada authored Jul 27, 2016
2 parents ec784aa + a54fb1d commit 7fe4fa6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
18 changes: 13 additions & 5 deletions analytics_data_api/v0/tests/views/test_courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,30 @@ def test_get_with_intervals(self):

def assertIntervalFilteringWorks(self, expected_response, start_date, end_date):
# If start date is after date of existing data, return a 404
date = (start_date + datetime.timedelta(days=30)).strftime(settings.DATE_FORMAT)
date = (start_date + datetime.timedelta(days=30)).strftime(settings.DATETIME_FORMAT)
response = self.authenticated_get(
'%scourses/%s%s?start_date=%s' % (self.api_root_path, self.course_id, self.path, date))
self.assertEquals(response.status_code, 404)

# If end date is before date of existing data, return a 404
date = (start_date - datetime.timedelta(days=30)).strftime(settings.DATE_FORMAT)
date = (start_date - datetime.timedelta(days=30)).strftime(settings.DATETIME_FORMAT)
response = self.authenticated_get(
'%scourses/%s%s?end_date=%s' % (self.api_root_path, self.course_id, self.path, date))
self.assertEquals(response.status_code, 404)

# If data falls in date range, data should be returned
start_date = start_date.strftime(settings.DATE_FORMAT)
end_date = end_date.strftime(settings.DATE_FORMAT)
start = start_date.strftime(settings.DATETIME_FORMAT)
end = end_date.strftime(settings.DATETIME_FORMAT)
response = self.authenticated_get('%scourses/%s%s?start_date=%s&end_date=%s' % (
self.api_root_path, self.course_id, self.path, start_date, end_date))
self.api_root_path, self.course_id, self.path, start, end))
self.assertEquals(response.status_code, 200)
self.assertListEqual(response.data, expected_response)

# Passing dates in DATE_FORMAT still works
start = start_date.strftime(settings.DATE_FORMAT)
end = end_date.strftime(settings.DATE_FORMAT)
response = self.authenticated_get('%scourses/%s%s?start_date=%s&end_date=%s' % (
self.api_root_path, self.course_id, self.path, start, end))
self.assertEquals(response.status_code, 200)
self.assertListEqual(response.data, expected_response)

Expand Down
21 changes: 11 additions & 10 deletions analytics_data_api/v0/views/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ def get(self, request, *args, **kwargs):
end_date = request.QUERY_PARAMS.get('end_date')
timezone = utc

if start_date:
start_date = datetime.datetime.strptime(start_date, settings.DATE_FORMAT)
start_date = make_aware(start_date, timezone)

if end_date:
end_date = datetime.datetime.strptime(end_date, settings.DATE_FORMAT)
end_date = make_aware(end_date, timezone)

self.start_date = start_date
self.end_date = end_date
self.start_date = self.parse_date(start_date, timezone)
self.end_date = self.parse_date(end_date, timezone)

return super(BaseCourseView, self).get(request, *args, **kwargs)

def parse_date(self, date, timezone):
if date:
try:
date = datetime.datetime.strptime(date, settings.DATETIME_FORMAT)
except ValueError:
date = datetime.datetime.strptime(date, settings.DATE_FORMAT)
date = make_aware(date, timezone)
return date

def apply_date_filtering(self, queryset):
raise NotImplementedError

Expand Down

0 comments on commit 7fe4fa6

Please sign in to comment.