Commit 7aa44189 by chrisndodge

Merge pull request #723 from edx/fix/cdodge/bad-links-can-cause-exception-on-rewrite

Fix/cdodge/bad links can cause exception on rewrite
parents aa49ef47 033f922e
...@@ -119,7 +119,15 @@ def replace_static_urls(text, data_directory, course_id=None): ...@@ -119,7 +119,15 @@ def replace_static_urls(text, data_directory, course_id=None):
elif course_id and modulestore().get_modulestore_type(course_id) != XML_MODULESTORE_TYPE: elif course_id and modulestore().get_modulestore_type(course_id) != XML_MODULESTORE_TYPE:
# first look in the static file pipeline and see if we are trying to reference # first look in the static file pipeline and see if we are trying to reference
# a piece of static content which is in the mitx repo (e.g. JS associated with an xmodule) # a piece of static content which is in the mitx repo (e.g. JS associated with an xmodule)
if staticfiles_storage.exists(rest):
exists_in_staticfiles_storage = False
try:
exists_in_staticfiles_storage = staticfiles_storage.exists(rest)
except Exception as err:
log.warning("staticfiles_storage couldn't find path {0}: {1}".format(
rest, str(err)))
if exists_in_staticfiles_storage:
url = staticfiles_storage.url(rest) url = staticfiles_storage.url(rest)
else: else:
# if not, then assume it's courseware specific content and then look in the # if not, then assume it's courseware specific content and then look in the
...@@ -142,6 +150,7 @@ def replace_static_urls(text, data_directory, course_id=None): ...@@ -142,6 +150,7 @@ def replace_static_urls(text, data_directory, course_id=None):
return "".join([quote, url, quote]) return "".join([quote, url, quote])
return re.sub( return re.sub(
_url_replace_regex('/static/(?!{data_dir})'.format(data_dir=data_directory)), _url_replace_regex('/static/(?!{data_dir})'.format(data_dir=data_directory)),
replace_static_url, replace_static_url,
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<html url_name="toyhtml"/> <html url_name="toyhtml"/>
<html url_name="nonportable"/> <html url_name="nonportable"/>
<html url_name="nonportable_link"/> <html url_name="nonportable_link"/>
<html url_name="badlink"/>
<video url_name="Video_Resources" youtube_id_1_0="1bK-WdDi6Qw" display_name="Video Resources"/> <video url_name="Video_Resources" youtube_id_1_0="1bK-WdDi6Qw" display_name="Video Resources"/>
</videosequence> </videosequence>
<video url_name="Welcome" youtube_id_1_0="p2Q6BrNhdh8" display_name="Welcome"/> <video url_name="Welcome" youtube_id_1_0="p2Q6BrNhdh8" display_name="Welcome"/>
......
<img src="/static//file.jpg" />
<html filename="badlink.html"/>
\ No newline at end of file
...@@ -280,10 +280,11 @@ class TestHtmlModifiers(ModuleStoreTestCase): ...@@ -280,10 +280,11 @@ class TestHtmlModifiers(ModuleStoreTestCase):
self.course = CourseFactory.create() self.course = CourseFactory.create()
self.content_string = '<p>This is the content<p>' self.content_string = '<p>This is the content<p>'
self.rewrite_link = '<a href="/static/foo/content">Test rewrite</a>' self.rewrite_link = '<a href="/static/foo/content">Test rewrite</a>'
self.rewrite_bad_link = '<img src="/static//file.jpg" />'
self.course_link = '<a href="/course/bar/content">Test course rewrite</a>' self.course_link = '<a href="/course/bar/content">Test course rewrite</a>'
self.descriptor = ItemFactory.create( self.descriptor = ItemFactory.create(
category='html', category='html',
data=self.content_string + self.rewrite_link + self.course_link data=self.content_string + self.rewrite_link + self.rewrite_bad_link + self.course_link
) )
self.location = self.descriptor.location self.location = self.descriptor.location
self.model_data_cache = ModelDataCache.cache_for_descriptor_descendents( self.model_data_cache = ModelDataCache.cache_for_descriptor_descendents(
...@@ -336,6 +337,24 @@ class TestHtmlModifiers(ModuleStoreTestCase): ...@@ -336,6 +337,24 @@ class TestHtmlModifiers(ModuleStoreTestCase):
result_fragment.content result_fragment.content
) )
def test_static_badlink_rewrite(self):
module = render.get_module(
self.user,
self.request,
self.location,
self.model_data_cache,
self.course.id,
)
result_fragment = module.runtime.render(module, None, 'student_view')
self.assertIn(
'/c4x/{org}/{course}/asset/_file.jpg'.format(
org=self.course.location.org,
course=self.course.location.course,
),
result_fragment.content
)
def test_course_link_rewrite(self): def test_course_link_rewrite(self):
module = render.get_module( module = render.get_module(
self.user, self.user,
......
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