Commit 2a216dd3 by Calen Pennington

Refactor to use wrapping classes

parent ac078937
import xmodule import xmodule
from xmodule import progress from xmodule import progress
def XModule(): class XModule():
def __init__(self, content, policy, state, child_pointers, preferences): class StudentWidget(ResourceTemplate):
self.content = content def __init__(self, content, policy, state, child_pointers, preferences):
self.policy = policy self.content = content
self.state = state self.policy = policy
self.child_pointers = child_pointers self.state = state
self.preferences = preferences self.child_pointers = child_pointers
self.preferences = preferences
@lazy_property
def children(self): @lazy_property
return [xmodule.get_module(child_ptr) for child_ptr in self.child_pointers] def children(self):
return [xmodule.get_module(child_ptr) for child_ptr in self.child_pointers]
def render(self, child, context=None):
if context is None: def render(self, child, context=None):
context = self.render_context if context is None:
context = self.render_context
# Make children use the appropriate render context
child.render_context = context # Make children use the appropriate render context
return child.find_view(context)() child.render_context = context
return child.find_view(context)()
def find_view(self, context):
# self._views is populated with metaclass magic that is TBD def find_view(self, context):
return self._views[context] # self._views is populated with metaclass magic that is TBD
return self._views[context]
def XModuleEditor():
class EditorWidget(ResourceTemplate):
@xmodule.register_view('edit') @xmodule.register_view('edit')
def edit(self): def edit(self):
return self.render_template( return self.render_template(
'combined', 'combined',
content=self.render(self, 'edit_content'), content=self.render(self, 'edit_content'),
policy=self.render(self, 'edit_policy'), policy=self.render(self, 'edit_policy'),
children=self.render(self, 'edit_children'), children=self.render(self, 'edit_children'),
) )
@xmodule.register_view('edit_children') @xmodule.register_view('edit_children')
def edit_children(self): def edit_children(self):
return self.render_template( return self.render_template(
'drag_and_drop', 'drag_and_drop',
children=self.children children=self.children
) )
@xmodule.register_view('edit_policy') @xmodule.register_view('edit_policy')
def edit_policy(self): def edit_policy(self):
return self.render_template( return self.render_template(
'mapping', 'mapping',
mapping=self.policy mapping=self.policy
) )
@xmodule.register_view('edit_content') @xmodule.register_view('edit_content')
def edit_content(self): def edit_content(self):
return self.render_template( return self.render_template(
'mapping', 'mapping',
mapping=self.content mapping=self.content
) )
@xmodule.register_handler('update_children') @xmodule.register_handler('update_children')
def update_children(self, data): def update_children(self, data):
""" """
Expects a new list of child xmodule ids Expects a new list of child xmodule ids
""" """
self.children = data['children'] self.children = data['children']
@xmodule.register_handler('set_policy') @xmodule.register_handler('set_policy')
def set_policy(self, data): def set_policy(self, data):
""" """
Expects keys 'name' and 'value'. Expects keys 'name' and 'value'.
If value is None, then delete the key, otherwise set If value is None, then delete the key, otherwise set
the named key to the value the named key to the value
""" """
if data['value'] is None: if data['value'] is None:
del self.policy[data['value']] del self.policy[data['value']]
else: else:
self.policy[data['name']] = self.policy[data['value']] self.policy[data['name']] = self.policy[data['value']]
def SequenceEditor(XModuleEditor, ResourceTemplate): class Sequence():
class EditorWidget(XModule.EditorWidget):
@xmodule.register_view('edit')
def edit(self): @xmodule.register_view('edit')
return self.render_template( def edit(self):
'combined', return self.render_template(
children=self.render(self, 'edit_children'), 'combined',
policy=self.render(self, 'edit_policy'), children=self.render(self, 'edit_children'),
) policy=self.render(self, 'edit_policy'),
)
@xmodule.register_view('edit_content')
def empty_view(self): @xmodule.register_view('edit_content')
return None def empty_view(self):
return None
def SequenceModule(XModule, ResourceTemplate):
class StudentWidget(XModule.StudentWidget):
@property
def visited(self): @property
""" def visited(self):
Return the set of ids that this user has visited in the past """
""" Return the set of ids that this user has visited in the past
return set(self.state['visited']) """
return set(self.state['visited'])
@property
def possible(self): @property
""" def possible(self):
Return the set of ids in this sequence that a student could have visited """
""" Return the set of ids in this sequence that a student could have visited
return set(child.id for child in self.children) """
return set(child.id for child in self.children)
@xmodule.register_view('student')
@xmodule.register_view('instructor') @xmodule.register_view('student')
def student_view(self): @xmodule.register_view('instructor')
return self.render_template( def student_view(self):
'main', return self.render_template(
# Render w/ no arguments executes the same view for the children 'main',
# that is currently rendering for the parent # Render w/ no arguments executes the same view for the children
children=[self.render(child) for child in self.children] # that is currently rendering for the parent
) children=[self.render(child) for child in self.children]
)
@xmodule.register_handler('update_position')
def update_position(self, data): @xmodule.register_handler('update_position')
new_position = self.children[data['position']].id def update_position(self, data):
# Updates to the state dictionary are transparently saved to the db new_position = self.children[data['position']].id
self.state['position'] = new_position # Updates to the state dictionary are transparently saved to the db
self.state['visited'] = self.visited.union(new_position) self.state['position'] = new_position
self.update_progress() self.state['visited'] = self.visited.union(new_position)
self.update_progress()
def update_progress(self):
progress.publish({ def update_progress(self):
'visited': (len(self.visited & self.possible), len(self.possible)) progress.publish({
}) 'visited': (len(self.visited & self.possible), len(self.possible))
})
def ChemistryEquationModule(XModule, ResourceTemplateModule):
class ChemistryEquation(XModule):
def __init__(self, definition, policy, state, preferences): class ChemistryEquationModule(XModule.StudentWidget):
self.state = state
def __init__(self, definition, policy, state, preferences):
@xmodule.register_view('student') self.state = state
def student_view(self):
return self.render_template( @xmodule.register_view('student')
'main', def student_view(self):
equation=self.state['equation'], return self.render_template(
rendered_equation=self.render_equation(self.state['equation']) 'main',
) equation=self.state['equation'],
rendered_equation=self.render_equation(self.state['equation'])
@xmodule.register_handler('render_eq') )
def render_eq_handler(self, data):
self.state['equation'] = data['equation'] @xmodule.register_handler('render_eq')
return self.render_equation(data['equation']) def render_eq_handler(self, data):
self.state['equation'] = data['equation']
def render_equation(self, equation): return self.render_equation(data['equation'])
return PIL.render_render_render(equation)
\ No newline at end of file def render_equation(self, equation):
return PIL.render_render_render(equation)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment