From c8f261efc786bb6707b005c07928bee49b6075c1 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Fri, 9 Mar 2012 20:20:02 -0500 Subject: [PATCH] Work-around for not finding XML for sections in section files. Note: Requires data push --- courseware/content_parser.py | 31 ++++++++++++++++++++++++------- courseware/module_render.py | 2 +- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/courseware/content_parser.py b/courseware/content_parser.py index 30b8c1753b..35ee7b5018 100644 --- a/courseware/content_parser.py +++ b/courseware/content_parser.py @@ -3,6 +3,7 @@ import json import logging import os import re +import sys from datetime import timedelta from lxml import etree @@ -22,6 +23,9 @@ course XML file and the rest of the system. TODO: Shift everything from xml.dom.minidom to XPath (or XQuery) ''' +class ContentException(Exception): + pass + log = logging.getLogger("mitx.courseware") @@ -207,21 +211,34 @@ def section_file(user, section): return tree -def module_xml(coursefile, module, id_tag, module_id): +def module_xml(user, module, id_tag, module_id): ''' Get XML for a module based on module and module_id. Assumes - module occurs once in courseware XML file.. ''' - doc = coursefile - + module occurs once in courseware XML file or hidden section. ''' # Sanitize input if not module.isalnum(): raise Exception("Module is not alphanumeric") if not module_id.isalnum(): raise Exception("Module ID is not alphanumeric") - xpath_search='//*/{module}[(@{id_tag} = "{id}") or (@id = "{id}")]'.format(module=module, - id_tag=id_tag, - id=module_id) + # Generate search + xpath_search='//{module}[(@{id_tag} = "{id}") or (@id = "{id}")]'.format(module=module, + id_tag=id_tag, + id=module_id) #result_set=doc.xpathEval(xpath_search) + doc = course_file(user) + section_list = (s[:-4] for s in os.listdir(settings.DATA_DIR+'/sections') if s[-4:]=='.xml') + result_set=doc.xpath(xpath_search) + if len(result_set)<1: + for section in section_list: + try: + s = section_file(user, section) + except etree.XMLSyntaxError: + ex= sys.exc_info() + raise ContentException("Malformed XML in " + section+ "("+str(ex[1].msg)+")") + result_set = s.xpath(xpath_search) + if len(result_set) != 0: + break + if len(result_set)>1: print "WARNING: Potentially malformed course file", module, module_id if len(result_set)==0: diff --git a/courseware/module_render.py b/courseware/module_render.py index d1f1f89930..0f96b8802f 100644 --- a/courseware/module_render.py +++ b/courseware/module_render.py @@ -67,7 +67,7 @@ def modx_dispatch(request, module=None, dispatch=None, id=None): ajax_url = '/modx/'+module+'/'+id+'/' # Grab the XML corresponding to the request from course.xml - xml = content_parser.module_xml(content_parser.course_file(request.user), module, 'id', id) + xml = content_parser.module_xml(request.user, module, 'id', id) # Create the module instance=courseware.modules.get_module_class(module)(xml,