Commit 11225820 by chrisndodge

Merge pull request #17 from edx-solutions/api

create optimized course tree query API endpoint
parents 2366b188 a2c044b8
......@@ -17,5 +17,6 @@ urlpatterns = patterns(
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)/static_tabs/(?P<tab_id>[a-zA-Z0-9/_:]+)$', 'static_tab_detail'),
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)/static_tabs$', 'static_tabs_list'),
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)/users$', 'course_users_list'),
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)/tree/(?P<depth>[0-9]+)$', 'course_tree'),
url(r'^(?P<course_id>[^/]+/[^/]+/[^/]+)$', 'courses_detail'),
)
......@@ -16,7 +16,7 @@ from api_manager.models import CourseGroupRelationship
from xmodule.modulestore.django import modulestore
from xmodule.modulestore import Location, InvalidLocationError
from courseware.courses import get_course_about_section, get_course_info_section
from courseware.courses import get_course_about_section, get_course_info_section, get_course_by_id
from courseware.views import get_static_tab_contents
from student.models import CourseEnrollment, CourseEnrollmentAllowed
......@@ -226,6 +226,40 @@ def courses_detail(request, course_id):
return Response(response_data, status=status_code)
@api_view(['GET'])
@permission_classes((ApiKeyHeaderPermission,))
def course_tree(request, course_id, depth):
"""
GET retrieves an existing course from the system and returns summary information about the submodules
to the specified depth
"""
response_data = {}
depth_int = int(depth)
# note, passing in depth=N optimizes the number of round trips to the database
course_descriptor = get_course_by_id(course_id, depth=depth_int)
if not course_descriptor:
return Response({}, status.HTTP_404_NOT_FOUND)
def _serialize_node_with_children(descriptor, depth):
data = _serialize_module(
request,
course_descriptor.id,
descriptor
)
if depth > 0:
data['modules'] = []
for child in descriptor.get_children():
data['modules'].append(_serialize_node_with_children(child, depth-1))
return data
response_data = _serialize_node_with_children(course_descriptor, depth_int)
return Response(response_data)
@api_view(['POST'])
@permission_classes((ApiKeyHeaderPermission,))
def courses_groups_list(request, course_id):
......
......@@ -170,6 +170,36 @@ class CoursesApiTests(TestCase):
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404)
def test_course_tree_get(self):
# query the course tree to quickly get naviation information
test_uri = self.base_courses_uri + '/' + self.test_course_id + '/tree/2'
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 200)
self.assertGreater(len(response.data), 0)
self.assertEqual(response.data['category'], 'course')
self.assertEqual(response.data['name'], self.course.display_name)
self.assertEqual(len(response.data['modules']), 1)
chapter = response.data['modules'][0]
self.assertEqual(chapter['category'], 'chapter')
self.assertEqual(chapter['name'], 'Overview')
self.assertEqual(len(chapter['modules']), 1)
sequence = chapter['modules'][0]
self.assertEqual(sequence['category'], 'videosequence')
self.assertEqual(sequence['name'], 'Video_Sequence')
self.assertNotIn('modules', sequence)
def test_course_tree_get_root(self):
# query the course tree to quickly get naviation information
test_uri = self.base_courses_uri + '/' + self.test_course_id + '/tree/0'
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 200)
self.assertGreater(len(response.data), 0)
self.assertEqual(response.data['category'], 'course')
self.assertEqual(response.data['name'], self.course.display_name)
self.assertNotIn('modules', response.data)
def test_chapter_list_get(self):
test_uri = self.base_chapters_uri
response = self.do_get(test_uri)
......
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