Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
warnings.warn(
"Passing an id_generator directly is deprecated "
"in favor of constructing the Runtime with the id_generator",
DeprecationWarning,
stacklevel=3,
)
id_generator = id_generator or self.id_generator
block_type = node.tag
# remove xblock-family from elements
node.attrib.pop('xblock-family', None)
# TODO: a way for this node to be a usage to an existing definition?
def_id = id_generator.create_definition(block_type)
usage_id = id_generator.create_usage(def_id)
keys = ScopeIds(None, block_type, def_id, usage_id)
block_class = self.mixologist.mix(self.load_block_type(block_type))
# pull the asides out of the xml payload
aside_children = []
for child in node.iterchildren():
# get xblock-family from node
xblock_family = child.attrib.pop('xblock-family', None)
if xblock_family:
xblock_family = self._family_id_to_superclass(xblock_family)
if issubclass(xblock_family, XBlockAside):
aside_children.append(child)
# now process them & remove them from the xml payload
for child in aside_children:
self._aside_from_xml(child, def_id, usage_id, id_generator)
node.remove(child)
block = block_class.parse_xml(node, self, keys, id_generator)
block.parent = parent_id
def_id = _id_generator.create_definition(block_type)
usage_id = _id_generator.create_usage(def_id, usage_id=def_id)
else:
# Test whether or not the usage is already in the datastore. If it
# is not present, there will be a NoSuchUsage exception.
try:
def_id = self.id_reader.get_definition_id(usage_id)
except xblock.exceptions.NoSuchUsage:
# In Course Builder the usages and defs are in 1-1
# correspondence so for definiteness, make id's the same
def_id = usage_id
def_id = _id_generator.create_definition(
block_type, def_id=def_id)
_id_generator.create_usage(def_id, usage_id=usage_id)
keys = xblock.fields.ScopeIds(
xblock.fields.UserScope.NONE, block_type, def_id, usage_id)
block_class = self.mixologist.mix(self.load_block_type(block_type))
# Load the block's fields and clear out any existing children
block = self.construct_xblock_from_class(block_class, keys)
if hasattr(block, 'children'):
# We need to force an explict save of the 'children' field
# and so first we have to make it dirty
block.children = ['dirt']
block.save()
block.children = []
block.save()
# Reload the block and attach new children
block = block_class.parse_xml(node, self, keys, _id_generator)
block.parent = parent_id
aside_type (`str`): the type of the aside
"""
# TODO: This function will need to be extended if we want to allow:
# a) XBlockAsides to statically indicated which types of blocks they can comment on
# b) XBlockRuntimes to limit the selection of asides to a subset of the installed asides
# c) Optimize by only loading asides that actually decorate a particular view
if self.id_generator is None:
raise Exception("Runtimes must be supplied with an IdGenerator to load XBlockAsides.")
usage_id = block.scope_ids.usage_id
aside_cls = self.load_aside_type(aside_type)
definition_id = self.id_reader.get_definition_id(usage_id)
aside_def_id, aside_usage_id = self.id_generator.create_aside(definition_id, usage_id, aside_type)
scope_ids = ScopeIds(self.user_id, aside_type, aside_def_id, aside_usage_id)
return aside_cls(runtime=self, scope_ids=scope_ids)
def get_aside(self, aside_usage_id):
"""
Create an XBlockAside in this runtime.
The `aside_usage_id` is used to find the Aside class and data.
"""
aside_type = self.id_reader.get_aside_type_from_usage(aside_usage_id)
xblock_usage = self.id_reader.get_usage_id_from_aside(aside_usage_id)
xblock_def = self.id_reader.get_definition_id(xblock_usage)
aside_def_id, aside_usage_id = self.id_generator.create_aside(xblock_def, xblock_usage, aside_type)
keys = ScopeIds(self.user_id, aside_type, aside_def_id, aside_usage_id)
block = self.create_aside(aside_type, keys)
return block
def _aside_from_xml(self, node, block_def_id, block_usage_id, id_generator):
"""
Create an aside from the xml and attach it to the given block
"""
id_generator = id_generator or self.id_generator
aside_type = node.tag
aside_class = self.load_aside_type(aside_type)
aside_def_id, aside_usage_id = id_generator.create_aside(block_def_id, block_usage_id, aside_type)
keys = ScopeIds(None, aside_type, aside_def_id, aside_usage_id)
aside = aside_class.parse_xml(node, self, keys, id_generator)
aside.save()