Skip to content

Commit 7f885c7

Browse files
fix: [FC-0092] fix 500 when return_type is list (#36969)
There was problem in filter_discussion_xblocks_from_response(). This function was breaking the list response for the BlocksInCourseView by returning a dict instead of list.
1 parent cb7f0f4 commit 7f885c7

2 files changed

Lines changed: 51 additions & 41 deletions

File tree

lms/djangoapps/course_api/blocks/utils.py

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,55 @@
1111

1212
def filter_discussion_xblocks_from_response(response, course_key):
1313
"""
14-
Removes discussion xblocks if discussion provider is openedx
14+
Removes discussion xblocks if discussion provider is openedx.
1515
"""
1616
configuration = DiscussionsConfiguration.get(context_key=course_key)
1717
provider = configuration.provider_type
18-
if provider == Provider.OPEN_EDX:
19-
# Finding ids of discussion xblocks
20-
if isinstance(response.data, ReturnList):
21-
discussion_xblocks = [
22-
value.get('id') for value in response.data if value.get('type') == 'discussion'
23-
]
24-
else:
25-
discussion_xblocks = [
26-
key for key, value in response.data.get('blocks', {}).items()
27-
if value.get('type') == 'discussion'
28-
]
29-
# Filtering discussion xblocks keys from blocks
30-
if isinstance(response.data, ReturnList):
31-
filtered_blocks = {
32-
value.get('id'): value
33-
for value in response.data
34-
if value.get('type') != 'discussion'
35-
}
36-
else:
37-
filtered_blocks = {
38-
key: value
39-
for key, value in response.data.get('blocks', {}).items()
40-
if value.get('type') != 'discussion'
41-
}
42-
# Removing reference of discussion xblocks from unit
43-
# These references needs to be removed because they no longer exist
44-
for _, block_data in filtered_blocks.items():
45-
for key in ['descendants', 'children']:
46-
descendants = block_data.get(key, [])
47-
if descendants:
48-
descendants = [
49-
descendant for descendant in descendants
50-
if descendant not in discussion_xblocks
51-
]
52-
block_data[key] = descendants
53-
if isinstance(response.data, ReturnList):
54-
response.data = filtered_blocks
55-
else:
56-
response.data['blocks'] = filtered_blocks
18+
19+
if provider != Provider.OPEN_EDX:
20+
return response
21+
22+
is_list_response = isinstance(response.data, ReturnList)
23+
24+
# Find discussion xblock IDs
25+
if is_list_response:
26+
discussion_xblocks = [
27+
block.get('id') for block in response.data
28+
if block.get('type') == 'discussion'
29+
]
30+
else:
31+
discussion_xblocks = [
32+
key for key, value in response.data.get('blocks', {}).items()
33+
if value.get('type') == 'discussion'
34+
]
35+
36+
# Filter out discussion blocks
37+
if is_list_response:
38+
filtered_blocks = [
39+
block for block in response.data
40+
if block.get('type') != 'discussion'
41+
]
42+
else:
43+
filtered_blocks = {
44+
key: value for key, value in response.data.get('blocks', {}).items()
45+
if value.get('type') != 'discussion'
46+
}
47+
48+
# Remove references to discussion xblocks
49+
# These references needs to be removed because they no longer exist
50+
blocks_iterable = filtered_blocks if is_list_response else filtered_blocks.values()
51+
for block_data in blocks_iterable:
52+
for key in ['descendants', 'children']:
53+
if key in block_data:
54+
block_data[key] = [
55+
descendant for descendant in block_data[key]
56+
if descendant not in discussion_xblocks
57+
]
58+
59+
# Update response
60+
if is_list_response:
61+
response.data = ReturnList(filtered_blocks, serializer=None)
62+
else:
63+
response.data['blocks'] = filtered_blocks
64+
5765
return response

lms/djangoapps/course_api/blocks/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,14 @@ def list(self, request, hide_access_denials=False): # pylint: disable=arguments
330330

331331
if course_block.get('type') == 'course':
332332
root = course_block['id']
333+
else:
334+
root = str(course_usage_key)
333335
else:
334336
root = response.data['root']
335337
course_blocks = response.data['blocks']
336338

337339
if not root:
338-
raise ValueError(f"Unable to find course block in {course_key_string}")
340+
raise ValidationError(f"Unable to find course block in '{course_key_string}'")
339341

340342
recurse_mark_complete(root, course_blocks)
341343
return response

0 commit comments

Comments
 (0)