This repository was archived by the owner on Mar 18, 2026. It is now read-only.
Repository navigation
Fix so real children work in studio and LMS (OC-513) #5
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
833e22e
A workaround that allows us to use mentoring in Studio
bradenmacdonald 24f648f
Prefix mentoring tag names to avoid collisions with other XBlocks
bradenmacdonald ac4c9c5
Display friendly message on newly-created blocks rather than empty form.
bradenmacdonald 23eb2f1
Raise NotImplementedError on methods that are not supported.
bradenmacdonald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.") | ||
|
|
||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 themNotImplementedErrorbecause the whole reason I added these four methods is to override the versions that raiseNotImplementedError- otherwise this class is considered abstract and I getTypeError: Can't instantiate abstract class TransientRuntime.There was a problem hiding this comment.
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.
@abstractmethodrequires the method to be overwritten with something but I believe that something can raise a NotImplementedError and still meet the requirement.There was a problem hiding this comment.
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.