Collect static asset URLs from static_replace.replace_static_urls

Allows users to keep track of which static asset URLs were found in given text,
and what they were replaced from/to.
This commit is contained in:
Usman Khalid
2018-08-30 05:02:02 +05:00
committed by Jillian Vogel
parent 4a1caf6c03
commit ca910b6f27
2 changed files with 37 additions and 1 deletions

View File

@@ -148,7 +148,7 @@ def make_static_urls_absolute(request, html):
)
def replace_static_urls(text, data_directory=None, course_id=None, static_asset_path=''):
def replace_static_urls(text, data_directory=None, course_id=None, static_asset_path='', static_paths_out=None):
"""
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
@@ -159,19 +159,29 @@ def replace_static_urls(text, data_directory=None, course_id=None, static_asset_
data_directory: The directory in which course data is stored
course_id: The course identifier used to distinguish static content for this course in studio
static_asset_path: Path for static assets, which overrides data_directory and course_namespace, if nonempty
static_paths_out: (optional) pass an array to collect tuples for each static URI found:
* the original unmodified static URI
* the updated static URI (will match the original if unchanged)
"""
if static_paths_out is None:
static_paths_out = []
def replace_static_url(original, prefix, quote, rest):
"""
Replace a single matched url.
"""
original_uri = "".join([prefix, rest])
# Don't mess with things that end in '?raw'
if rest.endswith('?raw'):
static_paths_out.append((original_uri, original_uri))
return original
# In debug mode, if we can find the url as is,
if settings.DEBUG and finders.find(rest, True):
static_paths_out.append((original_uri, original_uri))
return original
# if we're running with a MongoBacked store course_namespace is not None, then use studio style urls
elif (not static_asset_path) and course_id:
# first look in the static file pipeline and see if we are trying to reference
@@ -213,6 +223,7 @@ def replace_static_urls(text, data_directory=None, course_id=None, static_asset_
rest, str(err)))
url = "".join([prefix, course_path])
static_paths_out.append((original_uri, url))
return "".join([quote, url, quote])
return process_static_urls(text, replace_static_url, data_dir=static_asset_path or data_directory)