From b1b08d29896e66f73566e23873ab579f9acd67f7 Mon Sep 17 00:00:00 2001 From: ichuang Date: Fri, 1 Jun 2012 14:34:26 -0400 Subject: [PATCH] made "position" a generic parameter passed to all modules, from courseware.views.index --- djangoapps/courseware/module_render.py | 33 ++++++++++++++++++--- djangoapps/courseware/modules/seq_module.py | 4 +++ djangoapps/courseware/modules/x_module.py | 2 ++ djangoapps/courseware/views.py | 27 +++++------------ 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/djangoapps/courseware/module_render.py b/djangoapps/courseware/module_render.py index 7a3e4cd7b7..c0b8aaf958 100644 --- a/djangoapps/courseware/module_render.py +++ b/djangoapps/courseware/module_render.py @@ -38,6 +38,11 @@ class I4xSystem(object): self.render_function = render_function self.exception404 = Http404 self.DEBUG = settings.DEBUG + + def get(self,attr): # uniform access to attributes (like etree) + return self.__dict__.get(attr) + def set(self,attr,val): # uniform access to attributes (like etree) + self.__dict__[attr] = val def __repr__(self): return repr(self.__dict__) def __str__(self): @@ -100,7 +105,7 @@ def get_state_from_module_object_preload(user, xml_module, module_object_preload return smod, state -def render_x_module(user, request, xml_module, module_object_preload): +def render_x_module(user, request, xml_module, module_object_preload,position=None): ''' Generic module for extensions. This renders to HTML. modules include sequential, vertical, problem, video, html @@ -113,6 +118,11 @@ def render_x_module(user, request, xml_module, module_object_preload): - request : current django HTTPrequest - xml_module : lxml etree of xml subtree for the current module - module_object_preload : list of StudentModule objects, one of which may match this module type and id + - position : extra information from URL for user-specified position within module + + Returns: + + - dict which is context for HTML rendering of the specified module ''' module_type=xml_module.tag @@ -138,6 +148,7 @@ def render_x_module(user, request, xml_module, module_object_preload): ajax_url = ajax_url, filestore = OSFS(data_root), ) + system.set('position',position) # pass URL specified position along to module, through I4xSystem instance=module_class(system, etree.tostring(xml_module), module_id, @@ -176,8 +187,22 @@ def render_x_module(user, request, xml_module, module_object_preload): return content -def render_module(user, request, module, module_object_preload): - ''' Generic dispatch for internal modules. ''' +def render_module(user, request, module, module_object_preload, position=None): + ''' Generic dispatch for internal modules. + + Args: + + - user : django User + - request : HTTP request + - module : ElementTree (xml) for this module + - module_object_preload : list of StudentModule objects, one of which may match this module type and id + - position : extra information from URL for user-specified position within module + + Returns: + + - dict which is context for HTML rendering of the specified module + + ''' if module==None : return {"content":""} - return render_x_module(user, request, module, module_object_preload) + return render_x_module(user, request, module, module_object_preload, position) diff --git a/djangoapps/courseware/modules/seq_module.py b/djangoapps/courseware/modules/seq_module.py index adce633885..b4e67d208f 100644 --- a/djangoapps/courseware/modules/seq_module.py +++ b/djangoapps/courseware/modules/seq_module.py @@ -113,4 +113,8 @@ class Module(XModule): state = json.loads(state) if 'position' in state: self.position = int(state['position']) + # if position is specified in system, then use that instead + if system.get('position'): + self.position = int(system.get('position')) + self.rendered = False diff --git a/djangoapps/courseware/modules/x_module.py b/djangoapps/courseware/modules/x_module.py index 0898ac4f8f..ffaeb8fb4d 100644 --- a/djangoapps/courseware/modules/x_module.py +++ b/djangoapps/courseware/modules/x_module.py @@ -68,6 +68,7 @@ class XModule(object): self.xml = xml self.item_id = item_id self.state = state + self.DEBUG = False if system: ## These are temporary; we really should go @@ -76,4 +77,5 @@ class XModule(object): self.tracker = system.track_function self.filestore = system.filestore self.render_function = system.render_function + self.DEBUG = system.DEBUG self.system = system diff --git a/djangoapps/courseware/views.py b/djangoapps/courseware/views.py index daae47e3ce..cb41d46a57 100644 --- a/djangoapps/courseware/views.py +++ b/djangoapps/courseware/views.py @@ -160,7 +160,11 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi - course : coursename (str) - chapter : chapter name (str) - section : section name (str) - - position : position in sequence, ie of module (int) + - position : position in module, eg of module (str) + + Returns: + + - HTTPresponse ''' user = request.user @@ -215,21 +219,6 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi else: module_object_preload = [] - if position and module and module.tag=='sequential': - smod, state = get_state_from_module_object_preload(user, module, module_object_preload) - newstate = json.dumps({ 'position':position }) - if smod: - smod.state = newstate - elif user.is_authenticated(): - smod=StudentModule(student=user, - module_type = module.tag, - module_id= module.get('id'), - state = newstate) - smod.save() - # now regenerate module_object_preload - module_object_preload = list(StudentModule.objects.filter(student=user, - module_id__in=module_ids)) - context = { 'csrf': csrf(request)['csrf_token'], 'accordion': render_accordion(request, course, chapter, section), @@ -237,7 +226,7 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi } try: - module = render_module(user, request, module, module_object_preload) # ugh - shouldn't overload module + module_context = render_module(user, request, module, module_object_preload, position) except: log.exception("Unable to load module") context.update({ @@ -247,8 +236,8 @@ def index(request, course=None, chapter="Using the System", section="Hints",posi return render_to_response('courseware.html', context) context.update({ - 'init': module.get('init_js', ''), - 'content': module['content'], + 'init': module_context.get('init_js', ''), + 'content': module_context['content'], }) result = render_to_response('courseware.html', context)