Skip to content

Commit

Permalink
test: Added for tags_count in library block response
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Sep 24, 2024
1 parent 72fbf10 commit cd75928
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
18 changes: 12 additions & 6 deletions openedx/core/djangoapps/content_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,12 @@ class LibraryXBlockMetadata:
created = attr.ib(default=None, type=datetime)

@classmethod
def from_component(cls, library_key, component):
def from_component(cls, library_key, component, load_tags_count=False):
"""
Construct a LibraryXBlockMetadata from a Component object.
Set `load_tags_count` as True to run query to get tags. This variable is used
to avoid running many unnecessary queries when obtaining all the components of a library.
"""
last_publish_log = component.versioning.last_publish_log

Expand All @@ -241,12 +244,14 @@ def from_component(cls, library_key, component):
last_draft_created = draft.created if draft else None
last_draft_created_by = draft.publishable_entity_version.created_by if draft else None

# Build 'lib:' component usage_key from component slug and library_key
component_key = str(component).replace('xblock.v1:', f'{str(library_key)}:').replace('lib:', 'lb:')
components_tags = _get_library_component_tags_count(library_key)
tags_count = 0
if component_key in components_tags:
tags_count = components_tags[component_key]
if load_tags_count:
# Build 'lib:' component usage_key from component slug and library_key
component_key = str(component).replace('xblock.v1:', f'{str(library_key)}:').replace('lib:', 'lb:')
components_tags = _get_library_component_tags_count(library_key)

if component_key in components_tags:
tags_count = components_tags[component_key]

return cls(
usage_key=LibraryUsageLocatorV2(
Expand Down Expand Up @@ -731,6 +736,7 @@ def get_library_block(usage_key) -> LibraryXBlockMetadata:
xblock_metadata = LibraryXBlockMetadata.from_component(
library_key=usage_key.context_key,
component=component,
load_tags_count=True,
)
return xblock_metadata

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
URL_BLOCK_XBLOCK_HANDLER,
)
from openedx.core.djangoapps.content_libraries.constants import VIDEO, COMPLEX, PROBLEM, CC_4_BY
from openedx.core.djangoapps.content_tagging import api as tagging_api
from openedx.core.djangoapps.xblock import api as xblock_api
from openedx.core.djangolib.testing.utils import skip_unless_cms
from common.djangoapps.student.tests.factories import UserFactory
Expand Down Expand Up @@ -284,6 +285,7 @@ def test_library_blocks(self):
"published_by": None,
"last_draft_created": create_date.isoformat().replace('+00:00', 'Z'),
"last_draft_created_by": "Bob",
'tags_count': 0,
})
block_id = block_data["id"]
# Confirm that the result contains a definition key, but don't check its value,
Expand Down Expand Up @@ -346,6 +348,26 @@ def test_library_blocks(self):
assert problem_get_response.status_code == 200
assert 'You have used 0 of 5 attempts' in problem_get_response.content.decode('utf-8')

# Now verify tags count of the block:
taxonomy = tagging_api.create_taxonomy(name="A", export_id="A")
tagging_api.set_taxonomy_orgs(taxonomy, all_orgs=True)
tagging_api.add_tag_to_taxonomy(taxonomy, "one")
tagging_api.add_tag_to_taxonomy(taxonomy, "two")
tagging_api.tag_object(block_id, taxonomy, ["one", "two"])

new_block_data = self._get_library_block(block_id)
self.assertDictContainsEntries(new_block_data, {
"id": "lb:CL-TEST:téstlꜟط:problem:ࠒröblæm1",
"display_name": "New Multi Choice Question",
"block_type": "problem",
"has_unpublished_changes": True,
"last_published": publish_date.isoformat().replace('+00:00', 'Z'),
"published_by": 'Bob',
"last_draft_created": update_date.isoformat().replace('+00:00', 'Z'),
"last_draft_created_by": None,
'tags_count': 2,
})

# Now delete the block:
assert self._get_library(lib_id)['has_unpublished_deletes'] is False
self._delete_library_block(block_id)
Expand Down Expand Up @@ -385,6 +407,7 @@ def test_library_blocks_studio_view(self):
"published_by": None,
"last_draft_created": create_date.isoformat().replace('+00:00', 'Z'),
"last_draft_created_by": "Bob",
'tags_count': 0,
})
block_id = block_data["id"]

Expand Down

0 comments on commit cd75928

Please sign in to comment.