From cbce664ecfcf6f568a1a86cf663e8f338e2ff734 Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Wed, 9 Oct 2024 16:18:48 +0530 Subject: [PATCH] feat: add collections to component api --- .../core/djangoapps/content_libraries/api.py | 22 +++++++++++++++++-- .../content_libraries/serializers.py | 10 +++++++++ .../djangoapps/content_libraries/views.py | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/openedx/core/djangoapps/content_libraries/api.py b/openedx/core/djangoapps/content_libraries/api.py index 11b720bbb970..e5f43375866e 100644 --- a/openedx/core/djangoapps/content_libraries/api.py +++ b/openedx/core/djangoapps/content_libraries/api.py @@ -204,6 +204,15 @@ class ContentLibraryPermissionEntry: access_level = attr.ib(AccessLevel.NO_ACCESS) +@attr.s +class CollectionMetadata: + """ + Class to represent collection metadata in a content library. + """ + key = attr.ib(type=str) + title = attr.ib(type=str) + + @attr.s class LibraryXBlockMetadata: """ @@ -219,9 +228,10 @@ class LibraryXBlockMetadata: published_by = attr.ib("") has_unpublished_changes = attr.ib(False) created = attr.ib(default=None, type=datetime) + collections = attr.ib(type=list[CollectionMetadata], factory=list) @classmethod - def from_component(cls, library_key, component): + def from_component(cls, library_key, component, collections=None): """ Construct a LibraryXBlockMetadata from a Component object. """ @@ -248,6 +258,7 @@ def from_component(cls, library_key, component): last_draft_created=last_draft_created, last_draft_created_by=last_draft_created_by, has_unpublished_changes=component.versioning.has_unpublished_changes, + collections=collections or [], ) @@ -690,7 +701,7 @@ def get_library_components(library_key, text_search=None, block_types=None) -> Q return components -def get_library_block(usage_key) -> LibraryXBlockMetadata: +def get_library_block(usage_key, include_collections=False) -> LibraryXBlockMetadata: """ Get metadata about (the draft version of) one specific XBlock in a library. @@ -713,9 +724,16 @@ def get_library_block(usage_key) -> LibraryXBlockMetadata: if not draft_version: raise ContentLibraryBlockNotFound(usage_key) + collections = [] + if include_collections: + collections = authoring_api.get_entity_collections( + component.learning_package_id, + component.key, + ).values('key', 'title') xblock_metadata = LibraryXBlockMetadata.from_component( library_key=usage_key.context_key, component=component, + collections=collections, ) return xblock_metadata diff --git a/openedx/core/djangoapps/content_libraries/serializers.py b/openedx/core/djangoapps/content_libraries/serializers.py index 6765656e69d5..8e9e5fc2a749 100644 --- a/openedx/core/djangoapps/content_libraries/serializers.py +++ b/openedx/core/djangoapps/content_libraries/serializers.py @@ -134,6 +134,14 @@ class ContentLibraryFilterSerializer(BaseFilterSerializer): type = serializers.ChoiceField(choices=LIBRARY_TYPES, default=None, required=False) +class CollectionMetadataSerializer(serializers.Serializer): + """ + Serializer for CollectionMetadata + """ + key = serializers.CharField() + title = serializers.CharField() + + class LibraryXBlockMetadataSerializer(serializers.Serializer): """ Serializer for LibraryXBlockMetadata @@ -161,6 +169,8 @@ class LibraryXBlockMetadataSerializer(serializers.Serializer): slug = serializers.CharField(write_only=True) tags_count = serializers.IntegerField(read_only=True) + collections = CollectionMetadataSerializer(many=True, required=False) + class LibraryXBlockTypeSerializer(serializers.Serializer): """ diff --git a/openedx/core/djangoapps/content_libraries/views.py b/openedx/core/djangoapps/content_libraries/views.py index c9051d155208..6e50559f38f6 100644 --- a/openedx/core/djangoapps/content_libraries/views.py +++ b/openedx/core/djangoapps/content_libraries/views.py @@ -618,7 +618,7 @@ def get(self, request, usage_key_str): """ key = LibraryUsageLocatorV2.from_string(usage_key_str) api.require_permission_for_library_key(key.lib_key, request.user, permissions.CAN_VIEW_THIS_CONTENT_LIBRARY) - result = api.get_library_block(key) + result = api.get_library_block(key, include_collections=True) return Response(LibraryXBlockMetadataSerializer(result).data)