Merge branch 'release'
Conflicts: lms/djangoapps/mobile_api/course_info/tests.py
This commit is contained in:
@@ -92,7 +92,50 @@ def replace_course_urls(text, course_key):
|
||||
return re.sub(_url_replace_regex('/course/'), replace_course_url, text)
|
||||
|
||||
|
||||
def replace_static_urls(text, data_directory, course_id=None, static_asset_path=''):
|
||||
def process_static_urls(text, replacement_function, data_dir=None):
|
||||
"""
|
||||
Run an arbitrary replacement function on any urls matching the static file
|
||||
directory
|
||||
"""
|
||||
def wrap_part_extraction(match):
|
||||
"""
|
||||
Unwraps a match group for the captures specified in _url_replace_regex
|
||||
and forward them on as function arguments
|
||||
"""
|
||||
original = match.group(0)
|
||||
prefix = match.group('prefix')
|
||||
quote = match.group('quote')
|
||||
rest = match.group('rest')
|
||||
return replacement_function(original, prefix, quote, rest)
|
||||
|
||||
return re.sub(
|
||||
_url_replace_regex(u'(?:{static_url}|/static/)(?!{data_dir})'.format(
|
||||
static_url=settings.STATIC_URL,
|
||||
data_dir=data_dir
|
||||
)),
|
||||
wrap_part_extraction,
|
||||
text
|
||||
)
|
||||
|
||||
|
||||
def make_static_urls_absolute(request, html):
|
||||
"""
|
||||
Converts relative URLs referencing static assets to absolute URLs
|
||||
"""
|
||||
def replace(__, prefix, quote, rest):
|
||||
"""
|
||||
Function to actually do a single relative -> absolute url replacement
|
||||
"""
|
||||
processed = request.build_absolute_uri(prefix + rest)
|
||||
return quote + processed + quote
|
||||
|
||||
return process_static_urls(
|
||||
html,
|
||||
replace
|
||||
)
|
||||
|
||||
|
||||
def replace_static_urls(text, data_directory=None, course_id=None, static_asset_path=''):
|
||||
"""
|
||||
Replace /static/$stuff urls either with their correct url as generated by collectstatic,
|
||||
(/static/$md5_hashed_stuff) or by the course-specific content static url
|
||||
@@ -105,11 +148,10 @@ def replace_static_urls(text, data_directory, course_id=None, static_asset_path=
|
||||
static_asset_path: Path for static assets, which overrides data_directory and course_namespace, if nonempty
|
||||
"""
|
||||
|
||||
def replace_static_url(match):
|
||||
original = match.group(0)
|
||||
prefix = match.group('prefix')
|
||||
quote = match.group('quote')
|
||||
rest = match.group('rest')
|
||||
def replace_static_url(original, prefix, quote, rest):
|
||||
"""
|
||||
Replace a single matched url.
|
||||
"""
|
||||
|
||||
# Don't mess with things that end in '?raw'
|
||||
if rest.endswith('?raw'):
|
||||
@@ -155,11 +197,4 @@ def replace_static_urls(text, data_directory, course_id=None, static_asset_path=
|
||||
|
||||
return "".join([quote, url, quote])
|
||||
|
||||
return re.sub(
|
||||
_url_replace_regex(u'(?:{static_url}|/static/)(?!{data_dir})'.format(
|
||||
static_url=settings.STATIC_URL,
|
||||
data_dir=static_asset_path or data_directory
|
||||
)),
|
||||
replace_static_url,
|
||||
text
|
||||
)
|
||||
return process_static_urls(text, replace_static_url, data_dir=static_asset_path or data_directory)
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import re
|
||||
|
||||
from nose.tools import assert_equals, assert_true, assert_false # pylint: disable=no-name-in-module
|
||||
from static_replace import (replace_static_urls, replace_course_urls,
|
||||
_url_replace_regex)
|
||||
from static_replace import (
|
||||
replace_static_urls,
|
||||
replace_course_urls,
|
||||
_url_replace_regex,
|
||||
process_static_urls,
|
||||
make_static_urls_absolute
|
||||
)
|
||||
from mock import patch, Mock
|
||||
|
||||
from opaque_keys.edx.locations import SlashSeparatedCourseKey
|
||||
@@ -27,6 +32,37 @@ def test_multi_replace():
|
||||
)
|
||||
|
||||
|
||||
def test_process_url():
|
||||
def processor(__, prefix, quote, rest): # pylint: disable=missing-docstring
|
||||
return quote + 'test' + prefix + rest + quote
|
||||
|
||||
assert_equals('"test/static/file.png"', process_static_urls(STATIC_SOURCE, processor))
|
||||
|
||||
|
||||
def test_process_url_data_dir_exists():
|
||||
base = '"/static/{data_dir}/file.png"'.format(data_dir=DATA_DIRECTORY)
|
||||
|
||||
def processor(original, prefix, quote, rest): # pylint: disable=unused-argument,missing-docstring
|
||||
return quote + 'test' + rest + quote
|
||||
|
||||
assert_equals(base, process_static_urls(base, processor, data_dir=DATA_DIRECTORY))
|
||||
|
||||
|
||||
def test_process_url_no_match():
|
||||
|
||||
def processor(__, prefix, quote, rest): # pylint: disable=missing-docstring
|
||||
return quote + 'test' + prefix + rest + quote
|
||||
|
||||
assert_equals('"test/static/file.png"', process_static_urls(STATIC_SOURCE, processor))
|
||||
|
||||
|
||||
@patch('django.http.HttpRequest')
|
||||
def test_static_urls(mock_request):
|
||||
mock_request.build_absolute_uri = lambda url: 'http://' + url
|
||||
result = make_static_urls_absolute(mock_request, STATIC_SOURCE)
|
||||
assert_equals(result, '\"http:///static/file.png\"')
|
||||
|
||||
|
||||
@patch('static_replace.staticfiles_storage')
|
||||
def test_storage_url_exists(mock_storage):
|
||||
mock_storage.exists.return_value = True
|
||||
|
||||
Reference in New Issue
Block a user