Skip to content

Commit

Permalink
feat: Add tags count for v2 library components (#33979)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh authored Jan 9, 2024
1 parent 829ac51 commit 8d2ddd7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 5 deletions.
14 changes: 14 additions & 0 deletions openedx/core/djangoapps/content_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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 [
Expand All @@ -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
]
Expand Down
1 change: 1 addition & 0 deletions openedx/core/djangoapps/content_libraries/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ libsass==0.10.0
click==8.1.6

# pinning this version to avoid updates while the library is being developed
openedx-learning==0.4.1
openedx-learning==0.4.2

# Open AI version 1.0.0 dropped support for openai.ChatCompletion which is currently in use in enterprise.
openai<=0.28.1
2 changes: 1 addition & 1 deletion requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ openedx-filters==1.6.0
# via
# -r requirements/edx/kernel.in
# lti-consumer-xblock
openedx-learning==0.4.1
openedx-learning==0.4.2
# via
# -c requirements/edx/../constraints.txt
# -r requirements/edx/kernel.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ openedx-filters==1.6.0
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
# lti-consumer-xblock
openedx-learning==0.4.1
openedx-learning==0.4.2
# via
# -c requirements/edx/../constraints.txt
# -r requirements/edx/doc.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ openedx-filters==1.6.0
# via
# -r requirements/edx/base.txt
# lti-consumer-xblock
openedx-learning==0.4.1
openedx-learning==0.4.2
# via
# -c requirements/edx/../constraints.txt
# -r requirements/edx/base.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ openedx-filters==1.6.0
# via
# -r requirements/edx/base.txt
# lti-consumer-xblock
openedx-learning==0.4.1
openedx-learning==0.4.2
# via
# -c requirements/edx/../constraints.txt
# -r requirements/edx/base.txt
Expand Down

0 comments on commit 8d2ddd7

Please sign in to comment.