From b328c0dcdc48fb978eba305643afd8ede7d814d9 Mon Sep 17 00:00:00 2001 From: Waheed Ahmed Date: Wed, 10 Jun 2015 21:03:52 +0500 Subject: [PATCH] Fixed slow transaction on xblock outline handler. TNL-2425 --- cms/djangoapps/contentstore/views/item.py | 15 +++++++------ .../contentstore/views/tests/test_item.py | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/cms/djangoapps/contentstore/views/item.py b/cms/djangoapps/contentstore/views/item.py index ae197ee0f2..f0d7b80128 100644 --- a/cms/djangoapps/contentstore/views/item.py +++ b/cms/djangoapps/contentstore/views/item.py @@ -332,13 +332,14 @@ def xblock_outline_handler(request, usage_key_string): response_format = request.REQUEST.get('format', 'html') if response_format == 'json' or 'application/json' in request.META.get('HTTP_ACCEPT', 'application/json'): store = modulestore() - root_xblock = store.get_item(usage_key) - return JsonResponse(create_xblock_info( - root_xblock, - include_child_info=True, - course_outline=True, - include_children_predicate=lambda xblock: not xblock.category == 'vertical' - )) + with store.bulk_operations(usage_key.course_key): + root_xblock = store.get_item(usage_key) + return JsonResponse(create_xblock_info( + root_xblock, + include_child_info=True, + course_outline=True, + include_children_predicate=lambda xblock: not xblock.category == 'vertical' + )) else: return Http404 diff --git a/cms/djangoapps/contentstore/views/tests/test_item.py b/cms/djangoapps/contentstore/views/tests/test_item.py index 3632fd4d75..c29bee1f1b 100644 --- a/cms/djangoapps/contentstore/views/tests/test_item.py +++ b/cms/djangoapps/contentstore/views/tests/test_item.py @@ -1410,6 +1410,28 @@ class TestXBlockInfo(ItemTest): json_response = json.loads(resp.content) self.validate_course_xblock_info(json_response, course_outline=True) + def test_xblock_outline_handler_mongo_calls(self): + expected_calls = 5 + with self.store.default_store(ModuleStoreEnum.Type.split): + course = CourseFactory.create() + chapter = ItemFactory.create( + parent_location=course.location, category='chapter', display_name='Week 1' + ) + outline_url = reverse_usage_url('xblock_outline_handler', chapter.location) + with check_mongo_calls(expected_calls): + self.client.get(outline_url, HTTP_ACCEPT='application/json') + + sequential = ItemFactory.create( + parent_location=chapter.location, category='sequential', display_name='Sequential 1' + ) + + ItemFactory.create( + parent_location=sequential.location, category='vertical', display_name='Vertical 1' + ) + # calls should be same after adding two new children. + with check_mongo_calls(expected_calls): + self.client.get(outline_url, HTTP_ACCEPT='application/json') + def test_entrance_exam_chapter_xblock_info(self): chapter = ItemFactory.create( parent_location=self.course.location, category='chapter', display_name="Entrance Exam",