delete_item should not delete any child that has a parent we do not intend to delete

This commit is contained in:
Adam Palay
2015-09-15 10:10:34 -04:00
parent 99c410dc16
commit 8834bbad90
4 changed files with 89 additions and 51 deletions

View File

@@ -2399,7 +2399,7 @@ class SplitMongoModuleStore(SplitBulkWriteMixin, ModuleStoreWriteBase):
parent_block.edit_info.source_version = None
self.decache_block(usage_locator.course_key, new_id, parent_block_key)
self._remove_subtree(BlockKey.from_usage_key(usage_locator), new_blocks)
self._remove_subtree(BlockKey.from_usage_key(usage_locator), new_structure)
# update index if appropriate and structures
self.update_structure(usage_locator.course_key, new_structure)
@@ -2416,14 +2416,30 @@ class SplitMongoModuleStore(SplitBulkWriteMixin, ModuleStoreWriteBase):
return result
@contract(block_key=BlockKey, blocks='dict(BlockKey: BlockData)')
def _remove_subtree(self, block_key, blocks):
@contract(block_key=BlockKey, structure='dict')
def _remove_subtree(self, block_key, structure):
"""
Remove the subtree rooted at block_key
We do this breadth-first to make sure that we don't remove
any children that may have parents that we don't want to delete.
"""
for child in blocks[block_key].fields.get('children', []):
self._remove_subtree(BlockKey(*child), blocks)
del blocks[block_key]
to_delete = {block_key}
tier = {block_key}
while tier:
new_tier = set()
for block_key in tier:
for child in structure['blocks'][block_key].fields.get('children', []):
child_block_key = BlockKey(*child)
parents = self._get_parents_from_structure(child_block_key, structure)
# Make sure we want to delete all of the child's parents
# before slating it for deletion
if all(parent in to_delete for parent in parents):
new_tier.add(child_block_key)
tier = new_tier
to_delete.update(tier)
for block_key in to_delete:
del structure['blocks'][block_key]
def delete_course(self, course_key, user_id):
"""

View File

@@ -410,7 +410,7 @@ class DraftVersioningModuleStore(SplitMongoModuleStore, ModuleStoreDraftAndPubli
new_structure = self.version_structure(draft_course_key, draft_course_structure, user_id)
# remove the block and its descendants from the new structure
self._remove_subtree(BlockKey.from_usage_key(location), new_structure['blocks'])
self._remove_subtree(BlockKey.from_usage_key(location), new_structure)
# copy over the block and its descendants from the published branch
def copy_from_published(root_block_id):