From dd1f2081994e900db3a85d3c7114ff0a745e4fd6 Mon Sep 17 00:00:00 2001 From: monteri Date: Thu, 25 Jan 2024 19:32:33 +0200 Subject: [PATCH 1/3] feat: add sequence_ids to container response --- .../rest_api/v1/serializers/vertical_block.py | 1 + .../rest_api/v1/views/vertical_block.py | 5 ++++ cms/djangoapps/contentstore/utils.py | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py b/cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py index 0900ba39d2b3..809ebf22bd5d 100644 --- a/cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py +++ b/cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py @@ -78,6 +78,7 @@ class ContainerHandlerSerializer(serializers.Serializer): assets_url = serializers.SerializerMethodField() unit_block_id = serializers.CharField(source="unit.location.block_id") subsection_location = serializers.CharField(source="subsection.location") + sequence_ids = serializers.ListField(child=serializers.CharField()) def get_assets_url(self, obj): """ diff --git a/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py b/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py index a11380a08a66..84505998e6d8 100644 --- a/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py +++ b/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py @@ -117,6 +117,11 @@ def get(self, request: Request, usage_key_string: str): "assets_url": "/assets/course-v1:edX+DemoX+Demo_Course/", "unit_block_id": "d6cee45205a449369d7ef8f159b22bdf", "subsection_location": "block-v1:edX+DemoX+Demo_Course+type@sequential+block@graded_simulations" + "sequence_ids": [ + "block-v1:edX+DemoX+Demo_Course+type@sequential+block@graded_simulations", + "block-v1:edX+DemoX+Demo_Course+type@sequential+block@something_else", + ... + ], } ``` """ diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index 748e6a85a0d4..cd6fda9c3b3a 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -632,6 +632,31 @@ def ancestor_has_staff_lock(xblock, parent_xblock=None): return parent_xblock.visible_to_staff_only +def get_sequence_usage_keys(course_structure): + """ + Recursively searches through a nested course_structure, + extracting and returning a list of usage_keys where the 'category' + key is set to 'sequential' + """ + ids = [] + + def check_category_and_children(child_info): + if child_info.get('category') == 'sequential': + ids.extend([c.get('id') for c in child_info.get('children', [])]) + return + + # Recursively check children if they exist + for child in child_info.get('children', []): + if 'child_info' in child: + check_category_and_children(child['child_info']) + + if 'child_info' in course_structure: + check_category_and_children(course_structure['child_info']) + + return ids + + + def reverse_url(handler_name, key_name=None, key_value=None, kwargs=None): """ Creates the URL for the given handler. @@ -1847,7 +1872,10 @@ def get_container_handler_context(request, usage_key, course, xblock): # pylint create_xblock_info, ) from openedx.core.djangoapps.content_staging import api as content_staging_api + from cms.djangoapps.contentstore.views.course import _course_outline_json + course_structure = _course_outline_json(request, course) + sequence_ids = get_sequence_usage_keys(course_structure) component_templates = get_component_templates(course) ancestor_xblocks = [] parent = get_parent_xblock(xblock) @@ -1948,6 +1976,7 @@ def get_container_handler_context(request, usage_key, course, xblock): # pylint # Status of the user's clipboard, exactly as would be returned from the "GET clipboard" REST API. 'user_clipboard': user_clipboard, 'is_fullwidth_content': is_library_xblock, + 'sequence_ids': sequence_ids, } return context From 46e5155cbf863486e9fccb4c20a9daa22d07dbbb Mon Sep 17 00:00:00 2001 From: monteri Date: Fri, 26 Jan 2024 11:45:04 +0200 Subject: [PATCH 2/3] fix: PR comments fix --- .../rest_api/v1/serializers/vertical_block.py | 2 +- .../rest_api/v1/views/vertical_block.py | 2 +- cms/djangoapps/contentstore/utils.py | 35 ++++++------------- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py b/cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py index 809ebf22bd5d..6aa1f1af27c0 100644 --- a/cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py +++ b/cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py @@ -78,7 +78,7 @@ class ContainerHandlerSerializer(serializers.Serializer): assets_url = serializers.SerializerMethodField() unit_block_id = serializers.CharField(source="unit.location.block_id") subsection_location = serializers.CharField(source="subsection.location") - sequence_ids = serializers.ListField(child=serializers.CharField()) + course_sequence_ids = serializers.ListField(child=serializers.CharField()) def get_assets_url(self, obj): """ diff --git a/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py b/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py index 84505998e6d8..584d21428854 100644 --- a/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py +++ b/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py @@ -117,7 +117,7 @@ def get(self, request: Request, usage_key_string: str): "assets_url": "/assets/course-v1:edX+DemoX+Demo_Course/", "unit_block_id": "d6cee45205a449369d7ef8f159b22bdf", "subsection_location": "block-v1:edX+DemoX+Demo_Course+type@sequential+block@graded_simulations" - "sequence_ids": [ + "course_sequence_ids": [ "block-v1:edX+DemoX+Demo_Course+type@sequential+block@graded_simulations", "block-v1:edX+DemoX+Demo_Course+type@sequential+block@something_else", ... diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index cd6fda9c3b3a..9498b3dfbceb 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -632,29 +632,18 @@ def ancestor_has_staff_lock(xblock, parent_xblock=None): return parent_xblock.visible_to_staff_only -def get_sequence_usage_keys(course_structure): +def get_sequence_usage_keys(course): """ - Recursively searches through a nested course_structure, - extracting and returning a list of usage_keys where the 'category' - key is set to 'sequential' + Extracts a list of 'subsections' usage_keys """ - ids = [] - - def check_category_and_children(child_info): - if child_info.get('category') == 'sequential': - ids.extend([c.get('id') for c in child_info.get('children', [])]) - return - - # Recursively check children if they exist - for child in child_info.get('children', []): - if 'child_info' in child: - check_category_and_children(child['child_info']) - - if 'child_info' in course_structure: - check_category_and_children(course_structure['child_info']) - - return ids + course_sequence_ids = [] + sections = course.get_children() + for section in sections: + subsections = section.get_children() + for subsection in subsections: + course_sequence_ids.append(str(subsection.location)) + return course_sequence_ids def reverse_url(handler_name, key_name=None, key_value=None, kwargs=None): @@ -1872,10 +1861,8 @@ def get_container_handler_context(request, usage_key, course, xblock): # pylint create_xblock_info, ) from openedx.core.djangoapps.content_staging import api as content_staging_api - from cms.djangoapps.contentstore.views.course import _course_outline_json - course_structure = _course_outline_json(request, course) - sequence_ids = get_sequence_usage_keys(course_structure) + course_sequence_ids = get_sequence_usage_keys(course) component_templates = get_component_templates(course) ancestor_xblocks = [] parent = get_parent_xblock(xblock) @@ -1976,7 +1963,7 @@ def get_container_handler_context(request, usage_key, course, xblock): # pylint # Status of the user's clipboard, exactly as would be returned from the "GET clipboard" REST API. 'user_clipboard': user_clipboard, 'is_fullwidth_content': is_library_xblock, - 'sequence_ids': sequence_ids, + 'course_sequence_ids': course_sequence_ids, } return context From 4db5cee519f8ed71b32c53cbdfc1146bd8f60836 Mon Sep 17 00:00:00 2001 From: monteri Date: Fri, 26 Jan 2024 13:05:46 +0200 Subject: [PATCH 3/3] fix: update --- cms/djangoapps/contentstore/utils.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index 9498b3dfbceb..91e4a06693c5 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -636,14 +636,9 @@ def get_sequence_usage_keys(course): """ Extracts a list of 'subsections' usage_keys """ - course_sequence_ids = [] - sections = course.get_children() - for section in sections: - subsections = section.get_children() - for subsection in subsections: - course_sequence_ids.append(str(subsection.location)) - - return course_sequence_ids + return [str(subsection.location) + for section in course.get_children() + for subsection in section.get_children()] def reverse_url(handler_name, key_name=None, key_value=None, kwargs=None):