Skip to content

Commit fa83b9d

Browse files
ChrisChVmarlonkeating
authored andcommitted
feat: Add tags_count to ContainerMetadata (#36883)
1 parent 9cfb55c commit fa83b9d

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

openedx/core/djangoapps/content_libraries/api/block_metadata.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ def from_component(cls, library_key, component, associated_collections=None):
4848
tags = get_object_tag_counts(str(usage_key), count_implicit=True)
4949

5050
return cls(
51-
usage_key=library_component_usage_key(
52-
library_key,
53-
component,
54-
),
51+
usage_key=usage_key,
5552
display_name=draft.title,
5653
created=component.created,
5754
modified=draft.created,

openedx/core/djangoapps/content_libraries/api/containers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from openedx_learning.api import authoring as authoring_api
2727
from openedx_learning.api.authoring_models import Container, ContainerVersion, Component
2828
from openedx.core.djangoapps.content_libraries.api.collections import library_collection_locator
29+
from openedx.core.djangoapps.content_tagging.api import get_object_tag_counts
2930

3031
from openedx.core.djangoapps.xblock.api import get_component_from_usage_key
3132

@@ -132,6 +133,7 @@ def from_container(cls, library_key, container: Container, associated_collection
132133
last_draft_created_by = draft.publishable_entity_version.created_by.username
133134
else:
134135
last_draft_created_by = ""
136+
tags = get_object_tag_counts(str(container_key), count_implicit=True)
135137

136138
return cls(
137139
container_key=container_key,
@@ -148,6 +150,7 @@ def from_container(cls, library_key, container: Container, associated_collection
148150
last_draft_created=last_draft_created,
149151
last_draft_created_by=last_draft_created_by,
150152
has_unpublished_changes=authoring_api.contains_unpublished_changes(container.pk),
153+
tags_count=tags.get(str(container_key), 0),
151154
collections=associated_collections or [],
152155
)
153156

openedx/core/djangoapps/content_libraries/rest_api/containers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ class LibraryContainerChildrenView(GenericAPIView):
137137

138138
@convert_exceptions
139139
@swagger_auto_schema(
140-
responses={200: list[serializers.LibraryXBlockMetadataSerializer]}
140+
responses={
141+
200: list[serializers.LibraryXBlockMetadataSerializer]
142+
| list[serializers.LibraryContainerMetadataSerializer]
143+
}
141144
)
142145
def get(self, request, container_key: LibraryContainerLocator):
143146
"""

openedx/core/djangoapps/content_libraries/tests/test_containers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from common.djangoapps.student.tests.factories import UserFactory
1212
from openedx.core.djangoapps.content_libraries import api
1313
from openedx.core.djangoapps.content_libraries.tests.base import ContentLibrariesRestApiTest
14+
from openedx.core.djangoapps.content_tagging import api as tagging_api
1415
from openedx.core.djangolib.testing.utils import skip_unless_cms
1516

1617

@@ -45,6 +46,13 @@ def setUp(self) -> None:
4546
)
4647
self.lib_key = LibraryLocatorV2.from_string(self.lib["id"])
4748

49+
self.taxonomy = tagging_api.create_taxonomy('New Taxonomy')
50+
tagging_api.set_taxonomy_orgs(self.taxonomy, all_orgs=True)
51+
tagging_api.add_tag_to_taxonomy(self.taxonomy, "one")
52+
tagging_api.add_tag_to_taxonomy(self.taxonomy, "two")
53+
tagging_api.add_tag_to_taxonomy(self.taxonomy, "three")
54+
tagging_api.add_tag_to_taxonomy(self.taxonomy, "four")
55+
4856
# Create containers
4957
with freeze_time(self.create_date):
5058
# Unit
@@ -543,6 +551,24 @@ def test_restore_containers(self, container_type) -> None:
543551

544552
self.assertDictContainsEntries(new_container_data, expected_data)
545553

554+
@ddt.data(
555+
"unit",
556+
"subsection",
557+
"section",
558+
)
559+
def test_tag_containers(self, container_type) -> None:
560+
container = getattr(self, container_type)
561+
562+
assert container["tags_count"] == 0
563+
tagging_api.tag_object(
564+
container["id"],
565+
self.taxonomy,
566+
['one', 'three', 'four'],
567+
)
568+
569+
new_container_data = self._get_container(container["id"])
570+
assert new_container_data["tags_count"] == 3
571+
546572
def test_container_collections(self) -> None:
547573
# Create a collection
548574
col1 = api.create_library_collection(

0 commit comments

Comments
 (0)