Commit 36fda350 by Chris Dodge

do portable link rewriting on import and add test to confirm it

parent 5b5c8151
......@@ -720,6 +720,17 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase):
resp = self.client.get('http://localhost:8001/c4x/CDX/123123/asset/&images_circuits_Lab7Solution2.png')
self.assertEqual(resp.status_code, 400)
def test_rewrite_nonportable_links_on_import(self):
module_store = modulestore('direct')
content_store = contentstore()
import_from_xml(module_store, 'common/test/data/', ['toy'], static_content_store=content_store)
html_module_location = Location(['i4x', 'edX', 'toy', 'html', 'nonportable'])
html_module = module_store.get_instance('edX/toy/2012_Fall', html_module_location)
self.assertIn('/static/foo.jpg', html_module.data)
def test_delete_course(self):
"""
This test will import a course, make a draft item, and delete it. This will also assert that the
......
import re
from xmodule.contentstore.content import StaticContent
from xmodule.modulestore import Location
from xmodule.modulestore.mongo import MongoModuleStore
from xmodule.modulestore.inheritance import own_metadata
from static_replace import _url_replace_regex
import logging
def convert_to_portable_links(source_course_id, text):
"""
Does a regex replace on non-portable links:
/c4x/<org>/<course>/asset/<name> -> /static/<name>
/jump_to/i4x://<org>/<course>/<category>/<name> -> /jump_to_id/<id>
"""
def portable_asset_link_subtitution(match):
quote = match.group('quote')
rest = match.group('rest')
return "".join([quote, '/static/'+rest, quote])
org, course, run = source_course_id.split("/")
course_location = Location(['i4x', org, course, 'course', run])
c4x_link_base = '{0}/'.format(StaticContent.get_base_url_path_for_course_assets(course_location))
text = re.sub(_url_replace_regex(c4x_link_base), portable_asset_link_subtitution, text)
return text
def _clone_modules(modulestore, modules, dest_location):
for module in modules:
original_loc = Location(module.location)
......
......@@ -5,6 +5,7 @@
<html url_name="secret:toylab"/>
<html url_name="toyjumpto"/>
<html url_name="toyhtml"/>
<html url_name="nonportable"/>
<video url_name="Video_Resources" youtube_id_1_0="1bK-WdDi6Qw" display_name="Video Resources"/>
</videosequence>
<video url_name="Welcome" youtube_id_1_0="p2Q6BrNhdh8" display_name="Welcome"/>
......
<a href="/c4x/edX/toy/asset/foo.jpg">link</a>
<html filename="nonportable.html"/>
\ No newline at end of file
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