Commit e9d2731d by Zia Fazal Committed by Jonathan Piacenti

course leaders by points_scored

updated urls

updated docs
parent 4fac6450
......@@ -18,3 +18,12 @@ class CourseModuleCompletionSerializer(serializers.ModelSerializer):
class GradeSerializer(serializers.Serializer):
""" Serializer for model interactions """
grade = serializers.Field()
class CourseLeadersSerializer(serializers.Serializer):
""" Serializer for course leaderboard """
id = serializers.IntegerField(source='student__id')
username = serializers.CharField(source='student__username')
title = serializers.CharField(source='student__profile__title')
avatar_url = serializers.CharField(source='student__profile__avatar_url')
points_scored = serializers.IntegerField()
......@@ -129,10 +129,14 @@ class CoursesApiTests(TestCase):
display_name=u"test unit",
)
self.users = [UserFactory.create(username="testuser" + str(__)) for __ in xrange(USER_COUNT)]
self.users = [UserFactory.create(username="testuser" + str(__), profile='test') for __ in xrange(USER_COUNT)]
for user in self.users:
CourseEnrollmentFactory.create(user=user, course_id=self.course.id)
user_profile = user.profile
user_profile.avatar_url = 'http://example.com/{}.png'.format(user.id)
user_profile.title = 'Software Engineer {}'.format(user.id)
user_profile.save()
for i in xrange(USER_COUNT - 1):
category = 'mentoring'
......@@ -1294,6 +1298,24 @@ class CoursesApiTests(TestCase):
self.assertEqual(response.data['count'], 1)
self.assertEqual(len(response.data['results']), 1)
def test_courses_leaders_list_get(self):
test_uri = '{}/{}/metrics/proficiency/leaders/?{}'.format(self.base_courses_uri, self.test_course_id, 'count=3')
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 3)
# Filter by content_id
content_filter_uri = '{}/{}/metrics/proficiency/leaders/?content_id={}'\
.format(self.base_courses_uri, self.test_course_id, Location(self.item.location).url())
response = self.do_get(content_filter_uri)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 4)
# test with bogus course
test_uri = '{}/{}/metrics/proficiency/leaders/'.format(self.base_courses_uri, self.test_bogus_course_id)
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404)
def test_courses_grades_list_get(self):
# Retrieve the list of grades for this course
# All the course/item/user scaffolding was handled in Setup
......
......@@ -30,6 +30,7 @@ urlpatterns = patterns(
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)/completions/*$', courses_views.CourseModuleCompletionList.as_view(), name='completion-list'),
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)/projects/*$', courses_views.CoursesProjectList.as_view(), name='courseproject-list'),
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)/metrics/*$', courses_views.CourseMetrics.as_view(), name='course-metrics'),
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)/metrics/proficiency/leaders/*$', courses_views.CoursesLeadersList.as_view(), name='course-metrics-proficiency-leaders'),
)
urlpatterns = format_suffix_patterns(urlpatterns)
......@@ -34,7 +34,7 @@ from projects.models import Project
from projects.serializers import ProjectSerializer
from .serializers import CourseModuleCompletionSerializer
from .serializers import GradeSerializer
from .serializers import GradeSerializer, CourseLeadersSerializer
log = logging.getLogger(__name__)
......@@ -1424,3 +1424,46 @@ class CourseMetrics(SecureAPIView):
'users_enrolled': users_enrolled
}
return Response(data, status=status.HTTP_200_OK)
class CoursesLeadersList(SecureListAPIView):
"""
### The CoursesLeadersList view allows clients to retrieve ordered list of users who are leading
in terms of points_scored for the specified Course
- URI: ```/api/courses/{course_id}/metrics/proficiency/leaders/```
- GET: Returns a JSON representation (array) of the users with points scored
Filters can also be applied
```/api/courses/{course_id}/metrics/proficiency/leaders/?content_id={content_id}```
To get top 3 users use count parameter
```/api/courses/{course_id}/metrics/proficiency/leaders/?count=3```
### Use Cases/Notes:
* Example: Display leaderboard of a given course
* Example: Display top 3 users of a given course
"""
serializer_class = CourseLeadersSerializer
def get_queryset(self):
"""
GET /api/courses/{course_id}/leaders/
"""
course_id = self.kwargs['course_id']
content_id = self.request.QUERY_PARAMS.get('content_id', None)
count = self.request.QUERY_PARAMS.get('count', getattr(settings, 'API_LOOKUP_UPPER_BOUND', 100))
try:
get_course(course_id)
except ValueError:
raise Http404
queryset = StudentModule.objects.filter(
course_id__exact=course_id,
grade__isnull=False,
max_grade__isnull=False,
max_grade__gt=0
)
if content_id:
queryset = queryset.filter(module_state_key=content_id)
queryset = queryset.values('student__id', 'student__username', 'student__profile__title',
'student__profile__avatar_url').annotate(points_scored=Sum('grade')).order_by('-points_scored')[:count]
return queryset
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