Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lms/djangoapps/course_home_api/outline/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def get_blocks(self, block): # pylint: disable=missing-function-docstring
'type': block_type,
'has_scheduled_content': block.get('has_scheduled_content'),
'hide_from_toc': block.get('hide_from_toc'),
'is_gated': block.get('is_gated', False),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would rethink the name is_gated as it doesn't have as much intrinsic meaning / it differs from the other teminology we use across the platform. Perhaps something like is_restricted or access_restricted which is more explicit.

},
}
if 'special_exam_info' in self.context.get('extra_fields', []) and block.get('special_exam_info'):
Expand Down
24 changes: 18 additions & 6 deletions lms/djangoapps/course_home_api/outline/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,28 @@ def get(self, request, *args, **kwargs):
course_blocks = cache.get(cache_key)

if not course_blocks:
if getattr(enrollment, 'is_active', False) or bool(staff_access):
course_blocks = get_course_outline_block_tree(request, course_key_string, request.user)
elif allow_public_outline or allow_public or user_is_masquerading:
course_blocks = get_course_outline_block_tree(request, course_key_string, None)
all_blocks = get_course_outline_block_tree(request, course_key_string, None)
visible_blocks = get_course_outline_block_tree(request, course_key_string, request.user)
visible_ids = set()

def collect_ids(block):
visible_ids.add(block['id'])
for child in block.get('children', []):
collect_ids(child)
Comment on lines +479 to +482
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made significantly more efficient. This PR changes from traversing the entire course tree twice to traversing four times times. I'm going to challenge you to think about modifying the get or filter steps to preserve the metadata you want.


collect_ids(visible_blocks)

def mark_gated(block):
block['is_gated'] = block['id'] not in visible_ids
for child in block.get('children', []):
mark_gated(child)

mark_gated(all_blocks)
course_blocks = all_blocks

if not navigation_sidebar_caching_is_disabled:
cache.set(cache_key, course_blocks, self.COURSE_BLOCKS_CACHE_TIMEOUT)

course_blocks = self.filter_inaccessible_blocks(course_blocks, course_key)
course_blocks = self.mark_complete_recursive(course_blocks)

context = self.get_serializer_context()
Expand All @@ -491,7 +504,6 @@ def get(self, request, *args, **kwargs):
})

serializer = self.get_serializer_class()(course_blocks, context=context)

return Response(serializer.data)

def filter_inaccessible_blocks(self, course_blocks, course_key):
Expand Down