Skip to content

Commit 1d4e401

Browse files
authored
Merge pull request #37769 from mitodl/marslan/9456-delete-moved-sub-section
fix: try deleting the XBlock from draft-branch if not in published
2 parents 3b9d161 + 0b7a543 commit 1d4e401

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

xmodule/modulestore/split_mongo/split.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,10 +2529,36 @@ def delete_item(self, usage_locator, user_id, force=False): # lint-amnesty, pyl
25292529
if original_structure['root'] == block_key:
25302530
raise ValueError("Cannot delete the root of a course")
25312531
if block_key not in original_structure['blocks']:
2532-
raise ValueError("Cannot delete block_key {} from course {}, because that block does not exist.".format(
2533-
block_key,
2534-
usage_locator,
2535-
))
2532+
# When user move a full sub-section to another section
2533+
# These changes are in draft-branch only and when user delete this moved
2534+
# section, we need to delete it from draft-branch
2535+
draft_course_key = usage_locator.course_key.for_branch(ModuleStoreEnum.BranchName.draft)
2536+
try:
2537+
draft_structure = self._lookup_course(draft_course_key).structure
2538+
if block_key in draft_structure['blocks']:
2539+
# Block exists in draft, use draft structure instead
2540+
original_structure = draft_structure
2541+
log.info("Block %s found in draft branch, proceeding with deletion from draft", block_key)
2542+
else:
2543+
raise ValueError(
2544+
(
2545+
"Cannot delete block_key {} from course {}, "
2546+
"because that block does not exist in either branch."
2547+
).format(
2548+
block_key,
2549+
usage_locator,
2550+
)
2551+
)
2552+
except ItemNotFoundError as exc:
2553+
raise ValueError(
2554+
(
2555+
"Cannot delete block_key {} from course {}, "
2556+
"because that block does not exist."
2557+
).format(
2558+
block_key,
2559+
usage_locator,
2560+
)
2561+
) from exc
25362562
index_entry = self._get_index_if_valid(usage_locator.course_key, force)
25372563
new_structure = self.version_structure(usage_locator.course_key, original_structure, user_id)
25382564
new_blocks = new_structure['blocks']

xmodule/modulestore/tests/test_publish.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -810,14 +810,8 @@ def test_delete_draft_unit(self, modulestore_builder, revision_and_result):
810810
self.assertOLXIsDraftOnly(block_list_to_delete)
811811
# MODULESTORE_DIFFERENCE:
812812
if self.is_split_modulestore:
813-
if revision in (ModuleStoreEnum.RevisionOption.published_only, ModuleStoreEnum.RevisionOption.all):
814-
# Split throws an exception when trying to delete an item from the published branch
815-
# that isn't yet published.
816-
with pytest.raises(ValueError):
817-
self.delete_item(block_list_to_delete, revision=revision)
818-
else:
819-
self.delete_item(block_list_to_delete, revision=revision)
820-
self._check_for_item_deletion(block_list_to_delete, result)
813+
self.delete_item(block_list_to_delete, revision=revision)
814+
self._check_for_item_deletion(block_list_to_delete, result)
821815
else:
822816
raise Exception("Must test either Old Mongo or Split modulestore!")
823817

@@ -846,11 +840,8 @@ def test_split_delete_draft_vertical(self, modulestore_builder, revision_and_res
846840
# The vertical is a draft.
847841
self.assertOLXIsDraftOnly(block_list_to_delete)
848842
if revision in (ModuleStoreEnum.RevisionOption.published_only, ModuleStoreEnum.RevisionOption.all):
849-
# MODULESTORE_DIFFERENCE:
850-
# Split throws an exception when trying to delete an item from the published branch
851-
# that isn't yet published.
852-
with pytest.raises(ValueError):
853-
self.delete_item(block_list_to_delete, revision=revision)
843+
self.delete_item(block_list_to_delete, revision=revision)
844+
self._check_for_item_deletion(block_list_to_delete, result)
854845
else:
855846
self.delete_item(block_list_to_delete, revision=revision)
856847
self._check_for_item_deletion(block_list_to_delete, result)

0 commit comments

Comments
 (0)