From ba2c03357cdd3759f982318a90f2151cbb4c51b4 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sat, 21 Jan 2012 18:46:34 -0500 Subject: [PATCH] All modules moved into new OO framework --- courseware/module_render.py | 62 ++--------------------- courseware/modules/seq_module.py | 71 +++++++++++++++++++++++++++ courseware/modules/vertical_module.py | 5 -- 3 files changed, 74 insertions(+), 64 deletions(-) create mode 100644 courseware/modules/seq_module.py diff --git a/courseware/module_render.py b/courseware/module_render.py index e6dd0a5341..36f14508ef 100644 --- a/courseware/module_render.py +++ b/courseware/module_render.py @@ -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 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>') - - 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) diff --git a/courseware/modules/seq_module.py b/courseware/modules/seq_module.py new file mode 100644 index 0000000000..f6d4cc53b1 --- /dev/null +++ b/courseware/modules/seq_module.py @@ -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 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>') + + 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) diff --git a/courseware/modules/vertical_module.py b/courseware/modules/vertical_module.py index 343cc5e8d5..b5ad1be368 100644 --- a/courseware/modules/vertical_module.py +++ b/courseware/modules/vertical_module.py @@ -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]])