From 1ec3209d05aaf8a9c629e279defa328e3aba5e88 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sat, 21 Jan 2012 12:12:00 -0500 Subject: [PATCH] Moved courseware from minidom to lxml2. Appears to work, but big change. may still have bugs. --- courseware/module_render.py | 29 ++++++++++----------- courseware/views.py | 50 ++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/courseware/module_render.py b/courseware/module_render.py index 58574df190..0e83f936f2 100644 --- a/courseware/module_render.py +++ b/courseware/module_render.py @@ -29,6 +29,9 @@ from django.conf import settings import content_parser +import sys + +from lxml import etree import uuid modx_modules={'problem':capa_module.LoncapaModule, @@ -80,9 +83,8 @@ def modx_dispatch(request, module=None, dispatch=None, id=None): def vertical_module(request, module): ''' Layout module which lays out content vertically. ''' - contents=[(e.getAttribute("name"),render_module(request, e)) \ - for e in module.childNodes \ - if e.nodeType==1] + contents=[(e.get("name"),render_module(request, e)) \ + for e in module] init_js="".join([e[1]['init_js'] for e in contents if 'init_js' in e[1]]) destroy_js="".join([e[1]['destroy_js'] for e in contents if 'destroy_js' in e[1]]) @@ -107,9 +109,8 @@ def seq_module(request, module): "destroy_js":m['destroy_js'], 'init_js':m['init_js'], 'type':m['type']} - contents=[(e.getAttribute("name"),j(render_module(request, e))) \ - for e in module.childNodes \ - if e.nodeType==1] + contents=[(e.get("name"),j(render_module(request, e))) \ + for e in module] js="" @@ -125,12 +126,12 @@ def seq_module(request, module): # IDs to sequences. destroy_js="".join([e[1]['destroy_js'] for e in contents if 'destroy_js' in e[1]]) - if module.nodeName == 'sequential': + if module.tag == 'sequential': return {'init_js':js+render_to_string('seq_module.js',params), "destroy_js":destroy_js, 'content':render_to_string('seq_module.html',params), 'type':'sequential'} - if module.nodeName == 'tab': + if module.tag == 'tab': params['id'] = 'tab' return {'init_js':js+render_to_string('tab_module.js',params), "destroy_js":destroy_js, @@ -141,9 +142,9 @@ def seq_module(request, module): def render_x_module(request, xml_module): ''' Generic module for extensions. This renders to HTML. ''' # Check if problem has an instance in DB - module_type=xml_module.nodeName + module_type=xml_module.tag module_class=modx_modules[module_type] - module_id=xml_module.getAttribute(module_class.id_attribute) + module_id=xml_module.get(module_class.id_attribute) or "" # TODO: remove or "" # Grab state from database s = StudentModule.objects.filter(student=request.user, @@ -157,7 +158,7 @@ def render_x_module(request, xml_module): # Create a new instance ajax_url = '/modx/'+module_type+'/'+module_id+'/' - instance=module_class(xml_module.toxml(), + instance=module_class(etree.tostring(xml_module), module_id, ajax_url=ajax_url, state=state, @@ -190,9 +191,9 @@ module_types={'video':render_x_module, def render_module(request, module): ''' Generic dispatch for internal modules. ''' - if module==None: + if module==None : return {"content":""} - if str(module.localName) in module_types: - return module_types[module.localName](request, module) + if str(module.tag) in module_types: + return module_types[module.tag](request, module) print "rm404" raise Http404 diff --git a/courseware/views.py b/courseware/views.py index 16f9435cf8..22d5853fcb 100644 --- a/courseware/views.py +++ b/courseware/views.py @@ -1,7 +1,6 @@ from django.http import HttpResponse from django.template import Context, loader from djangomako.shortcuts import render_to_response, render_to_string -from xml.dom.minidom import parse, parseString import json, os, sys from django.core.context_processors import csrf @@ -33,6 +32,9 @@ from module_render import * from lxml import etree +etree.set_default_parser(etree.XMLParser(dtd_validation=False, load_dtd=False, + remove_comments = True)) + template_imports={'urllib':urllib} def profile(request): @@ -114,6 +116,7 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti # Fixes URLs -- we don't get funny encoding characters from spaces # so they remain readable + ## TODO: Properly replace underscores course=course.replace("_"," ") chapter=chapter.replace("_"," ") section=section.replace("_"," ") @@ -147,3 +150,48 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti return render_to_response('courseware.html', context) + +def index(request, course="6.002 Spring 2012", chapter="Using the System", section="Hints"): + ''' Displays courseware accordion, and any associated content. + ''' + if not settings.COURSEWARE_ENABLED or not request.user.is_authenticated(): + return redirect('/') + + # Fixes URLs -- we don't get funny encoding characters from spaces + # so they remain readable + ## TODO: Properly replace underscores + course=course.replace("_"," ") + chapter=chapter.replace("_"," ") + section=section.replace("_"," ") + + # HACK: Force course to 6.002 for now + # Without this, URLs break + if course!="6.002 Spring 2012": + return redirect('/') + + cf = content_parser.course_file(request.user) + dom=etree.parse(cf) + #dom_course=content_parser.dom_select(dom, 'course', course) + #dom_chapter=content_parser.dom_select(dom_course, 'chapter', chapter) + #dom_section=content_parser.dom_select(dom_chapter, 'section', section) + dom_module = dom.xpath("//course[@name=$course]/chapter[@name=$chapter]//section[@name=$section]/*[1]", + course=course, chapter=chapter, section=section) + if len(dom_module) == 0: + module = None + else: + module = dom_module[0] + + accordion=render_accordion(request, course, chapter, section) + + module=render_module(request, module) + + if 'init_js' not in module: + module['init_js']='' + + context={'init':accordion['init_js']+module['init_js'], + 'accordion':accordion['content'], + 'content':module['content'], + 'csrf':csrf(request)['csrf_token']} + return render_to_response('courseware.html', context) + +