Replaced libxml2 with lxml.etree

This commit is contained in:
Piotr Mitros
2011-12-27 19:17:40 -05:00
parent f5965487db
commit e21b9191ae
5 changed files with 58 additions and 50 deletions

View File

@@ -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):

View File

@@ -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"),

View File

@@ -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)

View File

@@ -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,

View File

@@ -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;}