From e1ac5e9f30080b6e9a83ac10120ee9bc524b7fcb Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Mon, 7 May 2012 20:13:30 -0400 Subject: [PATCH] Moved extra settings into state. Added fs module for aux content --- djangoapps/courseware/module_render.py | 37 ++++++++++++++----- djangoapps/courseware/modules/capa_module.py | 4 +- djangoapps/courseware/modules/html_module.py | 8 ++-- .../courseware/modules/schematic_module.py | 4 +- djangoapps/courseware/modules/seq_module.py | 5 +-- .../courseware/modules/template_module.py | 5 +-- .../courseware/modules/vertical_module.py | 5 ++- djangoapps/courseware/modules/video_module.py | 4 +- djangoapps/courseware/modules/x_module.py | 10 ++--- 9 files changed, 49 insertions(+), 33 deletions(-) diff --git a/djangoapps/courseware/module_render.py b/djangoapps/courseware/module_render.py index 2924aec9df..134bff8501 100644 --- a/djangoapps/courseware/module_render.py +++ b/djangoapps/courseware/module_render.py @@ -18,8 +18,12 @@ from django.http import HttpResponse from django.shortcuts import redirect from django.template import Context from django.template import Context, loader + +from fs.osfs import OSFS + from mitxmako.shortcuts import render_to_response, render_to_string + from models import StudentModule from student.models import UserProfile import track.views @@ -30,6 +34,13 @@ import courseware.modules log = logging.getLogger("mitx.courseware") +class I4xSystem(object): + def __init__(self, **args): + self.ajax_url = args['ajax_url'] + self.track_function = args['track_function'] + self.filestore = OSFS(settings.DATA_DIR) + self.render_function = args['render_function'] + def object_cache(cache, user, module_type, module_id): # We don't look up on user -- all queries include user # Additional lookup would require a DB hit the way Django @@ -76,12 +87,15 @@ def modx_dispatch(request, module=None, dispatch=None, id=None): xml = content_parser.module_xml(request.user, module, 'id', id) # Create the module - instance=courseware.modules.get_module_class(module)(xml, + system = I4xSystem(track_function = make_track_function(request), + render_function = None, + ajax_url = ajax_url, + filestore = None + ) + instance=courseware.modules.get_module_class(module)(system, + xml, id, - ajax_url=ajax_url, - state=oldstate, - track_function = make_track_function(request), - render_function = None) + state=oldstate) # Let the module handle the AJAX ajax_return=instance.handle_ajax(dispatch, request.POST) # Save the state back to the database @@ -128,12 +142,15 @@ def render_x_module(user, request, xml_module, module_object_preload): # Create a new instance ajax_url = '/modx/'+module_type+'/'+module_id+'/' - instance=module_class(etree.tostring(xml_module), + system = I4xSystem(track_function = make_track_function(request), + render_function = lambda x: render_module(user, request, x, module_object_preload), + ajax_url = ajax_url, + filestore = None + ) + instance=module_class(system, + etree.tostring(xml_module), module_id, - ajax_url=ajax_url, - state=state, - track_function = make_track_function(request), - render_function = lambda x: render_module(user, request, x, module_object_preload)) + state=state) # If instance wasn't already in the database, create it if not smod: diff --git a/djangoapps/courseware/modules/capa_module.py b/djangoapps/courseware/modules/capa_module.py index 86958dcf1c..c6deca64f6 100644 --- a/djangoapps/courseware/modules/capa_module.py +++ b/djangoapps/courseware/modules/capa_module.py @@ -131,8 +131,8 @@ class Module(XModule): return html - 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) + def __init__(self, system, xml, item_id, state=None): + XModule.__init__(self, system, xml, item_id, state) self.attempts = 0 self.max_attempts = None diff --git a/djangoapps/courseware/modules/html_module.py b/djangoapps/courseware/modules/html_module.py index 4194f73e74..08a7b268a9 100644 --- a/djangoapps/courseware/modules/html_module.py +++ b/djangoapps/courseware/modules/html_module.py @@ -24,13 +24,13 @@ class Module(XModule): textlist=[i for i in textlist if type(i)==str] return "".join(textlist) try: - filename=settings.DATA_DIR+"html/"+self.filename - return open(filename).read() + filename="html/"+self.filename + return self.filestore.open(filename).read() except: # For backwards compatibility. TODO: Remove return render_to_string(self.filename, {'id': self.item_id}) - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None, render_function = None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function, render_function) + def __init__(self, system, xml, item_id, state=None): + XModule.__init__(self, system, xml, item_id, state) xmltree=etree.fromstring(xml) self.filename = None filename_l=xmltree.xpath("/html/@filename") diff --git a/djangoapps/courseware/modules/schematic_module.py b/djangoapps/courseware/modules/schematic_module.py index e253f1acc6..5fef265e01 100644 --- a/djangoapps/courseware/modules/schematic_module.py +++ b/djangoapps/courseware/modules/schematic_module.py @@ -19,6 +19,6 @@ class Module(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, render_function = None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, render_function) + def __init__(self, system, xml, item_id, state=None): + XModule.__init__(self, system, xml, item_id, state) diff --git a/djangoapps/courseware/modules/seq_module.py b/djangoapps/courseware/modules/seq_module.py index 02796a9768..276931d9f3 100644 --- a/djangoapps/courseware/modules/seq_module.py +++ b/djangoapps/courseware/modules/seq_module.py @@ -107,9 +107,8 @@ class Module(XModule): self.rendered = True - - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None, render_function = None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function, render_function) + def __init__(self, system, xml, item_id, state=None): + XModule.__init__(self, system, xml, item_id, state) self.xmltree=etree.fromstring(xml) self.position = 1 diff --git a/djangoapps/courseware/modules/template_module.py b/djangoapps/courseware/modules/template_module.py index d9dbb613f0..07e920b5a3 100644 --- a/djangoapps/courseware/modules/template_module.py +++ b/djangoapps/courseware/modules/template_module.py @@ -20,10 +20,9 @@ class Module(XModule): def get_html(self): return self.html - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None, render_function = None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function, render_function) + def __init__(self, system, xml, item_id, state=None): + XModule.__init__(self, system, xml, item_id, state) xmltree = etree.fromstring(xml) filename = xmltree.tag params = dict(xmltree.items()) -# print params self.html = render_to_string(filename, params, namespace = 'custom_tags') diff --git a/djangoapps/courseware/modules/vertical_module.py b/djangoapps/courseware/modules/vertical_module.py index c068cb9a76..0867460e95 100644 --- a/djangoapps/courseware/modules/vertical_module.py +++ b/djangoapps/courseware/modules/vertical_module.py @@ -26,8 +26,9 @@ class Module(XModule): 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): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function, render_function) + + def __init__(self, system, xml, item_id, state=None): + XModule.__init__(self, system, xml, item_id, state) xmltree=etree.fromstring(xml) self.contents=[(e.get("name"),self.render_function(e)) \ for e in xmltree] diff --git a/djangoapps/courseware/modules/video_module.py b/djangoapps/courseware/modules/video_module.py index a893d1f3bb..aa44bd0ab2 100644 --- a/djangoapps/courseware/modules/video_module.py +++ b/djangoapps/courseware/modules/video_module.py @@ -58,8 +58,8 @@ class Module(XModule): def get_destroy_js(self): return "videoDestroy(\"{0}\");".format(self.item_id)+self.annotations_destroy - def __init__(self, xml, item_id, ajax_url=None, track_url=None, state=None, track_function=None, render_function = None): - XModule.__init__(self, xml, item_id, ajax_url, track_url, state, track_function, render_function) + def __init__(self, system, xml, item_id, state=None): + XModule.__init__(self, system, xml, item_id, state) xmltree=etree.fromstring(xml) self.youtube = xmltree.get('youtube') self.name = xmltree.get('name') diff --git a/djangoapps/courseware/modules/x_module.py b/djangoapps/courseware/modules/x_module.py index 4a379253c4..df96763d26 100644 --- a/djangoapps/courseware/modules/x_module.py +++ b/djangoapps/courseware/modules/x_module.py @@ -45,13 +45,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, render_function = None): + def __init__(self, system, xml, item_id, track_url=None, state=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.render_function = render_function + self.ajax_url = system.ajax_url + self.tracker = system.track_function + self.filestore = system.filestore + self.render_function = system.render_function