Exemplo n.º 1
0
def get_metadata_from_index(queryset):
    """
    Take a list of ContentLibrary objects and return metadata stored in
    ContentLibraryIndex.
    """
    metadata = None
    if ContentLibraryIndexer.indexing_is_enabled():
        try:
            library_keys = [lib.library_key for lib in queryset]
            metadata = ContentLibraryIndexer.get_libraries(library_keys)
        except (LibraryNotIndexedException, KeyError,
                ElasticConnectionError) as e:
            log.exception(e)

    # If ContentLibraryIndex is not available, we query blockstore for a limited set of metadata
    if metadata is None:
        uuids = [lib.bundle_uuid for lib in queryset]
        bundles = get_bundles(uuids)
        bundle_dict = {
            bundle.uuid: {
                'uuid': bundle.uuid,
                'title': bundle.title,
                'description': bundle.description,
                'version': bundle.latest_version,
            }
            for bundle in bundles
        }
        metadata = [bundle_dict[uuid] for uuid in uuids]

    libraries = [
        ContentLibraryMetadata(
            key=lib.library_key,
            bundle_uuid=metadata[i]['uuid'],
            title=metadata[i]['title'],
            description=metadata[i]['description'],
            version=metadata[i]['version'],
            allow_public_learning=queryset[i].allow_public_learning,
            allow_public_read=queryset[i].allow_public_read,
            num_blocks=metadata[i].get('num_blocks'),
            last_published=metadata[i].get('last_published'),
            has_unpublished_changes=metadata[i].get('has_unpublished_changes'),
            has_unpublished_deletes=metadata[i].get('has_unpublished_deletes'),
        ) for i, lib in enumerate(queryset)
    ]
    return libraries
Exemplo n.º 2
0
def get_metadata_from_index(queryset, text_search=None):
    """
    Take a list of ContentLibrary objects and return metadata stored in
    ContentLibraryIndex.
    """
    metadata = None
    if ContentLibraryIndexer.indexing_is_enabled():
        try:
            library_keys = [str(lib.library_key) for lib in queryset]
            metadata = ContentLibraryIndexer.get_items(library_keys,
                                                       text_search=text_search)
            metadata_dict = {item["id"]: item for item in metadata}
            metadata = [
                metadata_dict[key] if key in metadata_dict else None
                for key in library_keys
            ]
        except ElasticConnectionError as e:
            log.exception(e)

    # If ContentLibraryIndex is not available, we query blockstore for a limited set of metadata
    if metadata is None:
        uuids = [lib.bundle_uuid for lib in queryset]
        bundles = get_bundles(uuids=uuids, text_search=text_search)

        if text_search:
            # Bundle APIs can't apply text_search on a bundle's org, so including those results here
            queryset_org_search = queryset.filter(
                org__short_name__icontains=text_search)
            if queryset_org_search.exists():
                uuids_org_search = [
                    lib.bundle_uuid for lib in queryset_org_search
                ]
                bundles += get_bundles(uuids=uuids_org_search)

        bundle_dict = {
            bundle.uuid: {
                'uuid': bundle.uuid,
                'title': bundle.title,
                'description': bundle.description,
                'version': bundle.latest_version,
            }
            for bundle in bundles
        }
        metadata = [
            bundle_dict[uuid] if uuid in bundle_dict else None
            for uuid in uuids
        ]

    libraries = [
        ContentLibraryMetadata(
            key=lib.library_key,
            bundle_uuid=metadata[i]['uuid'],
            title=metadata[i]['title'],
            description=metadata[i]['description'],
            version=metadata[i]['version'],
            allow_public_learning=queryset[i].allow_public_learning,
            allow_public_read=queryset[i].allow_public_read,
            num_blocks=metadata[i].get('num_blocks'),
            last_published=metadata[i].get('last_published'),
            has_unpublished_changes=metadata[i].get('has_unpublished_changes'),
            has_unpublished_deletes=metadata[i].get('has_unpublished_deletes'),
        ) for i, lib in enumerate(queryset) if metadata[i] is not None
    ]
    return libraries