From e21b9191ae3c4188377bc759ef2ed7d90ccaf88c Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Tue, 27 Dec 2011 19:17:40 -0500 Subject: [PATCH] Replaced libxml2 with lxml.etree --- courseware/capa_module.py | 17 ++++---- courseware/content_parser.py | 17 +++++--- courseware/html_module.py | 2 +- courseware/module_render.py | 70 ++++++++++++++++----------------- courseware/static/css/local.css | 2 +- 5 files changed, 58 insertions(+), 50 deletions(-) diff --git a/courseware/capa_module.py b/courseware/capa_module.py index 45a2dd7c71..8436670ec7 100644 --- a/courseware/capa_module.py +++ b/courseware/capa_module.py @@ -9,7 +9,7 @@ import datetime import content_parser -import libxml2 +from lxml import etree ## TODO: Abstract out from Django from django.conf import settings @@ -103,31 +103,32 @@ class LoncapaModule(XModule): self.due_date = None #dom=parseString(xml) - dom2 = libxml2.parseMemory(xml, len(xml)) + #dom2 = libxml2.parseMemory(xml, len(xml)) + dom2 = etree.fromstring(xml) #node=dom.childNodes[0] #self.due_date=node.getAttribute("due") - self.due_date=content_parser.item(dom2.xpathEval('/problem/@due')) + self.due_date=content_parser.item(dom2.xpath('/problem/@due'))#dom2.xpathEval('/problem/@due')) if len(self.due_date)>0: self.due_date=dateutil.parser.parse(self.due_date) else: self.due_date=None #self.max_attempts=node.getAttribute("attempts") - self.max_attempts=content_parser.item(dom2.xpathEval('/problem/@attempts')) + self.max_attempts=content_parser.item(dom2.xpath('/problem/@attempts')) if len(self.max_attempts)>0: self.max_attempts=int(self.max_attempts) else: self.max_attempts=None #self.show_answer=node.getAttribute("showanswer") - self.show_answer=content_parser.item(dom2.xpathEval('/problem/@showanswer')) + self.show_answer=content_parser.item(dom2.xpath('/problem/@showanswer')) if self.show_answer=="": self.show_answer="closed" - self.rerandomize=content_parser.item(dom2.xpathEval('/problem/@rerandomize')) + self.rerandomize=content_parser.item(dom2.xpath('/problem/@rerandomize')) #self.rerandomize=node.getAttribute("rerandomize") if self.rerandomize=="": self.rerandomize=True @@ -143,12 +144,12 @@ class LoncapaModule(XModule): if state!=None and 'attempts' in state: self.attempts=state['attempts'] - self.filename=content_parser.item(dom2.xpathEval('/problem/@filename')) + self.filename=content_parser.item(dom2.xpath('/problem/@filename')) #self.filename=node.getAttribute("filename") #print self.filename filename=settings.DATA_DIR+"problems/"+self.filename+".xml" #self.name=node.getAttribute("name") - self.name=content_parser.item(dom2.xpathEval('/problem/@name')) + self.name=content_parser.item(dom2.xpath('/problem/@name')) self.lcp=LoncapaProblem(filename, self.item_id, state) def handle_ajax(self, dispatch, get): diff --git a/courseware/content_parser.py b/courseware/content_parser.py index cccf0dfd41..d1ebd36cd4 100644 --- a/courseware/content_parser.py +++ b/courseware/content_parser.py @@ -1,6 +1,8 @@ from django.conf import settings from xml.dom.minidom import parse, parseString -import libxml2 + +from lxml import etree + from auth.models import UserProfile ''' This file will eventually form an abstraction layer between the @@ -13,7 +15,7 @@ def item(l, default="", process=lambda x:x): if len(l)==0: return default elif len(l)==1: - return process(l[0].getContent()) + return process(l[0]) else: raise Exception('Malformed XML') @@ -25,7 +27,8 @@ def course_file(user): def module_xml(coursefile, 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 = libxml2.parseFile(coursefile) + #doc = libxml2.parseFile(coursefile) + doc = etree.parse(coursefile) # Sanitize input if not module.isalnum(): @@ -35,12 +38,14 @@ def module_xml(coursefile, module, id_tag, module_id): xpath_search='//*/{module}[@{id_tag} = "{id}"]'.format(module=module, id_tag=id_tag, id=module_id) - result_set=doc.xpathEval(xpath_search) + #result_set=doc.xpathEval(xpath_search) + result_set=doc.xpath(xpath_search) if len(result_set)>1: print "WARNING: Potentially malformed course file", module, module_id if len(result_set)==0: return None - return result_set[0].serialize() + return etree.tostring(result_set[0]) + #return result_set[0].serialize() def toc_from_xml(coursefile, active_chapter, active_section): dom=parse(coursefile) @@ -50,6 +55,8 @@ def toc_from_xml(coursefile, active_chapter, active_section): chapters = course.getElementsByTagName('chapter') ch=list() for c in chapters: + if c.getAttribute("name") == 'hidden': + continue sections=list() for s in c.getElementsByTagName('section'): sections.append({'name':s.getAttribute("name"), diff --git a/courseware/html_module.py b/courseware/html_module.py index 2f1134eff2..2035116146 100644 --- a/courseware/html_module.py +++ b/courseware/html_module.py @@ -1,4 +1,5 @@ from x_module import XModule +from lxml import etree import json @@ -19,5 +20,4 @@ class HtmlModule(XModule): return render_to_string(self.item_id, {'id': self.item_id}) def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None): - print "item id" , item_id XModule.__init__(self, xml, item_id, ajax_url, track_url, state) diff --git a/courseware/module_render.py b/courseware/module_render.py index d11effbf03..8976bc6965 100644 --- a/courseware/module_render.py +++ b/courseware/module_render.py @@ -30,6 +30,41 @@ import content_parser import uuid +modx_modules={'problem':capa_module.LoncapaModule, + 'video':video_module.VideoModule, + 'html':html_module.HtmlModule, + 'schematic':schematic_module.SchematicModule} + +def modx_dispatch(request, module=None, dispatch=None, id=None): + ''' Generic view for extensions. ''' + s = StudentModule.objects.filter(module_type=module, + student=request.user, + module_id=id) + if len(s) == 0: + print "ls404" + raise Http404 + + s=s[0] + + dispatch=dispatch.split('?')[0] + + ajax_url = '/modx/'+module+'/'+id+'/' + + id_tag=modx_modules[module].id_attribute + #print "X",s.xml, "Y",content_parser.module_xml(module, id_tag, id) + + xml = content_parser.module_xml(content_parser.course_file(request.user), module, id_tag, id) + + instance=modx_modules[module](xml, + s.module_id, + ajax_url=ajax_url, + state=s.state) + html=instance.handle_ajax(dispatch, request.GET) + s.state=instance.get_state() + s.grade=instance.get_score()['score'] + s.save() + return HttpResponse(html) + def vertical_module(request, module): ''' Layout module which lays out content vertically. ''' @@ -81,11 +116,6 @@ def seq_module(request, module): 'type':'sequential'} -modx_modules={'problem':capa_module.LoncapaModule, - 'video':video_module.VideoModule, - 'html':html_module.HtmlModule, - 'schematic':schematic_module.SchematicModule} - def render_x_module(request, xml_module): ''' Generic module for extensions. This renders to HTML. ''' # Check if problem has an instance in DB @@ -126,36 +156,6 @@ def render_x_module(request, xml_module): return content -def modx_dispatch(request, module=None, dispatch=None, id=None): - ''' Generic module for extensions. ''' - s = StudentModule.objects.filter(module_type=module, - student=request.user, - module_id=id) - if len(s) == 0: - print "ls404" - raise Http404 - - s=s[0] - - dispatch=dispatch.split('?')[0] - - ajax_url = '/modx/'+module+'/'+id+'/' - - id_tag=modx_modules[module].id_attribute - #print "X",s.xml, "Y",content_parser.module_xml(module, id_tag, id) - - xml = content_parser.module_xml(content_parser.course_file(request.user), module, id_tag, id) - - instance=modx_modules[module](xml, - s.module_id, - ajax_url=ajax_url, - state=s.state) - html=instance.handle_ajax(dispatch, request.GET) - s.state=instance.get_state() - s.grade=instance.get_score()['score'] - s.save() - return HttpResponse(html) - module_types={'video':render_x_module, 'html':render_x_module, 'tab':seq_module, diff --git a/courseware/static/css/local.css b/courseware/static/css/local.css index 25c5bc3527..288d92cacb 100644 --- a/courseware/static/css/local.css +++ b/courseware/static/css/local.css @@ -33,7 +33,7 @@ width: 100%; } -.bordered { border: 1px solid #AAAAAA; } +.bordered { border: 1px solid #AAAAAA; border-style : dotted; } .seq_problem_visited { background-color: #ccccaa;} .seq_video_visited { background-color: #ccaacc;}