Commit c7326041 by Clinton Blackburn

Added support for course activity resource

parent c2264bdd
import urllib
import warnings
import analyticsclient.activity_type as AT
import analyticsclient.data_format as DF
from analyticsclient.exceptions import InvalidRequestError
class Course(object):
......@@ -33,7 +35,7 @@ class Course(object):
Arguments:
demographic (str): Demographic by which enrollment data should be grouped.
start_date (str): Minimum date for returned enrollment data
end_date (str): Maxmimum date for returned enrollment data
end_date (str): Maximum date for returned enrollment data
data_format (str): Format in which data should be returned
"""
path = 'courses/{0}/enrollment/'.format(self.course_id)
......@@ -53,6 +55,33 @@ class Course(object):
return self.client.get(path, data_format=data_format)
def activity(self, activity_type=AT.ANY, start_date=None, end_date=None, data_format=DF.JSON):
"""
Get the course student activity.
Arguments:
activity_type (str): The type of recent activity to return. Defaults to ANY.
data_format (str): Format in which data should be returned
"""
if not activity_type:
raise InvalidRequestError('activity_type cannot be None.')
params = {
'activity_type': activity_type
}
if start_date:
params['start_date'] = start_date
if end_date:
params['end_date'] = end_date
path = 'courses/{0}/activity/'.format(self.course_id)
querystring = urllib.urlencode(params)
path += '?{0}'.format(querystring)
return self.client.get(path, data_format=data_format)
def recent_activity(self, activity_type=AT.ANY, data_format=DF.JSON):
"""
Get the recent course activity.
......@@ -61,5 +90,7 @@ class Course(object):
activity_type (str): The type of recent activity to return. Defaults to ANY.
data_format (str): Format in which data should be returned
"""
warnings.warn('recent_activity has been deprecated! Use activity instead.', DeprecationWarning)
path = 'courses/{0}/recent_activity/?activity_type={1}'.format(self.course_id, activity_type)
return self.client.get(path, data_format=data_format)
......@@ -44,6 +44,29 @@ class CoursesTests(ClientTestCase):
httpretty.register_uri(httpretty.GET, '{0}?start_date={1}&end_date={1}'.format(uri, date), body='{}')
course.enrollment(demographic, start_date=date, end_date=date)
def assertCorrectActivityUrl(self, course, activity_type=None):
""" Verifies that the activity URL is correct. """
uri = self.get_api_url('courses/{0}/activity/'.format(course.course_id))
if activity_type:
uri += '?activity_type=%s' % activity_type
httpretty.register_uri(httpretty.GET, uri, body='{}')
course.activity(activity_type)
date = '2014-01-01'
httpretty.reset()
httpretty.register_uri(httpretty.GET, '{0}&start_date={1}'.format(uri, date), body='{}')
course.activity(activity_type, start_date=date)
httpretty.reset()
httpretty.register_uri(httpretty.GET, '{0}&end_date={1}'.format(uri, date), body='{}')
course.activity(activity_type, end_date=date)
httpretty.reset()
httpretty.register_uri(httpretty.GET, '{0}&start_date={1}&end_date={1}'.format(uri, date), body='{}')
course.activity(activity_type, start_date=date, end_date=date)
@httpretty.activate
def assertRecentActivityResponseData(self, course, activity_type):
body = {
......@@ -93,6 +116,13 @@ class CoursesTests(ClientTestCase):
self.assertCorrectEnrollmentUrl(self.course, demo.GENDER)
self.assertCorrectEnrollmentUrl(self.course, demo.LOCATION)
def test_activity(self):
self.assertRaises(InvalidRequestError, self.assertCorrectActivityUrl, self.course, None)
self.assertCorrectActivityUrl(self.course, at.ANY)
self.assertCorrectActivityUrl(self.course, at.ATTEMPTED_PROBLEM)
self.assertCorrectActivityUrl(self.course, at.PLAYED_VIDEO)
self.assertCorrectActivityUrl(self.course, at.POSTED_FORUM)
def test_enrollment_data_format(self):
uri = self.get_api_url('courses/{0}/enrollment/'.format(self.course.course_id))
......
......@@ -2,7 +2,7 @@ from distutils.core import setup
setup(
name='edx-analytics-data-api-client',
version='0.1.0',
version='0.2.0',
packages=['analyticsclient'],
url='https://github.com/edx/edx-analytics-data-api-client',
description='Client used to access edX analytics data warehouse',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment