Commit 403ebbd5 by Chris Dodge Committed by Jonathan Piacenti

cdodge/speed-up-get-current-position: enough said

more

cleanup

fix method name
parent 0225e33a
...@@ -31,10 +31,10 @@ def get_course(request, user, course_id, depth=0): ...@@ -31,10 +31,10 @@ def get_course(request, user, course_id, depth=0):
pass pass
if course_descriptor: if course_descriptor:
field_data_cache = FieldDataCache([course_descriptor], course_key, user) field_data_cache = FieldDataCache([course_descriptor], course_key, user)
course_content = module_render.get_module( course_content = module_render.get_module_for_descriptor(
user, user,
request, request,
course_descriptor.location, course_descriptor,
field_data_cache, field_data_cache,
course_key) course_key)
return course_descriptor, course_key, course_content return course_descriptor, course_key, course_content
...@@ -61,10 +61,10 @@ def get_course_child(request, user, course_key, content_id): ...@@ -61,10 +61,10 @@ def get_course_child(request, user, course_key, content_id):
pass pass
if content_descriptor: if content_descriptor:
field_data_cache = FieldDataCache([content_descriptor], course_key, user) field_data_cache = FieldDataCache([content_descriptor], course_key, user)
content = module_render.get_module( content = module_render.get_module_for_descriptor(
user, user,
request, request,
content_key, content_descriptor,
field_data_cache, field_data_cache,
course_key) course_key)
return content_descriptor, content_key, content return content_descriptor, content_key, content
......
...@@ -47,6 +47,8 @@ from opaque_keys.edx.keys import UsageKey, CourseKey ...@@ -47,6 +47,8 @@ from opaque_keys.edx.keys import UsageKey, CourseKey
from opaque_keys.edx.locations import Location from opaque_keys.edx.locations import Location
from xmodule.modulestore import InvalidLocationError from xmodule.modulestore import InvalidLocationError
from xmodule.modulestore.django import modulestore
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
AUDIT_LOG = logging.getLogger("audit") AUDIT_LOG = logging.getLogger("audit")
...@@ -719,6 +721,27 @@ class UsersCoursesList(SecureAPIView): ...@@ -719,6 +721,27 @@ class UsersCoursesList(SecureAPIView):
return Response(response_data, status=status.HTTP_200_OK) return Response(response_data, status=status.HTTP_200_OK)
def _get_current_position_loc(parent_module):
"""
An optimized lookup for the current position. The LMS version can cause unnecessary round trips to
the Mongo database
"""
if not hasattr(parent_module, 'position'):
return None
if not parent_module.children:
return None
index = 0
if parent_module.position:
index = parent_module.position - 1 # position is 1 indexed
if 0 <= index < len(parent_module.children):
return parent_module.children[index]
return parent_module.children[0]
class UsersCoursesDetail(SecureAPIView): class UsersCoursesDetail(SecureAPIView):
""" """
### The UsersCoursesDetail view allows clients to interact with a specific User-Course relationship (aka, enrollment) ### The UsersCoursesDetail view allows clients to interact with a specific User-Course relationship (aka, enrollment)
...@@ -782,7 +805,7 @@ class UsersCoursesDetail(SecureAPIView): ...@@ -782,7 +805,7 @@ class UsersCoursesDetail(SecureAPIView):
base_uri = generate_base_uri(request) base_uri = generate_base_uri(request)
try: try:
user = User.objects.get(id=user_id, is_active=True) user = User.objects.get(id=user_id, is_active=True)
course_descriptor, course_key, course_content = get_course(request, user, course_id, depth=2) course_descriptor, course_key, course_content = get_course(request, user, course_id)
except (ObjectDoesNotExist, ValueError): except (ObjectDoesNotExist, ValueError):
return Response({}, status=status.HTTP_404_NOT_FOUND) return Response({}, status=status.HTTP_404_NOT_FOUND)
if not CourseEnrollment.is_enrolled(user, course_key): if not CourseEnrollment.is_enrolled(user, course_key):
...@@ -794,7 +817,7 @@ class UsersCoursesDetail(SecureAPIView): ...@@ -794,7 +817,7 @@ class UsersCoursesDetail(SecureAPIView):
course_key, course_key,
user, user,
course_descriptor, course_descriptor,
depth=2) depth=0)
course_module = module_render.get_module_for_descriptor( course_module = module_render.get_module_for_descriptor(
user, user,
request, request,
...@@ -805,17 +828,15 @@ class UsersCoursesDetail(SecureAPIView): ...@@ -805,17 +828,15 @@ class UsersCoursesDetail(SecureAPIView):
response_data['position_tree'] = {} response_data['position_tree'] = {}
parent_module = course_module parent_module = course_module
while parent_module is not None: while parent_module is not None:
current_child_descriptor = get_current_child(parent_module)
if current_child_descriptor: current_child_loc = _get_current_position_loc(parent_module)
response_data['position_tree'][current_child_descriptor.category] = {}
response_data['position_tree'][current_child_descriptor.category]['id'] = unicode(current_child_descriptor.scope_ids.usage_id) if current_child_loc:
parent_module = module_render.get_module( response_data['position_tree'][current_child_loc.category] = {}
user, response_data['position_tree'][current_child_loc.category]['id'] = unicode(current_child_loc)
request,
current_child_descriptor.scope_ids.usage_id, _,_,parent_module = get_course_child(request, user, course_key, unicode(current_child_loc))
field_data_cache,
course_key
)
else: else:
parent_module = None parent_module = None
return Response(response_data, status=status.HTTP_200_OK) return Response(response_data, status=status.HTTP_200_OK)
......
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