|
11 | 11 |
|
12 | 12 | def filter_discussion_xblocks_from_response(response, course_key): |
13 | 13 | """ |
14 | | - Removes discussion xblocks if discussion provider is openedx |
| 14 | + Removes discussion xblocks if discussion provider is openedx. |
15 | 15 | """ |
16 | 16 | configuration = DiscussionsConfiguration.get(context_key=course_key) |
17 | 17 | 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 | + |
57 | 65 | return response |
0 commit comments