feat: Add tags count for v2 library components (#33979)

This commit is contained in:
Yusuf Musleh
2024-01-09 21:23:30 +03:00
committed by GitHub
parent 829ac5178f
commit 8d2ddd73ec
7 changed files with 20 additions and 5 deletions

View File

@@ -134,6 +134,7 @@ from xmodule.library_root_xblock import LibraryRoot as LibraryRootV1
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.exceptions import ItemNotFoundError
from openedx_tagging.core.tagging import api as tagging_api
from . import tasks
@@ -234,6 +235,7 @@ class LibraryXBlockMetadata:
def_key = attr.ib(type=BundleDefinitionLocator)
display_name = attr.ib("")
has_unpublished_changes = attr.ib(False)
tags_count = attr.ib(0)
@attr.s
@@ -636,6 +638,15 @@ def delete_library(library_key):
raise
def _get_library_component_tags_count(library_key) -> dict:
"""
Get the count of tags that are applied to each component in this library, as a dict.
"""
# Create a pattern to match the IDs of the library components, e.g. "lb:org:id*"
library_key_pattern = str(library_key).replace("lib:", "lb:", 1) + "*"
return tagging_api.get_object_tag_counts(library_key_pattern, count_implicit=True)
def get_library_blocks(library_key, text_search=None, block_types=None) -> list[LibraryXBlockMetadata]:
"""
Get the list of top-level XBlocks in the specified library.
@@ -646,6 +657,7 @@ def get_library_blocks(library_key, text_search=None, block_types=None) -> list[
ref = ContentLibrary.objects.get_by_key(library_key) # type: ignore[attr-defined]
lib_bundle = LibraryBundle(library_key, ref.bundle_uuid, draft_name=DRAFT_NAME)
usages = lib_bundle.get_top_level_usages()
library_component_tags_count = _get_library_component_tags_count(library_key)
for usage_key in usages:
# For top-level definitions, we can go from definition key to usage key using the following, but this would
@@ -663,6 +675,7 @@ def get_library_blocks(library_key, text_search=None, block_types=None) -> list[
"def_key": def_key,
"display_name": display_name,
"has_unpublished_changes": lib_bundle.does_definition_have_unpublished_changes(def_key),
"tags_count": library_component_tags_count.get(str(usage_key), 0),
})
return [
@@ -671,6 +684,7 @@ def get_library_blocks(library_key, text_search=None, block_types=None) -> list[
def_key=item['def_key'],
display_name=item['display_name'],
has_unpublished_changes=item['has_unpublished_changes'],
tags_count=item['tags_count']
)
for item in metadata
]

View File

@@ -117,6 +117,7 @@ class LibraryXBlockMetadataSerializer(serializers.Serializer):
# When creating a new XBlock in a library, the slug becomes the ID part of
# the definition key and usage key:
slug = serializers.CharField(write_only=True)
tags_count = serializers.IntegerField(read_only=True)
class LibraryXBlockTypeSerializer(serializers.Serializer):