From fec6c42adc4ddbf8261ec01591e3c9557ae41a53 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Wed, 24 Oct 2012 20:55:04 -0400 Subject: [PATCH] implement importing of course info sections as modules in the course --- common/lib/xmodule/xmodule/modulestore/xml.py | 21 +++++++++++++++ common/lib/xmodule/xmodule/x_module.py | 9 ++++--- lms/djangoapps/courseware/courses.py | 26 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/common/lib/xmodule/xmodule/modulestore/xml.py b/common/lib/xmodule/xmodule/modulestore/xml.py index 7990bafe7d..0206eed242 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml.py +++ b/common/lib/xmodule/xmodule/modulestore/xml.py @@ -17,6 +17,8 @@ from xmodule.course_module import CourseDescriptor from xmodule.mako_module import MakoDescriptorSystem from xmodule.x_module import XModuleDescriptor, XMLParsingSystem +from xmodule.html_module import HtmlDescriptor + from . import ModuleStoreBase, Location from .exceptions import ItemNotFoundError @@ -423,6 +425,25 @@ class XMLModuleStore(ModuleStoreBase): # after we have the course descriptor. XModuleDescriptor.compute_inherited_metadata(course_descriptor) + # now import all pieces of course_info which is expected to be stored + # in /info or /info/ + 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)) return course_descriptor diff --git a/common/lib/xmodule/xmodule/x_module.py b/common/lib/xmodule/xmodule/x_module.py index a53f2db7da..99468946d7 100644 --- a/common/lib/xmodule/xmodule/x_module.py +++ b/common/lib/xmodule/xmodule/x_module.py @@ -323,11 +323,15 @@ class XModule(HTMLSnippet): # cdodge: added to support dynamic substitutions of # links for courseware assets (e.g. images). is passed through from lxml.html parser def rewrite_content_links(self, link): - # see if we start with our format, e.g. 'xasset:' + 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):] - loc = Location(self.location) # 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) @@ -335,7 +339,6 @@ class XModule(HTMLSnippet): return link - def policy_key(location): """ Get the key for a location in a policy file. (Since the policy file is diff --git a/lms/djangoapps/courseware/courses.py b/lms/djangoapps/courseware/courses.py index 65a1eee25b..338fcfa42a 100644 --- a/lms/djangoapps/courseware/courses.py +++ b/lms/djangoapps/courseware/courses.py @@ -3,6 +3,8 @@ from fs.errors import ResourceNotFoundError from functools import wraps import logging +from lxml.html import rewrite_links + from path import path from django.conf import settings from django.core.urlresolvers import reverse @@ -11,7 +13,9 @@ from django.http import Http404 from xmodule.course_module import CourseDescriptor from xmodule.modulestore import Location from xmodule.modulestore.django import modulestore +from xmodule.modulestore.xml import XMLModuleStore from xmodule.modulestore.exceptions import ItemNotFoundError +from xmodule.x_module import XModule from static_replace import replace_urls, try_staticfiles_lookup from courseware.access import has_access import branding @@ -136,6 +140,25 @@ def get_course_about_section(course, 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): """ @@ -153,6 +176,9 @@ def get_course_info_section(course, section_key): # markup. This can change without effecting this interface when we find a # 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']: try: fs = course.system.resources_fs