All modules moved into new OO framework
This commit is contained in:
@@ -21,6 +21,7 @@ import courseware.modules.video_module
|
||||
import courseware.modules.vertical_module
|
||||
import courseware.modules.html_module
|
||||
import courseware.modules.schematic_module
|
||||
import courseware.modules.seq_module
|
||||
|
||||
from models import StudentModule
|
||||
|
||||
@@ -40,6 +41,7 @@ modx_modules={'problem':courseware.modules.capa_module.LoncapaModule,
|
||||
'video':courseware.modules.video_module.VideoModule,
|
||||
'html':courseware.modules.html_module.HtmlModule,
|
||||
'vertical':courseware.modules.vertical_module.VerticalModule,
|
||||
'sequential':courseware.modules.seq_module.SequentialModule,
|
||||
'schematic':courseware.modules.schematic_module.SchematicModule}
|
||||
|
||||
def make_track_function(request):
|
||||
@@ -83,52 +85,6 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
|
||||
# Return whatever the module wanted to return to the client/caller
|
||||
return HttpResponse(ajax_return)
|
||||
|
||||
def seq_module(request, module):
|
||||
''' Layout module which lays out content in a temporal sequence
|
||||
'''
|
||||
def j(m):
|
||||
# jsonify contents so it can be embedded in a js array
|
||||
# We also need to split </script> tags so they don't break
|
||||
# mid-string
|
||||
if 'init_js' not in m: m['init_js']=""
|
||||
if 'type' not in m: m['init_js']=""
|
||||
content=json.dumps(m['content'])
|
||||
content=content.replace('</script>', '<"+"/script>')
|
||||
|
||||
return {'content':content,
|
||||
"destroy_js":m['destroy_js'],
|
||||
'init_js':m['init_js'],
|
||||
'type':m['type']}
|
||||
contents=[(e.get("name"),j(render_module(request, e))) \
|
||||
for e in module]
|
||||
|
||||
js=""
|
||||
|
||||
iid=uuid.uuid1().hex
|
||||
|
||||
params={'items':contents,
|
||||
'id':"seq"}
|
||||
|
||||
# TODO/BUG: Destroy JavaScript should only be called for the active view
|
||||
# This calls it for all the views
|
||||
#
|
||||
# To fix this, we'd probably want to have some way of assigning unique
|
||||
# IDs to sequences.
|
||||
destroy_js="".join([e[1]['destroy_js'] for e in contents if 'destroy_js' in e[1]])
|
||||
|
||||
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.tag == 'tab':
|
||||
params['id'] = 'tab'
|
||||
return {'init_js':js+render_to_string('tab_module.js',params),
|
||||
"destroy_js":destroy_js,
|
||||
'content':render_to_string('tab_module.html',params),
|
||||
'type':'tab'}
|
||||
|
||||
|
||||
def render_x_module(request, xml_module):
|
||||
''' Generic module for extensions. This renders to HTML. '''
|
||||
# Check if problem has an instance in DB
|
||||
@@ -172,20 +128,8 @@ def render_x_module(request, xml_module):
|
||||
|
||||
return content
|
||||
|
||||
module_types={'video':render_x_module,
|
||||
'html':render_x_module,
|
||||
'tab':seq_module,
|
||||
'vertical':render_x_module,
|
||||
'sequential':seq_module,
|
||||
'problem':render_x_module,
|
||||
'schematic':render_x_module
|
||||
}
|
||||
|
||||
def render_module(request, module):
|
||||
''' Generic dispatch for internal modules. '''
|
||||
if module==None :
|
||||
return {"content":""}
|
||||
if str(module.tag) in module_types:
|
||||
return module_types[module.tag](request, module)
|
||||
print "rm404"
|
||||
raise Http404
|
||||
return render_x_module(request, module)
|
||||
|
||||
71
courseware/modules/seq_module.py
Normal file
71
courseware/modules/seq_module.py
Normal file
@@ -0,0 +1,71 @@
|
||||
from x_module import XModule
|
||||
from lxml import etree
|
||||
|
||||
import json
|
||||
|
||||
## TODO: Abstract out from Django
|
||||
from django.conf import settings
|
||||
from djangomako.shortcuts import render_to_response, render_to_string
|
||||
|
||||
class SequentialModule(XModule):
|
||||
''' Layout module which lays out content in a temporal sequence
|
||||
'''
|
||||
id_attribute = 'id'
|
||||
|
||||
def get_state(self):
|
||||
return json.dumps({ })
|
||||
|
||||
def get_xml_tags():
|
||||
return ["sequential", 'tab']
|
||||
|
||||
def get_html(self):
|
||||
return self.content
|
||||
|
||||
def get_init_js(self):
|
||||
return self.init_js
|
||||
|
||||
def get_destroy_js(self):
|
||||
return self.destroy_js
|
||||
|
||||
def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None, render_function = None, meta = None):
|
||||
XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function, render_function)
|
||||
xmltree=etree.fromstring(xml)
|
||||
|
||||
def j(m):
|
||||
''' jsonify contents so it can be embedded in a js array
|
||||
We also need to split </script> tags so they don't break
|
||||
mid-string'''
|
||||
if 'init_js' not in m: m['init_js']=""
|
||||
if 'type' not in m: m['init_js']=""
|
||||
content=json.dumps(m['content'])
|
||||
content=content.replace('</script>', '<"+"/script>')
|
||||
|
||||
return {'content':content,
|
||||
"destroy_js":m['destroy_js'],
|
||||
'init_js':m['init_js'],
|
||||
'type':m['type']}
|
||||
|
||||
contents=[(e.get("name"),j(render_function(meta, e))) \
|
||||
for e in xmltree]
|
||||
|
||||
js=""
|
||||
|
||||
params={'items':contents,
|
||||
'id':"seq"}
|
||||
|
||||
# TODO/BUG: Destroy JavaScript should only be called for the active view
|
||||
# This calls it for all the views
|
||||
#
|
||||
# To fix this, we'd probably want to have some way of assigning unique
|
||||
# IDs to sequences.
|
||||
destroy_js="".join([e[1]['destroy_js'] for e in contents if 'destroy_js' in e[1]])
|
||||
|
||||
if xmltree.tag == 'sequential':
|
||||
self.init_js=js+render_to_string('seq_module.js',params)
|
||||
self.destroy_js=destroy_js
|
||||
self.content=render_to_string('seq_module.html',params)
|
||||
if xmltree.tag == 'tab':
|
||||
params['id'] = 'tab'
|
||||
self.init_js=js+render_to_string('tab_module.js',params)
|
||||
self.destroy_js=destroy_js
|
||||
self.content=render_to_string('tab_module.html',params)
|
||||
@@ -28,11 +28,6 @@ class VerticalModule(XModule):
|
||||
def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None, render_function = None, meta = None):
|
||||
XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function, render_function)
|
||||
xmltree=etree.fromstring(xml)
|
||||
self.filename = None
|
||||
filename_l=xmltree.xpath("/html/@filename")
|
||||
if len(filename_l)>0:
|
||||
self.filename=str(filename_l[0])
|
||||
|
||||
self.contents=[(e.get("name"),self.render_function(meta, e)) \
|
||||
for e in xmltree]
|
||||
self.init_js_text="".join([e[1]['init_js'] for e in self.contents if 'init_js' in e[1]])
|
||||
|
||||
Reference in New Issue
Block a user