def get_library_blocks(library_key, text_search=None, block_types=None): """ Get the list of top-level XBlocks in the specified library. Returns a list of LibraryXBlockMetadata objects """ metadata = None if LibraryBlockIndexer.indexing_is_enabled(): try: filter_terms = { 'library_key': [str(library_key)], 'is_child': [False], } if block_types: filter_terms['block_type'] = block_types metadata = [ { **item, "id": LibraryUsageLocatorV2.from_string(item['id']), } for item in LibraryBlockIndexer.get_items(filter_terms=filter_terms, text_search=text_search) if item is not None ] except ElasticConnectionError as e: log.exception(e) # If indexing is disabled, or connection to elastic failed if metadata is None: metadata = [] ref = ContentLibrary.objects.get_by_key(library_key) lib_bundle = LibraryBundle(library_key, ref.bundle_uuid, draft_name=DRAFT_NAME) usages = lib_bundle.get_top_level_usages() for usage_key in usages: # For top-level definitions, we can go from definition key to usage key using the following, but this would # not work for non-top-level blocks as they may have multiple usages. Top level blocks are guaranteed to # have only a single usage in the library, which is part of the definition of top level block. def_key = lib_bundle.definition_for_usage(usage_key) display_name = get_block_display_name(def_key) text_match = (text_search is None or text_search.lower() in display_name.lower() or text_search.lower() in str(usage_key).lower()) type_match = (block_types is None or usage_key.block_type in block_types) if text_match and type_match: metadata.append({ "id": usage_key, "def_key": def_key, "display_name": display_name, "has_unpublished_changes": lib_bundle.does_definition_have_unpublished_changes(def_key), }) return [ LibraryXBlockMetadata( usage_key=item['id'], def_key=item['def_key'], display_name=item['display_name'], has_unpublished_changes=item['has_unpublished_changes'], ) for item in metadata ]
def get_library_block(usage_key): """ Get metadata (LibraryXBlockMetadata) about one specific XBlock in a library To load the actual XBlock instance, use openedx.core.djangoapps.xblock.api.load_block() instead. """ assert isinstance(usage_key, LibraryUsageLocatorV2) lib_context = get_learning_context_impl(usage_key) def_key = lib_context.definition_for_usage(usage_key) if def_key is None: raise ContentLibraryBlockNotFound(usage_key) lib_bundle = LibraryBundle(usage_key.library_slug, def_key.bundle_uuid, draft_name=DRAFT_NAME) return LibraryXBlockMetadata( usage_key=usage_key, def_key=def_key, display_name=get_block_display_name(def_key), has_unpublished_changes=lib_bundle.does_definition_have_unpublished_changes(def_key), )
def get_library_blocks(library_key): """ Get the list of top-level XBlocks in the specified library. Returns a list of LibraryXBlockMetadata objects """ ref = ContentLibrary.objects.get_by_key(library_key) lib_bundle = LibraryBundle(library_key, ref.bundle_uuid, draft_name=DRAFT_NAME) usages = lib_bundle.get_top_level_usages() blocks = [] for usage_key in usages: # For top-level definitions, we can go from definition key to usage key using the following, but this would not # work for non-top-level blocks as they may have multiple usages. Top level blocks are guaranteed to have only # a single usage in the library, which is part of the definition of top level block. def_key = lib_bundle.definition_for_usage(usage_key) blocks.append(LibraryXBlockMetadata( usage_key=usage_key, def_key=def_key, display_name=get_block_display_name(def_key), has_unpublished_changes=lib_bundle.does_definition_have_unpublished_changes(def_key), )) return blocks