Skip to content
This repository was archived by the owner on Mar 18, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mentoring/components/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def parse_xml(cls, node, runtime, keys, id_generator):
setattr(block, name, value)

for xml_child in node:
if xml_child.tag == 'question':
if xml_child.tag == 'mentoring-question':
block.question = xml_child.text
else:
block.runtime.add_node_as_child(block, xml_child, id_generator)
Expand Down
4 changes: 2 additions & 2 deletions mentoring/components/questionnaire.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def parse_xml(cls, node, runtime, keys, id_generator):
setattr(block, name, value)

for xml_child in node:
if xml_child.tag == 'question':
if xml_child.tag == 'mentoring-question':
block.question = xml_child.text
elif xml_child.tag == 'message' and xml_child.get('type') == 'on-submit':
elif xml_child.tag == 'mentoring-message' and xml_child.get('type') == 'on-submit':
block.message = (xml_child.text or '').strip()
elif xml_child.tag is not etree.Comment:
block.runtime.add_node_as_child(block, xml_child, id_generator)
Expand Down
24 changes: 24 additions & 0 deletions mentoring/mentoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

from components import TitleBlock, SharedHeaderBlock, MentoringMessageBlock
from components.step import StepParentMixin, StepMixin
from .studio_xml_utils import studio_update_from_node

from xblockutils.resources import ResourceLoader

Expand Down Expand Up @@ -511,6 +512,9 @@ def studio_submit(self, submissions, suffix=''):
# assessment has a default of 2 max_attempts
root.attrib['max_attempts'] = '2'

assert root.tag == "mentoring"
studio_update_from_node(self, root)

if success:
response = {
'result': 'success',
Expand All @@ -533,6 +537,26 @@ def url_name_with_default(self):
else:
return self.url_name

@classmethod
def parse_xml(cls, node, runtime, keys, id_generator):
"""
To avoid collisions with e.g. the existing <html> XBlock in edx-platform,
we prefix all of the mentoring block tags with "mentoring-". However,
using that prefix in the XML is optional. This method adds that prefix
in when parsing XML in a mentoring context.
"""
PREFIX_TAGS = (
"column", "header", "answer", "quizz", "mcq", "mrq",
"message", "tip", "choice", "html", "title", "question",
"shared-header",
)
for element in node.iter():
# We have prefixed all our XBlock entry points with "mentoring-". But using the "mentoring-"
# prefix in the Studio XML editor is optional:
if element.tag in PREFIX_TAGS:
element.tag = "mentoring-{}".format(element.tag)
return super(MentoringBlock, cls).parse_xml(node, runtime, keys, id_generator)

@staticmethod
def workbench_scenarios():
"""
Expand Down
88 changes: 88 additions & 0 deletions mentoring/studio_xml_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
The mentoring XBlock is currently designed to be edited using XML.

Until it is re-written to allow creating children with a nice GUI in Studio,
this file contains a hack that allows us to parse the XML and create blocks in
Studio from the parsed XML.

It works by parsing the XML and creating XBlocks in a temporary runtime
environment, so that the blocks' fields can be read and copied into Studio.
"""
from xblock.fields import Scope
from xblock.runtime import Runtime, DictKeyValueStore, KvsFieldData, MemoryIdManager, ScopeIds


class TransientRuntime(Runtime):
"""
An XBlock runtime designed to have no persistence and no ability to render views/handlers.
"""
def __init__(self):
id_manager = MemoryIdManager()
field_data = KvsFieldData(DictKeyValueStore())
super(TransientRuntime, self).__init__(
id_reader=id_manager,
id_generator=id_manager,
field_data=field_data,
)

def create_block_from_node(self, node):
"""
Parse an XML node representing an XBlock (and children), and return the XBlock.
"""
block_type = node.tag
def_id = self.id_generator.create_definition(block_type)
usage_id = self.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))
block = block_class.parse_xml(node, self, keys, self.id_generator)
block.save()
return block

