From e2358af6a970754147f282cb0ac7abea84bbee7c Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Fri, 9 Aug 2013 01:03:16 -0400 Subject: [PATCH] add ability to rewrite links on the old /courses////jump_to/i4x:/// .. intracourseware linking to the new portable /jump_to_id/ format --- .../contentstore/tests/test_contentstore.py | 7 ++- .../xmodule/modulestore/store_utilities.py | 45 ++++++++++++++++++- common/test/data/toy/course/2012_Fall.xml | 1 + 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_contentstore.py b/cms/djangoapps/contentstore/tests/test_contentstore.py index e33fb27cde..a5bbbc6d80 100644 --- a/cms/djangoapps/contentstore/tests/test_contentstore.py +++ b/cms/djangoapps/contentstore/tests/test_contentstore.py @@ -777,11 +777,16 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): import_from_xml(module_store, 'common/test/data/', ['toy'], static_content_store=content_store) + # first check a static asset link 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) + # then check a intra courseware link + html_module_location = Location(['i4x', 'edX', 'toy', 'html', 'nonportable_link']) + html_module = module_store.get_instance('edX/toy/2012_Fall', html_module_location) + self.assertIn('/jump_to_id/nonportable_link', 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 diff --git a/common/lib/xmodule/xmodule/modulestore/store_utilities.py b/common/lib/xmodule/xmodule/modulestore/store_utilities.py index fd07d3f774..a8ace87831 100644 --- a/common/lib/xmodule/xmodule/modulestore/store_utilities.py +++ b/common/lib/xmodule/xmodule/modulestore/store_utilities.py @@ -3,11 +3,43 @@ 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 _asset_url_replace_regex(prefix): + """ + Match static urls in quotes that don't end in '?raw'. + + To anyone contemplating making this more complicated: + http://xkcd.com/1171/ + """ + return r""" + (?x) # flags=re.VERBOSE + (?P\\?['"]) # the opening quotes + (?P{prefix}) # the prefix + (?P.*?) # everything else in the url + (?P=quote) # the first matching closing quote + """.format(prefix=prefix) + + +def _jump_to_url_replace_regex(prefix): + """ + Match static urls in quotes that don't end in '?raw'. + + To anyone contemplating making this more complicated: + http://xkcd.com/1171/ + """ + return r""" + (?x) # flags=re.VERBOSE + (?P\\?['"]) # the opening quotes + (?P{prefix}) # the prefix + (?P[^/]+)/ + (?P.*?) # everything else in the url + (?P=quote) # the first matching closing quote + """.format(prefix=prefix) + + def convert_to_portable_links(source_course_id, text): """ Does a regex replace on non-portable links: @@ -20,11 +52,20 @@ def convert_to_portable_links(source_course_id, text): rest = match.group('rest') return "".join([quote, '/static/'+rest, quote]) + def portable_jump_to_link_substitution(match): + quote = match.group('quote') + rest = match.group('rest') + return "".join([quote, '/jump_to_id/'+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) + text = re.sub(_asset_url_replace_regex(c4x_link_base), portable_asset_link_subtitution, text) + + jump_to_link_base = '/courses/{org}/{course}/{run}/jump_to/i4x://{org}/{course}/'.format( + org=org, course=course, run=run) + text = re.sub(_jump_to_url_replace_regex(jump_to_link_base), portable_jump_to_link_substitution, text) return text diff --git a/common/test/data/toy/course/2012_Fall.xml b/common/test/data/toy/course/2012_Fall.xml index c2faad5727..9b14d49dcd 100644 --- a/common/test/data/toy/course/2012_Fall.xml +++ b/common/test/data/toy/course/2012_Fall.xml @@ -6,6 +6,7 @@ +