Beispiel #1
0
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
Beispiel #2
0
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 definition_for_usage(self, usage_key):
        """
        Given a usage key for an XBlock in this context, return the
        BundleDefinitionLocator which specifies the actual XBlock definition
        (as a path to an OLX in a specific blockstore bundle).

        Must return a BundleDefinitionLocator if the XBlock exists in this
        context, or None otherwise.
        """
        library_key = usage_key.context_key
        try:
            bundle_uuid = bundle_uuid_for_library_key(library_key)
        except ContentLibrary.DoesNotExist:
            return None
        bundle = LibraryBundle(library_key, bundle_uuid, self.use_draft)
        return bundle.definition_for_usage(usage_key)
Beispiel #4
0
    def definition_for_usage(self, usage_key, **kwargs):
        """
        Given a usage key for an XBlock in this context, return the
        BundleDefinitionLocator which specifies the actual XBlock definition
        (as a path to an OLX in a specific blockstore bundle).

        Must return a BundleDefinitionLocator if the XBlock exists in this
        context, or None otherwise.
        """
        library_key = usage_key.context_key
        try:
            bundle_uuid = bundle_uuid_for_library_key(library_key)
        except ContentLibrary.DoesNotExist:
            return None
        if 'force_draft' in kwargs:  # lint-amnesty, pylint: disable=consider-using-get
            use_draft = kwargs['force_draft']
        else:
            use_draft = self.use_draft
        bundle = LibraryBundle(library_key, bundle_uuid, use_draft)
        return bundle.definition_for_usage(usage_key)