def handler_url(self, *args, **kwargs):
raise RuntimeError("TransientRuntime does not support handler_url.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make these NotSupportedErrors?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kelketek I don't think there is such a thing as NotSupportedError? I can't make them NotImplementedError because the whole reason I added these four methods is to override the versions that raise NotImplementedError - otherwise this class is considered abstract and I get TypeError: Can't instantiate abstract class TransientRuntime.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bradenmacdonald Ah, yes, NotImplementedError is what I was going for. You should still replace these exceptions with that. @abstractmethod requires the method to be overwritten with something but I believe that something can raise a NotImplementedError and still meet the requirement.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh cool, you're right. I'll change them to NotImplementedError.


def local_resource_url(self, *args, **kwargs):
raise RuntimeError("TransientRuntime does not support local_resource_url.")

def publish(self, *args, **kwargs):
raise RuntimeError("TransientRuntime does not support publish.")

def resource_url(self, *args, **kwargs):
raise RuntimeError("TransientRuntime does not support resource_url.")


def studio_update_from_node(block, node):
"""
Given an XBlock that is using the edX Studio runtime, replace all of block's fields and
children with the fields and children defined by the XML node 'node'.
"""

user_id = block.runtime.user_id
temp_runtime = TransientRuntime()
source_block = temp_runtime.create_block_from_node(node)

def update_from_temp_block(real_block, temp_block):
"""
Recursively copy all fields and children from temp_block to real_block.
"""
# Fields:
for field_name, field in temp_block.fields.iteritems():
if field.scope in (Scope.content, Scope.settings) and field.is_set_on(temp_block):
setattr(real_block, field_name, getattr(temp_block, field_name))
# Children:
if real_block.has_children:
real_block.children = []
for child_id in temp_block.children:
child = temp_block.runtime.get_block(child_id)
new_child = real_block.runtime.modulestore.create_item(
user_id, real_block.location.course_key, child.scope_ids.block_type
)
update_from_temp_block(new_child, child)
real_block.children.append(new_child.location)
real_block.save()
real_block.runtime.modulestore.update_item(real_block, user_id)

with block.runtime.modulestore.bulk_operations(block.location.course_key):
for child_id in block.children:
block.runtime.modulestore.delete_item(child_id, user_id)
update_from_temp_block(block, source_block)
4 changes: 2 additions & 2 deletions mentoring/templates/xml/mentoring_data_export.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<vertical_demo>
<html>
<html_demo>
<p>Below are two mentoring blocks and a grade export block that
should be able to export their data as CSV.</p>
</html>
</html_demo>
<mentoring display_name="Mentoring Block 1" mode="standard">
<title>Mentoring Block 1</title>
<html>
Expand Down
4 changes: 2 additions & 2 deletions mentoring/tests/integration/xml/data_export.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<vertical_demo>
<html>
<html_demo>
<p>Below are two mentoring blocks and a grade export block that
should be able to export their data as CSV.</p>
</html>
</html_demo>
<mentoring display_name="Mentoring Block 1" mode="standard">
<title>Mentoring Block 1</title>
<html>
Expand Down
24 changes: 12 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ def package_data(pkg, root_list):
'mentoring-dataexport = mentoring:MentoringDataExportBlock',

'mentoring-table = mentoring.components:MentoringTableBlock',
'column = mentoring.components:MentoringTableColumnBlock',
'header = mentoring.components:MentoringTableColumnHeaderBlock',
'answer = mentoring.components:AnswerBlock',
'quizz = mentoring.components:MCQBlock',
'mcq = mentoring.components:MCQBlock',
'mrq = mentoring.components:MRQBlock',
'message = mentoring.components:MentoringMessageBlock',
'tip = mentoring.components:TipBlock',
'choice = mentoring.components:ChoiceBlock',
'html = mentoring.components:HTMLBlock',
'title = mentoring.components:TitleBlock',
'shared-header = mentoring.components:SharedHeaderBlock',
'mentoring-column = mentoring.components:MentoringTableColumnBlock',
'mentoring-header = mentoring.components:MentoringTableColumnHeaderBlock',
'mentoring-answer = mentoring.components:AnswerBlock',
'mentoring-quizz = mentoring.components:MCQBlock',
'mentoring-mcq = mentoring.components:MCQBlock',
'mentoring-mrq = mentoring.components:MRQBlock',
'mentoring-message = mentoring.components:MentoringMessageBlock',
'mentoring-tip = mentoring.components:TipBlock',
'mentoring-choice = mentoring.components:ChoiceBlock',
'mentoring-html = mentoring.components:HTMLBlock',
'mentoring-title = mentoring.components:TitleBlock',
'mentoring-shared-header = mentoring.components:SharedHeaderBlock',
]

setup(
Expand Down