Commit 46b0de01 by Chris Dodge Committed by Jonathan Piacenti

MCKIN-2781 update API calls to utilize the new date filtering feature on the cs_comment_client

parent 259242f0
......@@ -43,7 +43,18 @@ class SecureClient(Client):
kwargs.update({'SERVER_PORT': 443, 'wsgi.url_scheme': 'https'})
super(SecureClient, self).__init__(*args, **kwargs)
def _fake_get_get_course_social_stats(course_id):
def _fake_get_course_social_stats(course_id, end_date=None):
if end_date:
raise Exception("Expected None for end_date parameter")
return {
'1': {'foo':'bar'},
'2': {'one': 'two'}
}
def _fake_get_course_social_stats_date_expected(course_id, end_date=None):
if not end_date:
raise Exception("Expected non-None end_date parameter")
return {
'1': {'foo':'bar'},
'2': {'one': 'two'}
......@@ -57,7 +68,6 @@ def _fake_get_course_thread_stats(course_id):
}
@mock.patch("api_manager.courses.views.get_course_social_stats", _fake_get_get_course_social_stats)
@mock.patch("api_manager.courses.views.get_course_thread_stats", _fake_get_course_thread_stats)
@override_settings(EDX_API_KEY=TEST_API_KEY)
@mock.patch.dict("django.conf.settings.FEATURES", {'ENFORCE_PASSWORD_POLICY': False,
......@@ -1568,6 +1578,7 @@ class CoursesApiTests(ModuleStoreTestCase):
response = self.do_get(completion_uri)
self.assertEqual(response.status_code, 404)
@mock.patch("api_manager.courses.views.get_course_social_stats", _fake_get_course_social_stats_date_expected)
def test_courses_metrics_social_get(self):
test_uri = '{}/{}/metrics/social/'.format(self.base_courses_uri, self.test_course_id)
response = self.do_get(test_uri)
......@@ -1588,6 +1599,19 @@ class CoursesApiTests(ModuleStoreTestCase):
self.assertFalse(users.get('1'))
self.assertTrue(users.get('2'))
@mock.patch("api_manager.courses.views.get_course_social_stats", _fake_get_course_social_stats)
def test_courses_metrics_social_get_no_date(self):
course = CourseFactory.create(
start=datetime(2014, 6, 16, 14, 30)
)
test_uri = '{}/{}/metrics/social/'.format(self.base_courses_uri, unicode(self.course.id))
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 200)
self.assertTrue(len(response.data.keys()), 2)
users = response.data['users']
self.assertTrue(users.get('1'))
self.assertTrue(users.get('2'))
def test_courses_metrics_grades_leaders_list_get(self):
# make the last user an observer to asset that its content is being filtered out from
# the aggregates
......
......@@ -1790,10 +1790,19 @@ class CoursesMetricsSocial(SecureListAPIView):
slash_course_id = get_course_key(course_id, slashseparated=True)
organization = request.QUERY_PARAMS.get('organization', None)
# the forum service expects the legacy slash separated string format
data = get_course_social_stats(slash_course_id)
# load the course so that we can see when the course end date is
course_descriptor, course_key, course_content = get_course(self.request, self.request.user, course_id) # pylint: disable=W0612
if not course_descriptor:
raise Http404
# get the course social stats, passing along a course end date to remove any activity after the course
# closure from the stats
data = get_course_social_stats(slash_course_id, end_date=course_descriptor.end)
course_key = get_course_key(course_id)
# remove any excluded users from the aggregate
# remove any excluded users from the aggregate
exclude_users = get_aggregate_exclusion_user_ids(course_key)
for user_id in exclude_users:
......
......@@ -40,6 +40,22 @@ from notification_prefs import NOTIFICATION_PREF_KEY
TEST_API_KEY = str(uuid.uuid4())
def _fake_get_user_social_stats(user_id, course_id, end_date=None):
if not end_date:
raise Exception('Expected None end_date parameter')
return {
'1': {'foo':'bar'}
}
def _fake_get_user_social_stats_with_end(user_id, course_id, end_date=None):
if not end_date:
raise Exception('Expected non-None end_date parameter')
return {
'1': {'foo':'bar'}
}
class SecureClient(Client):
""" Django test client using a "secure" connection. """
......@@ -1590,6 +1606,20 @@ class UsersApiTests(ModuleStoreTestCase):
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404)
@mock.patch("api_manager.users.views.get_user_social_stats", _fake_get_user_social_stats)
def test_users_social_metrics(self):
test_uri = '{}/{}/courses/{}/metrics/social/'.format(self.users_base_uri, self.user.id, self.course.id)
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 200)
@mock.patch("api_manager.users.views.get_user_social_stats", _fake_get_user_social_stats_with_end)
def test_users_social_metrics_end_date(self):
course = CourseFactory.create(org='TUCGLG', run='TUCGLG1', end=datetime(2012, 1, 1))
test_uri = '{}/{}/courses/{}/metrics/social/'.format(self.users_base_uri, self.user.id, course.id)
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 200)
def test_users_roles_list_get(self):
allow_access(self.course, self.user, 'staff')
course2 = CourseFactory.create(
......
......@@ -31,6 +31,7 @@ from lang_pref import LANGUAGE_KEY
from notification_prefs.views import enable_notifications
from lms.lib.comment_client.user import User as CommentUser
from lms.lib.comment_client.utils import CommentClientRequestError
from lms.lib.comment_client.user import get_user_social_stats
from notification_prefs.views import enable_notifications
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import UsageKey, CourseKey
......@@ -1175,7 +1176,10 @@ class UsersSocialMetrics(SecureListAPIView):
except ObjectDoesNotExist:
return Response({}, status.HTTP_404_NOT_FOUND)
comment_user = CommentUser.from_django_user(user)
# load the course so that we can see when the course end date is
course_descriptor, course_key, course_content = get_course(self.request, self.request.user, course_id) # pylint: disable=W0612
if not course_descriptor:
raise Http404
# be robust to the try of course_id we get from caller
try:
......@@ -1186,10 +1190,10 @@ class UsersSocialMetrics(SecureListAPIView):
# assume course_id passed in is legacy format
slash_course_id = course_id
comment_user.course_id = slash_course_id
try:
data = (comment_user.social_stats())[user_id]
# get the course social stats, passing along a course end date to remove any activity after the course
# closure from the stats
data = (get_user_social_stats(user_id, slash_course_id, end_date=course_descriptor.end))[user_id]
http_status = status.HTTP_200_OK
except (CommentClientRequestError, ConnectionError), error:
data = {
......
......@@ -115,18 +115,8 @@ class User(models.Model):
)
return response.get('collection', []), response.get('page', 1), response.get('num_pages', 1)
def social_stats(self):
if not self.course_id:
raise CommentClientRequestError("Must provide course_id when retrieving social stats for the user")
url = _url_for_user_social_stats(self.id)
params = {'course_id': self.course_id}
response = perform_request(
'get',
url,
params
)
return response
def social_stats(self, end_date=None):
return get_user_social_stats(self.id, self.course_id, end_date=end_date)
def _retrieve(self, *args, **kwargs):
url = self.url(action='get', params=self.attributes)
......@@ -160,13 +150,32 @@ class User(models.Model):
raise
self._update_from_response(response)
def get_user_social_stats(user_id, course_id, end_date=None):
if not course_id:
raise CommentClientRequestError("Must provide course_id when retrieving social stats for the user")
url = _url_for_user_social_stats(user_id)
params = {'course_id': course_id}
if end_date:
params.update({'end_date': end_date.isoformat()})
def get_course_social_stats(course_id):
response = perform_request(
'get',
url,
params
)
return response
def get_course_social_stats(course_id, end_date=None):
"""
Helper method to get the social stats from the comment service
"""
url = _url_for_course_social_stats()
url = _url_for_course_social_stats(end_date=end_date)
params = {'course_id': course_id}
if end_date:
params.update({'end_date': end_date.isoformat()})
response = perform_request(
'get',
url,
......@@ -196,9 +205,9 @@ def _url_for_user_subscribed_threads(user_id):
def _url_for_user_stats(user_id,course_id):
return "{prefix}/users/{user_id}/stats?course_id={course_id}".format(prefix=settings.PREFIX, user_id=user_id,course_id=course_id)
def _url_for_user_social_stats(user_id):
def _url_for_user_social_stats(user_id, end_date=None):
return "{prefix}/users/{user_id}/social_stats".format(prefix=settings.PREFIX, user_id=user_id)
def _url_for_course_social_stats():
def _url_for_course_social_stats(end_date=None):
return "{prefix}/users/*/social_stats".format(prefix=settings.PREFIX)
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