Commit 76bf8031 by Jonathan Piacenti

Provide a path for default xml to load upon creation.

parent b59e5e4f
...@@ -55,6 +55,16 @@ from .utils import XBlockWithChildrenFragmentsMixin ...@@ -55,6 +55,16 @@ from .utils import XBlockWithChildrenFragmentsMixin
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def get_xml_content(block):
"""
Because we get the XML content dynamically, we can't use the String field's
default kwarg to fall back on. Therefore any time we need to access the
xml_content's value, we need to do it through this function.
"""
default = getattr(block, 'default_xml_content', '')
value = getattr(block, 'xml_content', '')
return value or default
# Classes ########################################################### # Classes ###########################################################
class LightChildrenMixin(XBlockWithChildrenFragmentsMixin): class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
...@@ -83,7 +93,7 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin): ...@@ -83,7 +93,7 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
log.debug('parse_xml called') log.debug('parse_xml called')
block = runtime.construct_xblock_from_class(cls, keys) block = runtime.construct_xblock_from_class(cls, keys)
cls.init_block_from_node(block, node, node.items()) cls.init_block_from_node(block, node, node.items())
block.xml_content = getattr(block, 'xml_content', '') or etree.tostring(node) block.xml_content = get_xml_content(block) or etree.tostring(node)
return block return block
@classmethod @classmethod
...@@ -126,11 +136,12 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin): ...@@ -126,11 +136,12 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
Load light children from the `xml_content` attribute Load light children from the `xml_content` attribute
""" """
self.light_children = [] self.light_children = []
if not hasattr(self, 'xml_content') or not self.xml_content: xml_content = get_xml_content(self)
if not xml_content:
return return
parser = etree.XMLParser(remove_comments=True) parser = etree.XMLParser(remove_comments=True)
node = etree.parse(StringIO(self.xml_content), parser=parser).getroot() node = etree.parse(StringIO(xml_content), parser=parser).getroot()
LightChildrenMixin.init_block_from_node(self, node, node.items()) LightChildrenMixin.init_block_from_node(self, node, node.items())
def get_children_objects(self): def get_children_objects(self):
......
...@@ -33,7 +33,7 @@ from xblock.core import XBlock ...@@ -33,7 +33,7 @@ from xblock.core import XBlock
from xblock.fields import Boolean, Scope, String, Integer, Float, List from xblock.fields import Boolean, Scope, String, Integer, Float, List
from xblock.fragment import Fragment from xblock.fragment import Fragment
from .light_children import XBlockWithLightChildren from .light_children import XBlockWithLightChildren, get_xml_content
from .title import TitleBlock from .title import TitleBlock
from .header import SharedHeaderBlock from .header import SharedHeaderBlock
from .html import HTMLBlock from .html import HTMLBlock
...@@ -71,6 +71,7 @@ class MentoringBlock(XBlockWithLightChildren, StepParentMixin): ...@@ -71,6 +71,7 @@ class MentoringBlock(XBlockWithLightChildren, StepParentMixin):
enforce_dependency = Boolean(help="Should the next step be the current block to complete?", enforce_dependency = Boolean(help="Should the next step be the current block to complete?",
default=False, scope=Scope.content, enforce_type=True) default=False, scope=Scope.content, enforce_type=True)
display_submit = Boolean(help="Allow to submit current block?", default=True, scope=Scope.content) display_submit = Boolean(help="Allow to submit current block?", default=True, scope=Scope.content)
# See get_xml_content from light_children.
xml_content = String(help="XML content", default='', scope=Scope.content) xml_content = String(help="XML content", default='', scope=Scope.content)
weight = Float(help="Defines the maximum total grade of the block.", weight = Float(help="Defines the maximum total grade of the block.",
default=1, scope=Scope.content, enforce_type=True) default=1, scope=Scope.content, enforce_type=True)
...@@ -373,7 +374,7 @@ class MentoringBlock(XBlockWithLightChildren, StepParentMixin): ...@@ -373,7 +374,7 @@ class MentoringBlock(XBlockWithLightChildren, StepParentMixin):
fragment = Fragment() fragment = Fragment()
fragment.add_content(render_template('templates/html/mentoring_edit.html', { fragment.add_content(render_template('templates/html/mentoring_edit.html', {
'self': self, 'self': self,
'xml_content': self.xml_content or self.default_xml_content, 'xml_content': get_xml_content(self)
})) }))
fragment.add_javascript_url( fragment.add_javascript_url(
self.runtime.local_resource_url(self, 'public/js/mentoring_edit.js')) self.runtime.local_resource_url(self, 'public/js/mentoring_edit.js'))
......
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