Commit 390e447e by Xavier Antoviaque

light-children: Fix issues - WIP

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