update _xmodule_recurse method
This commit is contained in:
@@ -4,7 +4,6 @@ from django.http import HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
from edxmako.shortcuts import render_to_string, render_to_response
|
||||
from xmodule.modulestore.django import loc_mapper, modulestore
|
||||
from xmodule.modulestore.exceptions import DuplicateItemError, ItemNotFoundError
|
||||
|
||||
__all__ = ['edge', 'event', 'landing']
|
||||
|
||||
@@ -37,24 +36,20 @@ def render_from_lms(template_name, dictionary, context=None, namespace='main'):
|
||||
return render_to_string(template_name, dictionary, context, namespace="lms." + namespace)
|
||||
|
||||
|
||||
def _xmodule_recurse(item, action, ignore_exception=None):
|
||||
def _xmodule_recurse(item, action, ignore_exception=()):
|
||||
"""
|
||||
Recursively apply provided action on item and its children
|
||||
|
||||
ignore_duplicate_exception (str): A optional argument; when set to a certain value then ignores the corresponding
|
||||
exception raised while xmodule recursion
|
||||
ignore_exception (Exception Object): A optional argument; when passed ignores the corresponding
|
||||
exception raised during xmodule recursion,
|
||||
"""
|
||||
for child in item.get_children():
|
||||
_xmodule_recurse(child, action, ignore_exception)
|
||||
|
||||
if ignore_exception in ['ItemNotFoundError', 'DuplicateItemError']:
|
||||
# In case of valid provided exception; ignore it and continue recursion
|
||||
try:
|
||||
return action(item)
|
||||
except (ItemNotFoundError, DuplicateItemError):
|
||||
return
|
||||
|
||||
action(item)
|
||||
try:
|
||||
return action(item)
|
||||
except ignore_exception:
|
||||
return
|
||||
|
||||
|
||||
def get_parent_xblock(xblock):
|
||||
|
||||
@@ -21,7 +21,7 @@ from xblock.fragment import Fragment
|
||||
|
||||
import xmodule
|
||||
from xmodule.modulestore.django import modulestore, loc_mapper
|
||||
from xmodule.modulestore.exceptions import ItemNotFoundError, InvalidLocationError
|
||||
from xmodule.modulestore.exceptions import ItemNotFoundError, InvalidLocationError, DuplicateItemError
|
||||
from xmodule.modulestore.inheritance import own_metadata
|
||||
from xmodule.modulestore.locator import BlockUsageLocator
|
||||
from xmodule.modulestore import Location
|
||||
@@ -313,7 +313,7 @@ def _save_item(request, usage_loc, item_location, data=None, children=None, meta
|
||||
_xmodule_recurse(
|
||||
existing_item,
|
||||
lambda i: modulestore().unpublish(i.location),
|
||||
ignore_exception='ItemNotFoundError'
|
||||
ignore_exception=ItemNotFoundError
|
||||
)
|
||||
elif publish == 'create_draft':
|
||||
# This recursively clones the existing item location to a draft location (the draft is
|
||||
@@ -321,7 +321,7 @@ def _save_item(request, usage_loc, item_location, data=None, children=None, meta
|
||||
_xmodule_recurse(
|
||||
existing_item,
|
||||
lambda i: modulestore().convert_to_draft(i.location),
|
||||
ignore_exception='DuplicateItemError'
|
||||
ignore_exception=DuplicateItemError
|
||||
)
|
||||
|
||||
if data:
|
||||
|
||||
Reference in New Issue
Block a user