Commit 743f2b56 by Chris Dodge

make course about view methods render the about content as a module, so we get…

make course about view methods render the about content as a module, so we get all the url rewriting goodness. Also, since we're now handling the url re-writing via the module get_html pipelines, we can remove the link rewriting inside the xmodules itself - which is good because there's a wierd bug in lxml.html rewriting
parent ded19f76
...@@ -59,6 +59,7 @@ def replace_static_urls(get_html, prefix, course_namespace=None): ...@@ -59,6 +59,7 @@ def replace_static_urls(get_html, prefix, course_namespace=None):
@wraps(get_html) @wraps(get_html)
def _get_html(): def _get_html():
logging.debug('in replace_static_urls')
return replace_urls(get_html(), staticfiles_prefix=prefix, course_namespace = course_namespace) return replace_urls(get_html(), staticfiles_prefix=prefix, course_namespace = course_namespace)
return _get_html return _get_html
......
...@@ -10,7 +10,6 @@ import sys ...@@ -10,7 +10,6 @@ import sys
from datetime import timedelta from datetime import timedelta
from lxml import etree from lxml import etree
from lxml.html import rewrite_links
from pkg_resources import resource_string from pkg_resources import resource_string
from capa.capa_problem import LoncapaProblem from capa.capa_problem import LoncapaProblem
...@@ -342,17 +341,6 @@ class CapaModule(XModule): ...@@ -342,17 +341,6 @@ class CapaModule(XModule):
html = '<div id="problem_{id}" class="problem" data-url="{ajax_url}">'.format( html = '<div id="problem_{id}" class="problem" data-url="{ajax_url}">'.format(
id=self.location.html_id(), ajax_url=self.system.ajax_url) + html + "</div>" id=self.location.html_id(), ajax_url=self.system.ajax_url) + html + "</div>"
# cdodge: OK, we have to do two rounds of url reference subsitutions
# one which uses the 'asset library' that is served by the contentstore and the
# more global /static/ filesystem based static content.
# NOTE: rewrite_content_links is defined in XModule
# This is a bit unfortunate and I'm sure we'll try to considate this into
# a one step process.
try:
html = rewrite_links(html, self.rewrite_content_links)
except:
logging.error('error rewriting links in {0}'.format(html))
# now do the substitutions which are filesystem based, e.g. '/static/' prefixes # now do the substitutions which are filesystem based, e.g. '/static/' prefixes
return self.system.replace_urls(html, self.metadata['data_dir']) return self.system.replace_urls(html, self.metadata['data_dir'])
......
...@@ -4,7 +4,6 @@ import logging ...@@ -4,7 +4,6 @@ import logging
import os import os
import sys import sys
from lxml import etree from lxml import etree
from lxml.html import rewrite_links
from path import path from path import path
from .x_module import XModule from .x_module import XModule
...@@ -29,14 +28,7 @@ class HtmlModule(XModule): ...@@ -29,14 +28,7 @@ class HtmlModule(XModule):
js_module_name = "HTMLModule" js_module_name = "HTMLModule"
def get_html(self): def get_html(self):
# cdodge: perform link substitutions for any references to course static content (e.g. images) return self.html
_html = self.html
try:
_html = rewrite_links(_html, self.rewrite_content_links)
except:
logging.error('error rewriting links on the following HTML content: {0}'.format(_html))
return _html
def __init__(self, system, location, definition, descriptor, def __init__(self, system, location, definition, descriptor,
instance_state=None, shared_state=None, **kwargs): instance_state=None, shared_state=None, **kwargs):
......
...@@ -30,7 +30,7 @@ def import_static_content(modules, data_dir, static_content_store, target_locati ...@@ -30,7 +30,7 @@ def import_static_content(modules, data_dir, static_content_store, target_locati
# now import all static assets # now import all static assets
static_dir = '{0}/static/'.format(course_data_dir) static_dir = '{0}/static/'.format(data_dir / course_data_dir)
for dirname, dirnames, filenames in os.walk(static_dir): for dirname, dirnames, filenames in os.walk(static_dir):
for filename in filenames: for filename in filenames:
...@@ -185,7 +185,7 @@ def import_from_xml(store, data_dir, course_dirs=None, ...@@ -185,7 +185,7 @@ def import_from_xml(store, data_dir, course_dirs=None,
# it as a StaticContent asset # it as a StaticContent asset
try: try:
remap_dict = {} remap_dict = {}
# use the rewrite_links as a utility means to enumerate through all links # use the rewrite_links as a utility means to enumerate through all links
# in the module data. We use that to load that reference into our asset store # in the module data. We use that to load that reference into our asset store
# IMPORTANT: There appears to be a bug in lxml.rewrite_link which makes us not be able to # IMPORTANT: There appears to be a bug in lxml.rewrite_link which makes us not be able to
......
...@@ -320,24 +320,6 @@ class XModule(HTMLSnippet): ...@@ -320,24 +320,6 @@ class XModule(HTMLSnippet):
get is a dictionary-like object ''' get is a dictionary-like object '''
return "" return ""
# cdodge: added to support dynamic substitutions of
# links for courseware assets (e.g. images). <link> is passed through from lxml.html parser
def rewrite_content_links(self, link):
loc = Location(self.location)
return XModule._rewrite_content_links(loc, link)
@staticmethod
def _rewrite_content_links(loc, link):
if link.startswith(XASSET_SRCREF_PREFIX):
# yes, then parse out the name
name = link[len(XASSET_SRCREF_PREFIX):]
# resolve the reference to our internal 'filepath' which
content_loc = StaticContent.compute_location(loc.org, loc.course, name)
link = StaticContent.get_url_path_from_location(content_loc)
return link
def policy_key(location): def policy_key(location):
""" """
......
...@@ -2,6 +2,7 @@ from collections import defaultdict ...@@ -2,6 +2,7 @@ from collections import defaultdict
from fs.errors import ResourceNotFoundError from fs.errors import ResourceNotFoundError
from functools import wraps from functools import wraps
import logging import logging
import inspect
from lxml.html import rewrite_links from lxml.html import rewrite_links
...@@ -21,11 +22,24 @@ from xmodule.x_module import XModule ...@@ -21,11 +22,24 @@ from xmodule.x_module import XModule
from static_replace import replace_urls, try_staticfiles_lookup from static_replace import replace_urls, try_staticfiles_lookup
from courseware.access import has_access from courseware.access import has_access
import branding import branding
from courseware.models import StudentModuleCache
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def get_request_for_thread():
"""Walk up the stack, return the nearest first argument named "request"."""
frame = None
try:
for f in inspect.stack()[1:]:
frame = f[0]
code = frame.f_code
if code.co_varnames[:1] == ("request",):
return frame.f_locals["request"]
elif code.co_varnames[:2] == ("self", "request",):
return frame.f_locals["request"]
finally:
del frame
def get_course_by_id(course_id): def get_course_by_id(course_id):
""" """
...@@ -129,10 +143,23 @@ def get_course_about_section(course, section_key): ...@@ -129,10 +143,23 @@ def get_course_about_section(course, section_key):
'effort', 'end_date', 'prerequisites', 'ocw_links']: 'effort', 'end_date', 'prerequisites', 'ocw_links']:
try: try:
request = get_request_for_thread()
student_module_cache = StudentModuleCache.cache_for_descriptor_descendents(
course.id, request.user, course, depth=2)
loc = course.location._replace(category='about', name=section_key) loc = course.location._replace(category='about', name=section_key)
course_module = get_module(request.user, request, loc, student_module_cache, course.id)
html = ''
if course_module is not None:
html = course_module.get_html()
item = modulestore().get_instance(course.id, loc) item = modulestore().get_instance(course.id, loc)
return item.definition['data'] return html
except ItemNotFoundError: except ItemNotFoundError:
log.warning("Missing about section {key} in course {url}".format( log.warning("Missing about section {key} in course {url}".format(
...@@ -161,6 +188,7 @@ def get_course_info_section(request, cache, course, section_key): ...@@ -161,6 +188,7 @@ def get_course_info_section(request, cache, course, section_key):
- guest_updates - guest_updates
""" """
loc = Location(course.location.tag, course.location.org, course.location.course, 'course_info', section_key) loc = Location(course.location.tag, course.location.org, course.location.course, 'course_info', section_key)
course_module = get_module(request.user, request, loc, cache, course.id) course_module = get_module(request.user, request, loc, cache, course.id)
......
...@@ -259,6 +259,8 @@ def _get_module(user, request, location, student_module_cache, course_id, positi ...@@ -259,6 +259,8 @@ def _get_module(user, request, location, student_module_cache, course_id, positi
module.metadata['data_dir'] if 'data_dir' in module.metadata else '', module.metadata['data_dir'] if 'data_dir' in module.metadata else '',
course_namespace = module.location._replace(category=None, name=None)) course_namespace = module.location._replace(category=None, name=None))
logging.debug('in get_module')
# 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
# hierarchy of this course # hierarchy of this course
module.get_html = replace_course_urls(module.get_html, course_id) module.get_html = replace_course_urls(module.get_html, course_id)
......
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