Commit c6bb02f5 by Braden MacDonald

Consolidate some code and speed up StepParentMixin

parent b6f6a971
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
# along with this program in a file in the toplevel directory called # along with this program in a file in the toplevel directory called
# "AGPLv3". If not, see <http://www.gnu.org/licenses/>. # "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
# #
from .utils import child_isinstance
class StepParentMixin(object): class StepParentMixin(object):
...@@ -27,15 +28,9 @@ class StepParentMixin(object): ...@@ -27,15 +28,9 @@ class StepParentMixin(object):
@property @property
def steps(self): def steps(self):
""" """
Generator returning the usage_id for all of this XBlock's Get the usage_ids of all of this XBlock's children that are "Steps"
children that are "Steps"
""" """
step_ids = [] return [child_id for child_id in self.children if child_isinstance(self, child_id, StepMixin)]
for child_id in self.children:
child = self.runtime.get_block(child_id)
if isinstance(child, StepMixin):
step_ids.append(child_id)
return step_ids
class StepMixin(object): class StepMixin(object):
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
import errno import errno
from .utils import child_isinstance
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fields import Scope, String from xblock.fields import Scope, String
from xblock.fragment import Fragment from xblock.fragment import Fragment
...@@ -120,7 +122,7 @@ class MentoringTableColumnBlock(XBlock): ...@@ -120,7 +122,7 @@ class MentoringTableColumnBlock(XBlock):
""" """
return self._render_table_view( return self._render_table_view(
view_name='mentoring_table_view', view_name='mentoring_table_view',
id_filter=lambda child_id: not issubclass(self._get_child_class(child_id), MentoringTableColumnHeaderBlock), id_filter=lambda child_id: not child_isinstance(self, child_id, MentoringTableColumnHeaderBlock),
template='mentoring-table-column.html', template='mentoring-table-column.html',
context=context context=context
) )
...@@ -131,20 +133,11 @@ class MentoringTableColumnBlock(XBlock): ...@@ -131,20 +133,11 @@ class MentoringTableColumnBlock(XBlock):
""" """
return self._render_table_view( return self._render_table_view(
view_name='mentoring_table_header_view', view_name='mentoring_table_header_view',
id_filter=lambda child_id: issubclass(self._get_child_class(child_id), MentoringTableColumnHeaderBlock), id_filter=lambda child_id: child_isinstance(self, child_id, MentoringTableColumnHeaderBlock),
template='mentoring-table-header.html', template='mentoring-table-header.html',
context=context context=context
) )
def _get_child_class(self, child_id):
"""
Helper method to get a block type from a usage_id without loading the block.
Returns the XBlock subclass of the child block.
"""
type_name = self.runtime.id_reader.get_block_type(self.runtime.id_reader.get_definition_id(child_id))
return self.runtime.load_block_type(type_name)
class MentoringTableColumnHeaderBlock(XBlock): class MentoringTableColumnHeaderBlock(XBlock):
""" """
......
"""
Helper methods
Should eventually be moved to xblock-utils.
"""
def child_isinstance(block, child_id, block_class_or_mixin):
"""
Is "block"'s child identified by usage_id "child_id" an instance of
"block_class_or_mixin"?
This is a bit complicated since it avoids the need to actually
instantiate the child block.
"""
def_id = block.runtime.id_reader.get_definition_id(child_id)
type_name = block.runtime.id_reader.get_block_type(def_id)
child_class = block.runtime.load_block_type(type_name)
return issubclass(child_class, block_class_or_mixin)
...@@ -12,7 +12,11 @@ class Parent(StepParentMixin): ...@@ -12,7 +12,11 @@ class Parent(StepParentMixin):
@property @property
def runtime(self): def runtime(self):
return Mock(get_block=lambda i: self._children[i]) return Mock(
get_block=lambda i: self._children[i],
load_block_type=lambda i: type(self._children[i]),
id_reader=Mock(get_definition_id=lambda i: i, get_block_type=lambda i: i)
)
def _set_children_for_test(self, *children): def _set_children_for_test(self, *children):
self._children = children self._children = children
...@@ -24,7 +28,11 @@ class Parent(StepParentMixin): ...@@ -24,7 +28,11 @@ class Parent(StepParentMixin):
pass pass
class Step(StepMixin): class BaseClass(object):
pass
class Step(BaseClass, StepMixin):
def __init__(self): def __init__(self):
pass pass
......
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