From e06b6fea62082da8b5e5048db18c08c3f438693d Mon Sep 17 00:00:00 2001 From: "E. Kolpakov" Date: Wed, 17 Dec 2014 19:07:45 +0300 Subject: [PATCH] Filtering children by CAPA input type. --- .../xmodule/xmodule/library_content_module.py | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/common/lib/xmodule/xmodule/library_content_module.py b/common/lib/xmodule/xmodule/library_content_module.py index 45452a088d..8a59c4a764 100644 --- a/common/lib/xmodule/xmodule/library_content_module.py +++ b/common/lib/xmodule/xmodule/library_content_module.py @@ -25,6 +25,10 @@ from pkg_resources import resource_string _ = lambda text: text +ANY_CAPA_TYPE_VALUE = 'any' +CAPA_BLOCK_TYPE = 'problem' + + def enum(**enums): """ enum helper in lieu of enum34 """ return type('Enum', (), enums) @@ -32,6 +36,7 @@ def enum(**enums): def _get_capa_types(): capa_types = { + ANY_CAPA_TYPE_VALUE: _('Any Type'), 'annotationinput': _('Annotation'), 'checkboxgroup': _('Checkbox Group'), 'checkboxtextgroup': _('Checkbox Text Group'), @@ -185,8 +190,8 @@ class LibraryContentFields(object): capa_type = String( display_name=_("Problem Type"), help=_("The type of components to include in this block"), - default="any", - values=[{"display_name": _("Any Type"), "value": "any"}] + _get_capa_types(), + default=ANY_CAPA_TYPE_VALUE, + values=_get_capa_types(), scope=Scope.settings, ) filters = String(default="") # TBD @@ -215,6 +220,21 @@ class LibraryContentModule(LibraryContentFields, XModule, StudioEditableModule): as children of this block, but only a subset of those children are shown to any particular student. """ + def _filter_children(self, child_locator): + if self.capa_type == ANY_CAPA_TYPE_VALUE: + return True + + if child_locator.block_type != CAPA_BLOCK_TYPE: + return False + + block = self.runtime.get_block(child_locator) + + if not hasattr(block, 'lcp'): + return True + + return any(self.capa_type in capa_input.tags for capa_input in block.lcp.inputs.values()) + + def selected_children(self): """ Returns a set() of block_ids indicating which of the possible children @@ -231,7 +251,7 @@ class LibraryContentModule(LibraryContentFields, XModule, StudioEditableModule): return self._selected_set # pylint: disable=access-member-before-definition # Determine which of our children we will show: selected = set(tuple(k) for k in self.selected) # set of (block_type, block_id) tuples - valid_block_keys = set([(c.block_type, c.block_id) for c in self.children]) # pylint: disable=no-member + valid_block_keys = set([(c.block_type, c.block_id) for c in self.children if self._filter_children(c)]) # pylint: disable=no-member # Remove any selected blocks that are no longer valid: selected -= (selected - valid_block_keys) # If max_count has been decreased, we may have to drop some previously selected blocks: @@ -407,7 +427,8 @@ class LibraryContentDescriptor(LibraryContentFields, MakoModuleDescriptor, XmlDe If source_libraries has been edited, refresh_children automatically. """ old_source_libraries = LibraryList().from_json(old_metadata.get('source_libraries', [])) - if set(old_source_libraries) != set(self.source_libraries): + if (set(old_source_libraries) != set(self.source_libraries) or + old_metadata.get('capa_type', ANY_CAPA_TYPE_VALUE) != self.capa_type): try: self.refresh_children(None, None, update_db=False) # update_db=False since update_item() is about to be called anyways except ValueError: