Skip to content

Commit

Permalink
fix: make serialize_tag_data a classmethod
Browse files Browse the repository at this point in the history
so we can serialize tags for blocks that aren't officially TaggedXBlocks
  • Loading branch information
pomegranited committed Mar 5, 2024
1 parent 77f8311 commit da1f12d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions cms/djangoapps/contentstore/views/tests/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,11 +1138,11 @@ def test_duplicate_tags(self):
# Refresh.
source_chapter = self.store.get_item(source_chapter.location)
expected_chapter_tags = 'A:one,two;B:four,three'
assert source_chapter.serialize_tag_data() == expected_chapter_tags
assert source_chapter.serialize_tag_data(source_chapter.location) == expected_chapter_tags

source_block = self.store.get_item(source_block.location)
expected_block_tags = 'A:two'
assert source_block.serialize_tag_data() == expected_block_tags
assert source_block.serialize_tag_data(source_block.location) == expected_block_tags

# Duplicate the chapter (and its children)
dupe_location = duplicate_block(
Expand All @@ -1155,8 +1155,8 @@ def test_duplicate_tags(self):
dupe_block = dupe_chapter.get_children()[0]

# Check that the duplicated blocks also duplicated tags
assert dupe_chapter.serialize_tag_data() == expected_chapter_tags
assert dupe_block.serialize_tag_data() == expected_block_tags
assert dupe_chapter.serialize_tag_data(dupe_chapter.location) == expected_chapter_tags
assert dupe_block.serialize_tag_data(dupe_block.location) == expected_block_tags


@ddt.ddt
Expand Down
9 changes: 5 additions & 4 deletions cms/lib/xblock/tagging/tagged_block_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ def tags_v1(self, tags: str) -> None:
"""
self._tags_v1 = tags

def serialize_tag_data(self):
@classmethod
def serialize_tag_data(cls, usage_id):
"""
Serialize block's tag data to include in the xml, escaping special characters
Serialize a block's tag data to include in the xml, escaping special characters
Example tags:
LightCast Skills Taxonomy: ["Typing", "Microsoft Office"]
Expand All @@ -45,7 +46,7 @@ def serialize_tag_data(self):
# This import is done here since we import and use TaggedBlockMixin in the cms settings, but the
# content_tagging app wouldn't have loaded yet, so importing it outside causes an error
from openedx.core.djangoapps.content_tagging.api import get_object_tags
content_tags = get_object_tags(self.scope_ids.usage_id)
content_tags = get_object_tags(usage_id)

serialized_tags = []
taxonomies_and_tags = {}
Expand All @@ -69,7 +70,7 @@ def add_tags_to_node(self, node):
"""
Serialize and add tag data (if any) to node
"""
tag_data = self.serialize_tag_data()
tag_data = self.serialize_tag_data(self.scope_ids.usage_id)
if tag_data:
node.set('tags-v1', tag_data)

Expand Down

0 comments on commit da1f12d

Please sign in to comment.