Skip to content

Commit

Permalink
feat: add collections to component api
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Oct 10, 2024
1 parent 0a438d0 commit 4c8ab8a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
22 changes: 20 additions & 2 deletions openedx/core/djangoapps/content_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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.
"""
Expand All @@ -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 [],
)


Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions openedx/core/djangoapps/content_libraries/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/content_libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 4c8ab8a

Please sign in to comment.