Commit e933c9fe by Ben McMorran

Fix error when changing course details on split

parent 30fc0441
...@@ -86,14 +86,18 @@ class CourseDetails(object): ...@@ -86,14 +86,18 @@ class CourseDetails(object):
temploc = course_key.make_usage_key('about', about_key) temploc = course_key.make_usage_key('about', about_key)
store = modulestore() store = modulestore()
if data is None: if data is None:
store.delete_item(temploc, user.id) try:
store.delete_item(temploc, user.id)
# Ignore an attempt to delete an item that doesn't exist
except ValueError:
pass
else: else:
try: try:
about_item = store.get_item(temploc) about_item = store.get_item(temploc)
except ItemNotFoundError: except ItemNotFoundError:
about_item = store.create_xblock(course.runtime, course.id, 'about', about_key) about_item = store.create_xblock(course.runtime, course.id, 'about', about_key)
about_item.data = data about_item.data = data
store.update_item(about_item, user.id) store.update_item(about_item, user.id, allow_not_found=True)
@classmethod @classmethod
def update_from_json(cls, course_key, jsondict, user): def update_from_json(cls, course_key, jsondict, user):
......
...@@ -1500,17 +1500,20 @@ class SplitMongoModuleStore(ModuleStoreWriteBase): ...@@ -1500,17 +1500,20 @@ class SplitMongoModuleStore(ModuleStoreWriteBase):
original_structure = self._lookup_course(usage_locator.course_key)['structure'] original_structure = self._lookup_course(usage_locator.course_key)['structure']
if original_structure['root'] == usage_locator.block_id: if original_structure['root'] == usage_locator.block_id:
raise ValueError("Cannot delete the root of a course") raise ValueError("Cannot delete the root of a course")
if encode_key_for_mongo(usage_locator.block_id) not in original_structure['blocks']:
raise ValueError("Cannot delete a block that does not exist")
index_entry = self._get_index_if_valid(usage_locator, force) index_entry = self._get_index_if_valid(usage_locator, force)
new_structure = self._version_structure(original_structure, user_id) new_structure = self._version_structure(original_structure, user_id)
new_blocks = new_structure['blocks'] new_blocks = new_structure['blocks']
new_id = new_structure['_id'] new_id = new_structure['_id']
encoded_block_id = self._get_parent_from_structure(usage_locator.block_id, original_structure) encoded_block_id = self._get_parent_from_structure(usage_locator.block_id, original_structure)
parent_block = new_blocks[encoded_block_id] if encoded_block_id:
parent_block['fields']['children'].remove(usage_locator.block_id) parent_block = new_blocks[encoded_block_id]
parent_block['edit_info']['edited_on'] = datetime.datetime.now(UTC) parent_block['fields']['children'].remove(usage_locator.block_id)
parent_block['edit_info']['edited_by'] = user_id parent_block['edit_info']['edited_on'] = datetime.datetime.now(UTC)
parent_block['edit_info']['previous_version'] = parent_block['edit_info']['update_version'] parent_block['edit_info']['edited_by'] = user_id
parent_block['edit_info']['update_version'] = new_id parent_block['edit_info']['previous_version'] = parent_block['edit_info']['update_version']
parent_block['edit_info']['update_version'] = new_id
def remove_subtree(block_id): def remove_subtree(block_id):
""" """
......
...@@ -1332,6 +1332,8 @@ class TestItemCrud(SplitModuleTest): ...@@ -1332,6 +1332,8 @@ class TestItemCrud(SplitModuleTest):
self.assertFalse(modulestore().has_item(deleted)) self.assertFalse(modulestore().has_item(deleted))
with self.assertRaises(VersionConflictError): with self.assertRaises(VersionConflictError):
modulestore().has_item(locn_to_del) modulestore().has_item(locn_to_del)
with self.assertRaises(ValueError):
modulestore().delete_item(deleted, self.user_id)
self.assertTrue(modulestore().has_item(locn_to_del.course_agnostic())) self.assertTrue(modulestore().has_item(locn_to_del.course_agnostic()))
self.assertNotEqual(new_course_loc.version_guid, course.location.version_guid) self.assertNotEqual(new_course_loc.version_guid, course.location.version_guid)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment