From 16ab1bb564f0e27a5a4f6f94e4f1dff4b7d0887c Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sat, 21 Jan 2012 15:25:25 -0500 Subject: [PATCH] Vertical Module is now an x_module --- courseware/module_render.py | 21 ++++---------- courseware/modules/capa_module.py | 4 +-- courseware/modules/html_module.py | 4 +-- courseware/modules/schematic_module.py | 4 +-- courseware/modules/vertical_module.py | 39 ++++++++++++++++++++++++++ courseware/modules/video_module.py | 4 +-- courseware/modules/x_module.py | 16 ++++++----- courseware/views.py | 4 +++ 8 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 courseware/modules/vertical_module.py diff --git a/courseware/module_render.py b/courseware/module_render.py index 5a69715f02..e6dd0a5341 100644 --- a/courseware/module_render.py +++ b/courseware/module_render.py @@ -18,6 +18,7 @@ import urllib import courseware.modules.capa_module import courseware.modules.video_module +import courseware.modules.vertical_module import courseware.modules.html_module import courseware.modules.schematic_module @@ -38,6 +39,7 @@ import uuid 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, 'schematic':courseware.modules.schematic_module.SchematicModule} def make_track_function(request): @@ -81,19 +83,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 vertical_module(request, module): - ''' Layout module which lays out content vertically. - ''' - 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]]) - - return {'init_js':init_js, - 'destroy_js':destroy_js, - 'content':render_to_string('vert_module.html',{'items':contents}), - 'type':'vertical'} - def seq_module(request, module): ''' Layout module which lays out content in a temporal sequence ''' @@ -163,7 +152,9 @@ def render_x_module(request, xml_module): module_id, ajax_url=ajax_url, state=state, - track_function = make_track_function(request)) + track_function = make_track_function(request), + render_function = render_module, + meta = request) # If instance wasn't already in the database, create it if len(s) == 0: @@ -184,7 +175,7 @@ def render_x_module(request, xml_module): module_types={'video':render_x_module, 'html':render_x_module, 'tab':seq_module, - 'vertical':vertical_module, + 'vertical':render_x_module, 'sequential':seq_module, 'problem':render_x_module, 'schematic':render_x_module diff --git a/courseware/modules/capa_module.py b/courseware/modules/capa_module.py index 7c6953a8bf..591f9c7516 100644 --- a/courseware/modules/capa_module.py +++ b/courseware/modules/capa_module.py @@ -108,8 +108,8 @@ class LoncapaModule(XModule): return html - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function) + 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) self.attempts = 0 self.max_attempts = None diff --git a/courseware/modules/html_module.py b/courseware/modules/html_module.py index 853a322236..ce26ea0045 100644 --- a/courseware/modules/html_module.py +++ b/courseware/modules/html_module.py @@ -25,8 +25,8 @@ class HtmlModule(XModule): textlist=[i for i in textlist if type(i)==str] return "".join(textlist) - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function) + 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") diff --git a/courseware/modules/schematic_module.py b/courseware/modules/schematic_module.py index 5a7fa390a0..b97e80541b 100644 --- a/courseware/modules/schematic_module.py +++ b/courseware/modules/schematic_module.py @@ -18,6 +18,6 @@ class SchematicModule(XModule): def get_html(self): return ''.format(item_id=self.item_id) - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state) + def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, render_function = None, meta = None): + XModule.__init__(self, xml, item_id, ajax_url, track_url, state, render_function) diff --git a/courseware/modules/vertical_module.py b/courseware/modules/vertical_module.py new file mode 100644 index 0000000000..343cc5e8d5 --- /dev/null +++ b/courseware/modules/vertical_module.py @@ -0,0 +1,39 @@ +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 VerticalModule(XModule): + id_attribute = 'id' + + def get_state(self): + return json.dumps({ }) + + def get_xml_tags(): + return "vertical" + + def get_html(self): + return render_to_string('vert_module.html',{'items':self.contents}) + + def get_init_js(self): + return self.init_js_text + + def get_destroy_js(self): + return self.destroy_js_text + + 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]]) + self.destroy_js_text="".join([e[1]['destroy_js'] for e in self.contents if 'destroy_js' in e[1]]) diff --git a/courseware/modules/video_module.py b/courseware/modules/video_module.py index 0fdb54f119..4676184b7e 100644 --- a/courseware/modules/video_module.py +++ b/courseware/modules/video_module.py @@ -46,8 +46,8 @@ class VideoModule(XModule): def get_destroy_js(self): return "videoDestroy();" - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function) + 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) print state if state!=None and "time" not in json.loads(state): self.video_time = 0 diff --git a/courseware/modules/x_module.py b/courseware/modules/x_module.py index 2b79519d20..75ff038f23 100644 --- a/courseware/modules/x_module.py +++ b/courseware/modules/x_module.py @@ -39,11 +39,13 @@ class XModule(object): get is a dictionary-like object ''' return "" - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None): + def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None, render_function = None, meta = None): ''' In most cases, you must pass state or xml''' - self.xml=xml - self.item_id=item_id - self.ajax_url=ajax_url - self.track_url=track_url - self.state=state - self.tracker=track_function + self.xml = xml + self.item_id = item_id + self.ajax_url = ajax_url + self.track_url = track_url + self.state = state + self.tracker = track_function + self.render_function = render_function + self.meta = meta diff --git a/courseware/views.py b/courseware/views.py index 2656f18aa4..98d575baf5 100644 --- a/courseware/views.py +++ b/courseware/views.py @@ -133,9 +133,13 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti module=render_module(request, module) + print "Here",module['init_js'] + if 'init_js' not in module: module['init_js']='' + + context={'init':accordion['init_js']+module['init_js'], 'accordion':accordion['content'], 'content':module['content'],