fix: remove chapters if thre are no sequentials in Topics v3 (#31625)

This commit is contained in:
Ahtisham Shahid
2023-01-24 13:18:27 +05:00
committed by GitHub
parent 3a3d47f0ef
commit 536fe66362
2 changed files with 15 additions and 5 deletions

View File

@@ -131,7 +131,12 @@ class TestRemoveEmptySequentials(unittest.TestCase):
{"type": "sequential", "children": [{"type": "vertical1"}]},
{"type": "sequential", "children": []},
{"children": [{"type": "vertical2"}]}
]}
]},
{"type": "chapter", "children": [
{"type": "sequential", "children": []},
{"type": "sequential", "children": []},
]
}
]
expected_output = [
{"type": "chapter", "children": [

View File

@@ -274,22 +274,27 @@ def create_topics_v3_structure(blocks, topics):
def remove_empty_sequentials(data):
"""
Removes all objects of type "sequential" from a nested list of objects if they have no children.
Removes all objects of type "sequential" from a nested list of objects if they
have no children.
After removing the empty sequentials, if the parent of the sequential is now empty,
it will also be removed.
Parameters:
data (list): A list of nested objects to check and remove empty sequentials from.
Returns:
list: The modified list with empty sequentials removed.
"""
new_data = []
for obj in data:
block_type = obj.get('type')
if block_type != 'sequential' or (block_type == 'sequential' and obj.get('children')):
new_data.append(obj)
if obj.get('children'):
obj['children'] = remove_empty_sequentials(obj['children'])
if obj['children'] or block_type != 'chapter':
new_data.append(obj)
else:
if block_type != 'chapter':
new_data.append(obj)
return new_data