Commit e21b9191 by Piotr Mitros

Replaced libxml2 with lxml.etree

parent f5965487
...@@ -9,7 +9,7 @@ import datetime ...@@ -9,7 +9,7 @@ import datetime
import content_parser import content_parser
import libxml2 from lxml import etree
## TODO: Abstract out from Django ## TODO: Abstract out from Django
from django.conf import settings from django.conf import settings
...@@ -103,31 +103,32 @@ class LoncapaModule(XModule): ...@@ -103,31 +103,32 @@ class LoncapaModule(XModule):
self.due_date = None self.due_date = None
#dom=parseString(xml) #dom=parseString(xml)
dom2 = libxml2.parseMemory(xml, len(xml)) #dom2 = libxml2.parseMemory(xml, len(xml))
dom2 = etree.fromstring(xml)
#node=dom.childNodes[0] #node=dom.childNodes[0]
#self.due_date=node.getAttribute("due") #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: if len(self.due_date)>0:
self.due_date=dateutil.parser.parse(self.due_date) self.due_date=dateutil.parser.parse(self.due_date)
else: else:
self.due_date=None self.due_date=None
#self.max_attempts=node.getAttribute("attempts") #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: if len(self.max_attempts)>0:
self.max_attempts=int(self.max_attempts) self.max_attempts=int(self.max_attempts)
else: else:
self.max_attempts=None self.max_attempts=None
#self.show_answer=node.getAttribute("showanswer") #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=="": if self.show_answer=="":
self.show_answer="closed" 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") #self.rerandomize=node.getAttribute("rerandomize")
if self.rerandomize=="": if self.rerandomize=="":
self.rerandomize=True self.rerandomize=True
...@@ -143,12 +144,12 @@ class LoncapaModule(XModule): ...@@ -143,12 +144,12 @@ class LoncapaModule(XModule):
if state!=None and 'attempts' in state: if state!=None and 'attempts' in state:
self.attempts=state['attempts'] 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") #self.filename=node.getAttribute("filename")
#print self.filename #print self.filename
filename=settings.DATA_DIR+"problems/"+self.filename+".xml" filename=settings.DATA_DIR+"problems/"+self.filename+".xml"
#self.name=node.getAttribute("name") #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) self.lcp=LoncapaProblem(filename, self.item_id, state)
def handle_ajax(self, dispatch, get): def handle_ajax(self, dispatch, get):
......
from django.conf import settings from django.conf import settings
from xml.dom.minidom import parse, parseString from xml.dom.minidom import parse, parseString
import libxml2
from lxml import etree
from auth.models import UserProfile from auth.models import UserProfile
''' This file will eventually form an abstraction layer between the ''' This file will eventually form an abstraction layer between the
...@@ -13,7 +15,7 @@ def item(l, default="", process=lambda x:x): ...@@ -13,7 +15,7 @@ def item(l, default="", process=lambda x:x):
if len(l)==0: if len(l)==0:
return default return default
elif len(l)==1: elif len(l)==1:
return process(l[0].getContent()) return process(l[0])
else: else:
raise Exception('Malformed XML') raise Exception('Malformed XML')
...@@ -25,7 +27,8 @@ def course_file(user): ...@@ -25,7 +27,8 @@ def course_file(user):
def module_xml(coursefile, module, id_tag, module_id): def module_xml(coursefile, module, id_tag, module_id):
''' Get XML for a module based on module and module_id. Assumes ''' Get XML for a module based on module and module_id. Assumes
module occurs once in courseware XML file.. ''' module occurs once in courseware XML file.. '''
doc = libxml2.parseFile(coursefile) #doc = libxml2.parseFile(coursefile)
doc = etree.parse(coursefile)
# Sanitize input # Sanitize input
if not module.isalnum(): if not module.isalnum():
...@@ -35,12 +38,14 @@ def module_xml(coursefile, module, id_tag, module_id): ...@@ -35,12 +38,14 @@ def module_xml(coursefile, module, id_tag, module_id):
xpath_search='//*/{module}[@{id_tag} = "{id}"]'.format(module=module, xpath_search='//*/{module}[@{id_tag} = "{id}"]'.format(module=module,
id_tag=id_tag, id_tag=id_tag,
id=module_id) 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: if len(result_set)>1:
print "WARNING: Potentially malformed course file", module, module_id print "WARNING: Potentially malformed course file", module, module_id
if len(result_set)==0: if len(result_set)==0:
return None 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): def toc_from_xml(coursefile, active_chapter, active_section):
dom=parse(coursefile) dom=parse(coursefile)
...@@ -50,6 +55,8 @@ def toc_from_xml(coursefile, active_chapter, active_section): ...@@ -50,6 +55,8 @@ def toc_from_xml(coursefile, active_chapter, active_section):
chapters = course.getElementsByTagName('chapter') chapters = course.getElementsByTagName('chapter')
ch=list() ch=list()
for c in chapters: for c in chapters:
if c.getAttribute("name") == 'hidden':
continue
sections=list() sections=list()
for s in c.getElementsByTagName('section'): for s in c.getElementsByTagName('section'):
sections.append({'name':s.getAttribute("name"), sections.append({'name':s.getAttribute("name"),
......
from x_module import XModule from x_module import XModule
from lxml import etree
import json import json
...@@ -19,5 +20,4 @@ class HtmlModule(XModule): ...@@ -19,5 +20,4 @@ class HtmlModule(XModule):
return render_to_string(self.item_id, {'id': self.item_id}) 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): 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) XModule.__init__(self, xml, item_id, ajax_url, track_url, state)
...@@ -30,6 +30,41 @@ import content_parser ...@@ -30,6 +30,41 @@ import content_parser
import uuid 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): def vertical_module(request, module):
''' Layout module which lays out content vertically. ''' Layout module which lays out content vertically.
''' '''
...@@ -81,11 +116,6 @@ def seq_module(request, module): ...@@ -81,11 +116,6 @@ def seq_module(request, module):
'type':'sequential'} '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): def render_x_module(request, xml_module):
''' Generic module for extensions. This renders to HTML. ''' ''' Generic module for extensions. This renders to HTML. '''
# Check if problem has an instance in DB # Check if problem has an instance in DB
...@@ -126,36 +156,6 @@ def render_x_module(request, xml_module): ...@@ -126,36 +156,6 @@ def render_x_module(request, xml_module):
return content 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, module_types={'video':render_x_module,
'html':render_x_module, 'html':render_x_module,
'tab':seq_module, 'tab':seq_module,
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
width: 100%; width: 100%;
} }
.bordered { border: 1px solid #AAAAAA; } .bordered { border: 1px solid #AAAAAA; border-style : dotted; }
.seq_problem_visited { background-color: #ccccaa;} .seq_problem_visited { background-color: #ccccaa;}
.seq_video_visited { background-color: #ccaacc;} .seq_video_visited { background-color: #ccaacc;}
......
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