Commit 1ec3209d by Piotr Mitros

Moved courseware from minidom to lxml2. Appears to work, but big change. may still have bugs.

parent 8539b926
...@@ -29,6 +29,9 @@ from django.conf import settings ...@@ -29,6 +29,9 @@ from django.conf import settings
import content_parser import content_parser
import sys
from lxml import etree
import uuid import uuid
modx_modules={'problem':capa_module.LoncapaModule, modx_modules={'problem':capa_module.LoncapaModule,
...@@ -80,9 +83,8 @@ def modx_dispatch(request, module=None, dispatch=None, id=None): ...@@ -80,9 +83,8 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
def vertical_module(request, module): def vertical_module(request, module):
''' Layout module which lays out content vertically. ''' Layout module which lays out content vertically.
''' '''
contents=[(e.getAttribute("name"),render_module(request, e)) \ contents=[(e.get("name"),render_module(request, e)) \
for e in module.childNodes \ for e in module]
if e.nodeType==1]
init_js="".join([e[1]['init_js'] for e in contents if 'init_js' in e[1]]) 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]]) 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): ...@@ -107,9 +109,8 @@ def seq_module(request, module):
"destroy_js":m['destroy_js'], "destroy_js":m['destroy_js'],
'init_js':m['init_js'], 'init_js':m['init_js'],
'type':m['type']} 'type':m['type']}
contents=[(e.getAttribute("name"),j(render_module(request, e))) \ contents=[(e.get("name"),j(render_module(request, e))) \
for e in module.childNodes \ for e in module]
if e.nodeType==1]
js="" js=""
...@@ -125,12 +126,12 @@ def seq_module(request, module): ...@@ -125,12 +126,12 @@ def seq_module(request, module):
# IDs to sequences. # IDs to sequences.
destroy_js="".join([e[1]['destroy_js'] for e in contents if 'destroy_js' in e[1]]) 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), return {'init_js':js+render_to_string('seq_module.js',params),
"destroy_js":destroy_js, "destroy_js":destroy_js,
'content':render_to_string('seq_module.html',params), 'content':render_to_string('seq_module.html',params),
'type':'sequential'} 'type':'sequential'}
if module.nodeName == 'tab': if module.tag == 'tab':
params['id'] = 'tab' params['id'] = 'tab'
return {'init_js':js+render_to_string('tab_module.js',params), return {'init_js':js+render_to_string('tab_module.js',params),
"destroy_js":destroy_js, "destroy_js":destroy_js,
...@@ -141,9 +142,9 @@ def seq_module(request, module): ...@@ -141,9 +142,9 @@ def seq_module(request, module):
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
module_type=xml_module.nodeName module_type=xml_module.tag
module_class=modx_modules[module_type] 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 # Grab state from database
s = StudentModule.objects.filter(student=request.user, s = StudentModule.objects.filter(student=request.user,
...@@ -157,7 +158,7 @@ def render_x_module(request, xml_module): ...@@ -157,7 +158,7 @@ def render_x_module(request, xml_module):
# Create a new instance # Create a new instance
ajax_url = '/modx/'+module_type+'/'+module_id+'/' ajax_url = '/modx/'+module_type+'/'+module_id+'/'
instance=module_class(xml_module.toxml(), instance=module_class(etree.tostring(xml_module),
module_id, module_id,
ajax_url=ajax_url, ajax_url=ajax_url,
state=state, state=state,
...@@ -190,9 +191,9 @@ module_types={'video':render_x_module, ...@@ -190,9 +191,9 @@ module_types={'video':render_x_module,
def render_module(request, module): def render_module(request, module):
''' Generic dispatch for internal modules. ''' ''' Generic dispatch for internal modules. '''
if module==None: if module==None :
return {"content":""} return {"content":""}
if str(module.localName) in module_types: if str(module.tag) in module_types:
return module_types[module.localName](request, module) return module_types[module.tag](request, module)
print "rm404" print "rm404"
raise Http404 raise Http404
from django.http import HttpResponse from django.http import HttpResponse
from django.template import Context, loader from django.template import Context, loader
from djangomako.shortcuts import render_to_response, render_to_string from djangomako.shortcuts import render_to_response, render_to_string
from xml.dom.minidom import parse, parseString
import json, os, sys import json, os, sys
from django.core.context_processors import csrf from django.core.context_processors import csrf
...@@ -33,6 +32,9 @@ from module_render import * ...@@ -33,6 +32,9 @@ from module_render import *
from lxml import etree from lxml import etree
etree.set_default_parser(etree.XMLParser(dtd_validation=False, load_dtd=False,
remove_comments = True))
template_imports={'urllib':urllib} template_imports={'urllib':urllib}
def profile(request): def profile(request):
...@@ -114,6 +116,7 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti ...@@ -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 # Fixes URLs -- we don't get funny encoding characters from spaces
# so they remain readable # so they remain readable
## TODO: Properly replace underscores
course=course.replace("_"," ") course=course.replace("_"," ")
chapter=chapter.replace("_"," ") chapter=chapter.replace("_"," ")
section=section.replace("_"," ") section=section.replace("_"," ")
...@@ -147,3 +150,48 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti ...@@ -147,3 +150,48 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti
return render_to_response('courseware.html', context) 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)
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