Commit a5bb971c by ichuang

add static_asset_path metadata to course, and honor its use in link rewriting

parent 1b111b1d
...@@ -90,7 +90,7 @@ def replace_course_urls(text, course_id): ...@@ -90,7 +90,7 @@ def replace_course_urls(text, course_id):
return re.sub(_url_replace_regex('/course/'), replace_course_url, text) return re.sub(_url_replace_regex('/course/'), replace_course_url, text)
def replace_static_urls(text, data_directory, course_namespace=None): def replace_static_urls(text, data_directory, course_namespace=None, static_asset_path=''):
""" """
Replace /static/$stuff urls either with their correct url as generated by collectstatic, 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 (/static/$md5_hashed_stuff) or by the course-specific content static url
...@@ -100,6 +100,7 @@ def replace_static_urls(text, data_directory, course_namespace=None): ...@@ -100,6 +100,7 @@ def replace_static_urls(text, data_directory, course_namespace=None):
text: The source text to do the substitution in text: The source text to do the substitution in
data_directory: The directory in which course data is stored data_directory: The directory in which course data is stored
course_namespace: The course identifier used to distinguish static content for this course in studio course_namespace: 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
""" """
def replace_static_url(match): def replace_static_url(match):
...@@ -116,7 +117,7 @@ def replace_static_urls(text, data_directory, course_namespace=None): ...@@ -116,7 +117,7 @@ def replace_static_urls(text, data_directory, course_namespace=None):
if settings.DEBUG and finders.find(rest, True): if settings.DEBUG and finders.find(rest, True):
return original return original
# if we're running with a MongoBacked store course_namespace is not None, then use studio style urls # if we're running with a MongoBacked store course_namespace is not None, then use studio style urls
elif course_namespace is not None and not isinstance(modulestore(), XMLModuleStore): elif (not static_asset_path) and course_namespace is not None and not isinstance(modulestore(), XMLModuleStore):
# 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): if staticfiles_storage.exists(rest):
...@@ -127,7 +128,7 @@ def replace_static_urls(text, data_directory, course_namespace=None): ...@@ -127,7 +128,7 @@ def replace_static_urls(text, data_directory, course_namespace=None):
url = StaticContent.convert_legacy_static_url(rest, course_namespace) url = StaticContent.convert_legacy_static_url(rest, course_namespace)
# Otherwise, look the file up in staticfiles_storage, and append the data directory if needed # Otherwise, look the file up in staticfiles_storage, and append the data directory if needed
else: else:
course_path = "/".join((data_directory, rest)) course_path = "/".join((static_asset_path or data_directory, rest))
try: try:
if staticfiles_storage.exists(rest): if staticfiles_storage.exists(rest):
...@@ -143,7 +144,7 @@ def replace_static_urls(text, data_directory, course_namespace=None): ...@@ -143,7 +144,7 @@ def replace_static_urls(text, data_directory, course_namespace=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=static_asset_path or data_directory)),
replace_static_url, replace_static_url,
text text
) )
...@@ -76,7 +76,7 @@ def replace_course_urls(get_html, course_id): ...@@ -76,7 +76,7 @@ def replace_course_urls(get_html, course_id):
return _get_html return _get_html
def replace_static_urls(get_html, data_dir, course_namespace=None): def replace_static_urls(get_html, data_dir, course_namespace=None, static_asset_path=''):
""" """
Updates the supplied module with a new get_html function that wraps Updates the supplied module with a new get_html function that wraps
the old get_html function and substitutes urls of the form /static/... the old get_html function and substitutes urls of the form /static/...
...@@ -85,7 +85,7 @@ def replace_static_urls(get_html, data_dir, course_namespace=None): ...@@ -85,7 +85,7 @@ def replace_static_urls(get_html, data_dir, course_namespace=None):
@wraps(get_html) @wraps(get_html)
def _get_html(): def _get_html():
return static_replace.replace_static_urls(get_html(), data_dir, course_namespace) return static_replace.replace_static_urls(get_html(), data_dir, course_namespace, static_asset_path=static_asset_path)
return _get_html return _get_html
......
...@@ -9,7 +9,8 @@ INHERITABLE_METADATA = ( ...@@ -9,7 +9,8 @@ INHERITABLE_METADATA = (
# intended to be set per-course, but can be overridden in for specific # intended to be set per-course, but can be overridden in for specific
# elements. Can be a float. # elements. Can be a float.
'days_early_for_beta', 'days_early_for_beta',
'giturl' # for git edit link 'giturl', # for git edit link
'static_asset_path', # for static assets placed outside xcontent contentstore
) )
......
...@@ -82,8 +82,8 @@ def get_opt_course_with_access(user, course_id, action): ...@@ -82,8 +82,8 @@ def get_opt_course_with_access(user, course_id, action):
def course_image_url(course): def course_image_url(course):
"""Try to look up the image url for the course. If it's not found, """Try to look up the image url for the course. If it's not found,
log an error and return the dead link""" log an error and return the dead link"""
if isinstance(modulestore(), XMLModuleStore): if course.lms.static_asset_path or isinstance(modulestore(), XMLModuleStore):
return '/static/' + course.data_dir + "/images/course_image.jpg" return '/static/' + (course.lms.static_asset_path or getattr(course, 'data_dir', '')) + "/images/course_image.jpg"
else: else:
loc = course.location._replace(tag='c4x', category='asset', name='images_course_image.jpg') loc = course.location._replace(tag='c4x', category='asset', name='images_course_image.jpg')
path = StaticContent.get_url_path_from_location(loc) path = StaticContent.get_url_path_from_location(loc)
......
...@@ -348,6 +348,7 @@ def get_module_for_descriptor_internal(user, descriptor, model_data_cache, cours ...@@ -348,6 +348,7 @@ def get_module_for_descriptor_internal(user, descriptor, model_data_cache, cours
static_replace.replace_static_urls, static_replace.replace_static_urls,
data_directory=getattr(descriptor, 'data_dir', None), data_directory=getattr(descriptor, 'data_dir', None),
course_namespace=descriptor.location._replace(category=None, name=None), course_namespace=descriptor.location._replace(category=None, name=None),
static_asset_path=descriptor.lms.static_asset_path,
), ),
replace_course_urls=partial( replace_course_urls=partial(
static_replace.replace_course_urls, static_replace.replace_course_urls,
...@@ -405,7 +406,8 @@ def get_module_for_descriptor_internal(user, descriptor, model_data_cache, cours ...@@ -405,7 +406,8 @@ def get_module_for_descriptor_internal(user, descriptor, model_data_cache, cours
module.get_html = replace_static_urls( module.get_html = replace_static_urls(
_get_html, _get_html,
getattr(descriptor, 'data_dir', None), getattr(descriptor, 'data_dir', None),
course_namespace=module.location._replace(category=None, name=None) course_namespace=module.location._replace(category=None, name=None),
static_asset_path=descriptor.lms.static_asset_path
) )
# Allow URLs of the form '/course/' refer to the root of multicourse directory # Allow URLs of the form '/course/' refer to the root of multicourse directory
......
...@@ -56,3 +56,4 @@ class LmsNamespace(Namespace): ...@@ -56,3 +56,4 @@ class LmsNamespace(Namespace):
default=None, default=None,
scope=Scope.settings scope=Scope.settings
) )
static_asset_path = String(help="Path to use for static assets - overrides Studio c4x://", scope=Scope.settings, default='')
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