Skip to content

Commit

Permalink
Merge pull request #24 from open-craft/josuebc/BB-1126
Browse files Browse the repository at this point in the history
BB-1126 Map block ids into correct course ids when importing XML
  • Loading branch information
xirdneh authored Apr 18, 2019
2 parents 53c6b6e + 281a84b commit 5486a00
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
39 changes: 39 additions & 0 deletions eoc_journal/eoc_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from xblock.fragment import Fragment
from xblockutils.resources import ResourceLoader
from xblockutils.studio_editable import StudioEditableXBlockMixin
from opaque_keys.edx.keys import UsageKey, CourseKey

from .api_client import ApiClient
from .completion_api import CompletionApiClient
Expand Down Expand Up @@ -530,3 +531,41 @@ def _expand_static_url(self, url, absolute=False):
url = self._make_url_absolute(url)

return url

@classmethod
def parse_xml(cls, node, runtime, keys, id_generator):
"""
Use `node` to construct a new block.
Arguments:
node (:class:`~xml.etree.ElementTree.Element`): The xml node to parse into an xblock.
runtime (:class:`.Runtime`): The runtime to use while parsing.
keys (:class:`.ScopeIds`): The keys identifying where this block
will store its data.
id_generator (:class:`.IdGenerator`): An object that will allow the
runtime to generate correct definition and usage ids for
children of this block.
Notes:
This method is overwritten from :class:`~xblock.mixins.XmlSerizalizationMixin`.
https://github.com/edx/XBlock/blob/master/xblock/mixins.py
"""
block = super(EOCJournalXBlock, cls).parse_xml(node, runtime, keys, id_generator)
course_id = getattr(id_generator, 'target_course_id', '')

if not course_id:
return block

course_id = unicode(normalize_id(course_id))
course_key = CourseKey.from_string(course_id)

transformed_block_ids = []
for selected in block.selected_pb_answer_blocks:
usage_key = UsageKey.from_string(selected)
if usage_key.course_key == course_key:
transformed_block_ids.append(selected)
else:
mapped = usage_key.map_into_course(course_key)
transformed_block_ids.append(unicode(mapped))

block.selected_pb_answer_blocks = transformed_block_ids
return block
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ six
web-fragments
webob
XBlock
edx-opaque-keys==0.4.4
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def package_data(pkg, roots):

setup(
name='xblock-eoc-journal',
version='0.2',
version='0.3',
description='End of Course Journal XBlock',
packages=[
'eoc_journal',
Expand Down
Empty file added tests/unit/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions tests/unit/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Test EOC Journal Block import.
"""

import unittest
from mock import Mock, patch
from django.test import TestCase
from eoc_journal.eoc_journal import EOCJournalXBlock


class TestEOCJournalImport(TestCase):
"""
Test End of Course Journal Import
"""

@patch('eoc_journal.eoc_journal.XBlock.parse_xml',
return_value=Mock(autospec=EOCJournalXBlock)
)
def test_parse_xml(self, mock_xblock_parse_xml):
mock_xblock_parse_xml().selected_pb_answer_blocks = [
'i4x://Org/Course/pb-answer/b86edf60454b47dbb8f2e1b4e2d48d6a',
'i4x://Org/Course/pb-answer/6f070c350e39429cbccfd3185a33621c',
'i4x://Org/Course/pb-answer/a0b04a13d3074229b6be33fbc31de233',
'i4x://Org/Course/pb-answer/927c5b8cd051475e937b8c1091a9feaf',
]
mock_node = Mock()
mock_runtime = Mock()
mock_keys = Mock()
mock_id_generator = Mock(target_course_id='Org/CourseToImport/2014')
block = EOCJournalXBlock.parse_xml(mock_node, mock_runtime, mock_keys, mock_id_generator)
for block_id in block.selected_pb_answer_blocks:
self.assertTrue(block_id.startswith('i4x://Org/CourseToImport'))

0 comments on commit 5486a00

Please sign in to comment.