Commit 390e447e by Xavier Antoviaque

light-children: Fix issues - WIP

parent d0da3ceb
......@@ -44,7 +44,7 @@ class HTMLBlock(LightChild):
content = String(help="HTML content", scope=Scope.content, default="")
@classmethod
def init_block_from_node(cls, block, node):
def init_block_from_node(cls, block, node, attr):
block.light_children = []
# TODO-LIGHT-CHILDREN: get real value from `node` (lxml)
......
......@@ -25,6 +25,10 @@
import logging
from cStringIO import StringIO
from lxml import etree
from xblock.core import XBlock
from xblock.fragment import Fragment
from xblock.plugin import Plugin
......@@ -36,9 +40,16 @@ from .utils import XBlockWithChildrenFragmentsMixin
log = logging.getLogger(__name__)
# Functions #########################################################
def node_to_xml(node):
# TODO-LIGHT-CHILDREN
return '<mentoring><html>Hello</html><answer name="test1" /></mentoring>'
# Classes ###########################################################
class XBlockWithLightChildrenMixin(XBlockWithChildrenFragmentsMixin):
class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
"""
Allows to use lightweight children on a given XBlock, which will
have a similar behavior but will not be instanciated as full-fledged
......@@ -58,34 +69,20 @@ class XBlockWithLightChildrenMixin(XBlockWithChildrenFragmentsMixin):
* fields on LightChild don't have any persistence
"""
def __init__(self, *args, **kwargs):
self.load_children_from_xml_content()
def load_children_from_xml_content(self):
"""
Load light children from the `xml_content` attribute
"""
if not hasattr(self, 'xml_content') or not self.xml_content:
return
# TODO-LIGHT-CHILDREN: replace by proper lxml call
node = None # lxml.load(self.xml_content)
self.init_block_from_node(self, node)
@classmethod
def parse_xml(cls, node, runtime, keys, id_generator):
block = runtime.construct_xblock_from_class(cls, keys)
cls.init_block_from_node(block, node)
cls.init_block_from_node(block, node, node.items())
block.xml_content = getattr(block, 'xml_content', '') or node_to_xml(node)
return block
@classmethod
def init_block_from_node(cls, block, node):
def init_block_from_node(cls, block, node, attr):
block.light_children = []
for child_id, xml_child in enumerate(node):
cls.add_node_as_child(block, xml_child, child_id)
for name, value in node.items():
for name, value in attr:
setattr(block, name, value)
return block
......@@ -97,10 +94,8 @@ class XBlockWithLightChildrenMixin(XBlockWithChildrenFragmentsMixin):
child = child_class()
child.name = u'{}_{}'.format(block.name, child_id)
log.warn(child_class)
# Add any children the child may itself have
child_class.init_block_from_node(child, xml_child)
child_class.init_block_from_node(child, xml_child, xml_child.items())
block.light_children.append(child)
......@@ -108,6 +103,17 @@ class XBlockWithLightChildrenMixin(XBlockWithChildrenFragmentsMixin):
def get_class_by_element(cls, xml_tag):
return LightChild.load_class(xml_tag)
def load_children_from_xml_content(self):
"""
Load light children from the `xml_content` attribute
"""
self.light_children = []
if not hasattr(self, 'xml_content') or not self.xml_content:
return
node = etree.parse(StringIO(self.xml_content)).getroot()
LightChildrenMixin.init_block_from_node(self, node, {})
def get_children_objects(self):
"""
Replacement for ```[self.runtime.get_block(child_id) for child_id in self.children]```
......@@ -135,7 +141,16 @@ class XBlockWithLightChildrenMixin(XBlockWithChildrenFragmentsMixin):
return fragment, named_child_frags
class LightChild(Plugin, XBlockWithLightChildrenMixin):
class XBlockWithLightChildren(LightChildrenMixin, XBlock):
"""
XBlock base class with support for LightChild
"""
def __init__(self, *args, **kwargs):
super(XBlockWithLightChildren, self).__init__(*args, **kwargs)
self.load_children_from_xml_content()
class LightChild(Plugin, LightChildrenMixin):
"""
Base class for the light children
"""
......
......@@ -28,7 +28,7 @@ import logging
from xblock.core import XBlock
from xblock.fields import Boolean, Scope, String
from .light_children import XBlockWithLightChildrenMixin
from .light_children import XBlockWithLightChildren
from .message import MentoringMessageBlock
from .utils import get_scenarios_from_path, load_resource, render_template
......@@ -40,7 +40,7 @@ log = logging.getLogger(__name__)
# Classes ###########################################################
class MentoringBlock(XBlockWithLightChildrenMixin, XBlock):
class MentoringBlock(XBlockWithLightChildren):
"""
An XBlock providing mentoring capabilities
......@@ -65,6 +65,7 @@ class MentoringBlock(XBlockWithLightChildrenMixin, XBlock):
has_children = True
def student_view(self, context):
log.warn('xml_content => {}'.format(self.xml_content))
fragment, named_children = self.get_children_fragment(context, view_name='mentoring_view',
not_instance_of=MentoringMessageBlock)
......
......@@ -67,7 +67,7 @@ class QuizzBlock(LightChild):
high = String(help="Label for high ratings", scope=Scope.content, default="More")
@classmethod
def init_block_from_node(cls, block, node):
def init_block_from_node(cls, block, node, attr):
block.light_children = []
for child_id, xml_child in enumerate(node):
if xml_child.tag == "question":
......@@ -75,7 +75,7 @@ class QuizzBlock(LightChild):
else:
cls.add_node_as_child(block, xml_child, child_id)
for name, value in node.items():
for name, value in attr:
setattr(block, name, value)
return block
......
......@@ -26,10 +26,9 @@
import errno
import logging
from xblock.core import XBlock
from xblock.fields import Scope, String
from .light_children import LightChild, XBlockWithLightChildrenMixin, String as LightString
from .light_children import LightChild, XBlockWithLightChildren, String as LightString
from .utils import load_resource, render_template
......@@ -41,7 +40,7 @@ log = logging.getLogger(__name__)
# Classes ###########################################################
# TODO-LIGHT-CHILDREN: Transform this into always using as LightChildren
class MentoringTableBlock(XBlockWithLightChildrenMixin, XBlock):
class MentoringTableBlock(XBlockWithLightChildren):
"""
Table-type display of information from mentoring blocks
......@@ -57,7 +56,7 @@ class MentoringTableBlock(XBlockWithLightChildrenMixin, XBlock):
f, header_frags = self.get_children_fragment(context, view_name='mentoring_table_header_view')
# TODO: What's the right way to link to images from CSS? This hack won't work in prod
bg_image_url = self.runtime.resources_url('mentoring/img/{}-bg.png'.format(self.type))
bg_image_url = '' #self.runtime.resources_url('mentoring/img/{}-bg.png'.format(self.type))
# Load an optional description for the background image, for accessibility
try:
......
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