From d91cadfeb39694e1725f7e70318ee1e1aac04d70 Mon Sep 17 00:00:00 2001 From: Muhammad Qasim Gulzar <19186089+qasimgulzar@users.noreply.github.com> Date: Sat, 11 May 2024 03:40:09 +0500 Subject: [PATCH] fix: Course blocks API with param return_type=list error when the new discussion is enabled (#34426) Co-authored-by: Jason Wesson --- lms/djangoapps/course_api/blocks/utils.py | 37 +++++++++++++++++------ 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/lms/djangoapps/course_api/blocks/utils.py b/lms/djangoapps/course_api/blocks/utils.py index 3a9fea117c23..0af24b951ade 100644 --- a/lms/djangoapps/course_api/blocks/utils.py +++ b/lms/djangoapps/course_api/blocks/utils.py @@ -1,6 +1,8 @@ """ Utils for Blocks """ +from rest_framework.utils.serializer_helpers import ReturnList + from openedx.core.djangoapps.discussions.models import ( DiscussionsConfiguration, Provider, @@ -15,16 +17,28 @@ def filter_discussion_xblocks_from_response(response, course_key): provider = configuration.provider_type if provider == Provider.OPEN_EDX: # Finding ids of discussion xblocks - discussion_xblocks = [ - key for key, value in response.data.get('blocks', {}).items() - if value.get('type') == 'discussion' - ] + if isinstance(response.data, ReturnList): + discussion_xblocks = [ + value.get('id') for value in response.data if value.get('type') == 'discussion' + ] + else: + discussion_xblocks = [ + key for key, value in response.data.get('blocks', {}).items() + if value.get('type') == 'discussion' + ] # Filtering discussion xblocks keys from blocks - filtered_blocks = { - key: value - for key, value in response.data.get('blocks', {}).items() - if value.get('type') != 'discussion' - } + if isinstance(response.data, ReturnList): + filtered_blocks = { + value.get('id'): value + for value in response.data + if value.get('type') != 'discussion' + } + else: + filtered_blocks = { + key: value + for key, value in response.data.get('blocks', {}).items() + if value.get('type') != 'discussion' + } # Removing reference of discussion xblocks from unit # These references needs to be removed because they no longer exist for _, block_data in filtered_blocks.items(): @@ -36,5 +50,8 @@ def filter_discussion_xblocks_from_response(response, course_key): if descendant not in discussion_xblocks ] block_data[key] = descendants - response.data['blocks'] = filtered_blocks + if isinstance(response.data, ReturnList): + response.data = filtered_blocks + else: + response.data['blocks'] = filtered_blocks return response