Commit fec6c42a by Chris Dodge

implement importing of course info sections as modules in the course

parent 14a94bac
...@@ -17,6 +17,8 @@ from xmodule.course_module import CourseDescriptor ...@@ -17,6 +17,8 @@ from xmodule.course_module import CourseDescriptor
from xmodule.mako_module import MakoDescriptorSystem from xmodule.mako_module import MakoDescriptorSystem
from xmodule.x_module import XModuleDescriptor, XMLParsingSystem from xmodule.x_module import XModuleDescriptor, XMLParsingSystem
from xmodule.html_module import HtmlDescriptor
from . import ModuleStoreBase, Location from . import ModuleStoreBase, Location
from .exceptions import ItemNotFoundError from .exceptions import ItemNotFoundError
...@@ -423,6 +425,25 @@ class XMLModuleStore(ModuleStoreBase): ...@@ -423,6 +425,25 @@ class XMLModuleStore(ModuleStoreBase):
# after we have the course descriptor. # after we have the course descriptor.
XModuleDescriptor.compute_inherited_metadata(course_descriptor) XModuleDescriptor.compute_inherited_metadata(course_descriptor)
# now import all pieces of course_info which is expected to be stored
# in <content_dir>/info or <content_dir>/info/<url_name>
if url_name:
info_path = self.data_dir / course_dir / 'info' / url_name
if not os.path.exists(info_path):
info_path = self.data_dir / course_dir / 'info'
# we have a fixed number of .html info files that we expect there
for info_filename in ['handouts', 'guest_handouts', 'updates', 'guest_updates']:
filepath = info_path / info_filename + '.html'
if os.path.exists(filepath):
with open(filepath) as info_file:
html = info_file.read()
loc = Location('i4x', course_descriptor.location.org, course_descriptor.location.course, 'course_info', info_filename)
html_module = HtmlDescriptor(system, definition={'data' : html}, **{'location' : loc})
self.modules[course_id][html_module.location] = html_module
log.debug('========> Done with course import from {0}'.format(course_dir)) log.debug('========> Done with course import from {0}'.format(course_dir))
return course_descriptor return course_descriptor
......
...@@ -323,11 +323,15 @@ class XModule(HTMLSnippet): ...@@ -323,11 +323,15 @@ class XModule(HTMLSnippet):
# cdodge: added to support dynamic substitutions of # cdodge: added to support dynamic substitutions of
# links for courseware assets (e.g. images). <link> is passed through from lxml.html parser # links for courseware assets (e.g. images). <link> is passed through from lxml.html parser
def rewrite_content_links(self, link): def rewrite_content_links(self, link):
# see if we start with our format, e.g. 'xasset:<filename>' loc = Location(self.location)
return XModule._rewrite_content_links(loc, link)
@staticmethod
def _rewrite_content_links(loc, link):
if link.startswith(XASSET_SRCREF_PREFIX): if link.startswith(XASSET_SRCREF_PREFIX):
# yes, then parse out the name # yes, then parse out the name
name = link[len(XASSET_SRCREF_PREFIX):] name = link[len(XASSET_SRCREF_PREFIX):]
loc = Location(self.location)
# resolve the reference to our internal 'filepath' which # resolve the reference to our internal 'filepath' which
content_loc = StaticContent.compute_location(loc.org, loc.course, name) content_loc = StaticContent.compute_location(loc.org, loc.course, name)
link = StaticContent.get_url_path_from_location(content_loc) link = StaticContent.get_url_path_from_location(content_loc)
...@@ -335,7 +339,6 @@ class XModule(HTMLSnippet): ...@@ -335,7 +339,6 @@ class XModule(HTMLSnippet):
return link return link
def policy_key(location): def policy_key(location):
""" """
Get the key for a location in a policy file. (Since the policy file is Get the key for a location in a policy file. (Since the policy file is
......
...@@ -3,6 +3,8 @@ from fs.errors import ResourceNotFoundError ...@@ -3,6 +3,8 @@ from fs.errors import ResourceNotFoundError
from functools import wraps from functools import wraps
import logging import logging
from lxml.html import rewrite_links
from path import path from path import path
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
...@@ -11,7 +13,9 @@ from django.http import Http404 ...@@ -11,7 +13,9 @@ from django.http import Http404
from xmodule.course_module import CourseDescriptor from xmodule.course_module import CourseDescriptor
from xmodule.modulestore import Location from xmodule.modulestore import Location
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
from xmodule.modulestore.xml import XMLModuleStore
from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.exceptions import ItemNotFoundError
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
...@@ -136,6 +140,25 @@ def get_course_about_section(course, section_key): ...@@ -136,6 +140,25 @@ def get_course_about_section(course, section_key):
raise KeyError("Invalid about key " + str(section_key)) raise KeyError("Invalid about key " + str(section_key))
def get_course_info_section_from_db(course, section_key):
loc = Location(course.location.tag, course.location.org, course.location.course, 'course_info', section_key)
html = ''
try:
item = modulestore().get_item(loc)
# return the raw HTML here which is stored as part of the definition. If we call get_html here, HTMLModule's parent
# descriptors will try to return an 'editing' rendering of the HTML
_html = item.definition['data']
try:
# apply link transforms which are defined in XModule, maybe that should actually be a static method in
# Content.py
html = rewrite_links(_html, XModule.rewrite_content_links)
except:
logging.error('error rewriting links on the following HTML content: {0}'.format(_html))
except Exception, e:
logging.exception("Could not find course_info section {0} at {1}: {2}".format(section_key, loc, str(e)))
return html
def get_course_info_section(course, section_key): def get_course_info_section(course, section_key):
""" """
...@@ -153,6 +176,9 @@ def get_course_info_section(course, section_key): ...@@ -153,6 +176,9 @@ def get_course_info_section(course, section_key):
# markup. This can change without effecting this interface when we find a # markup. This can change without effecting this interface when we find a
# good format for defining so many snippets of text/html. # good format for defining so many snippets of text/html.
if not isinstance(modulestore(), XMLModuleStore):
return get_course_info_section_from_db(course, section_key)
if section_key in ['handouts', 'guest_handouts', 'updates', 'guest_updates']: if section_key in ['handouts', 'guest_handouts', 'updates', 'guest_updates']:
try: try:
fs = course.system.resources_fs fs = course.system.resources_fs
......
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