From 15ea0c8e1023e8762a7d4f6e7f18bfdae3a86d17 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Mon, 9 Jun 2025 20:08:08 -0500 Subject: [PATCH] feat: Add `tags_count` to `ContainerMetadata` --- .../content_libraries/api/block_metadata.py | 5 +--- .../content_libraries/api/containers.py | 3 +++ .../content_libraries/rest_api/containers.py | 5 +++- .../tests/test_containers.py | 26 +++++++++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/openedx/core/djangoapps/content_libraries/api/block_metadata.py b/openedx/core/djangoapps/content_libraries/api/block_metadata.py index 507822d3074e..ec5c43b1218b 100644 --- a/openedx/core/djangoapps/content_libraries/api/block_metadata.py +++ b/openedx/core/djangoapps/content_libraries/api/block_metadata.py @@ -48,10 +48,7 @@ def from_component(cls, library_key, component, associated_collections=None): tags = get_object_tag_counts(str(usage_key), count_implicit=True) return cls( - usage_key=library_component_usage_key( - library_key, - component, - ), + usage_key=usage_key, display_name=draft.title, created=component.created, modified=draft.created, diff --git a/openedx/core/djangoapps/content_libraries/api/containers.py b/openedx/core/djangoapps/content_libraries/api/containers.py index dc13d9d43107..8730846c615d 100644 --- a/openedx/core/djangoapps/content_libraries/api/containers.py +++ b/openedx/core/djangoapps/content_libraries/api/containers.py @@ -26,6 +26,7 @@ from openedx_learning.api import authoring as authoring_api from openedx_learning.api.authoring_models import Container, ContainerVersion, Component from openedx.core.djangoapps.content_libraries.api.collections import library_collection_locator +from openedx.core.djangoapps.content_tagging.api import get_object_tag_counts from openedx.core.djangoapps.xblock.api import get_component_from_usage_key @@ -132,6 +133,7 @@ def from_container(cls, library_key, container: Container, associated_collection last_draft_created_by = draft.publishable_entity_version.created_by.username else: last_draft_created_by = "" + tags = get_object_tag_counts(str(container_key), count_implicit=True) return cls( container_key=container_key, @@ -148,6 +150,7 @@ def from_container(cls, library_key, container: Container, associated_collection last_draft_created=last_draft_created, last_draft_created_by=last_draft_created_by, has_unpublished_changes=authoring_api.contains_unpublished_changes(container.pk), + tags_count=tags.get(str(container_key), 0), collections=associated_collections or [], ) diff --git a/openedx/core/djangoapps/content_libraries/rest_api/containers.py b/openedx/core/djangoapps/content_libraries/rest_api/containers.py index 36118346daa6..d861cbceecfd 100644 --- a/openedx/core/djangoapps/content_libraries/rest_api/containers.py +++ b/openedx/core/djangoapps/content_libraries/rest_api/containers.py @@ -137,7 +137,10 @@ class LibraryContainerChildrenView(GenericAPIView): @convert_exceptions @swagger_auto_schema( - responses={200: list[serializers.LibraryXBlockMetadataSerializer]} + responses={ + 200: list[serializers.LibraryXBlockMetadataSerializer] + | list[serializers.LibraryContainerMetadataSerializer] + } ) def get(self, request, container_key: LibraryContainerLocator): """ diff --git a/openedx/core/djangoapps/content_libraries/tests/test_containers.py b/openedx/core/djangoapps/content_libraries/tests/test_containers.py index 26196d8394d8..90b0c717946c 100644 --- a/openedx/core/djangoapps/content_libraries/tests/test_containers.py +++ b/openedx/core/djangoapps/content_libraries/tests/test_containers.py @@ -11,6 +11,7 @@ from common.djangoapps.student.tests.factories import UserFactory from openedx.core.djangoapps.content_libraries import api from openedx.core.djangoapps.content_libraries.tests.base import ContentLibrariesRestApiTest +from openedx.core.djangoapps.content_tagging import api as tagging_api from openedx.core.djangolib.testing.utils import skip_unless_cms @@ -45,6 +46,13 @@ def setUp(self) -> None: ) self.lib_key = LibraryLocatorV2.from_string(self.lib["id"]) + self.taxonomy = tagging_api.create_taxonomy('New Taxonomy') + tagging_api.set_taxonomy_orgs(self.taxonomy, all_orgs=True) + tagging_api.add_tag_to_taxonomy(self.taxonomy, "one") + tagging_api.add_tag_to_taxonomy(self.taxonomy, "two") + tagging_api.add_tag_to_taxonomy(self.taxonomy, "three") + tagging_api.add_tag_to_taxonomy(self.taxonomy, "four") + # Create containers with freeze_time(self.create_date): # Unit @@ -543,6 +551,24 @@ def test_restore_containers(self, container_type) -> None: self.assertDictContainsEntries(new_container_data, expected_data) + @ddt.data( + "unit", + "subsection", + "section", + ) + def test_tag_containers(self, container_type) -> None: + container = getattr(self, container_type) + + assert container["tags_count"] == 0 + tagging_api.tag_object( + container["id"], + self.taxonomy, + ['one', 'three', 'four'], + ) + + new_container_data = self._get_container(container["id"]) + assert new_container_data["tags_count"] == 3 + def test_container_collections(self) -> None: # Create a collection col1 = api.create_library_collection(