Skip to content

Commit

Permalink
fix: store tags in TaggedBlockMixin.tags_v1 field
Browse files Browse the repository at this point in the history
Previously, we relied on the presence of the xml_attributes field, which
is added by the XMLMixin. However, XMLMixin is not part of the default
XBLOCK_MIXINS, so custom XBlocks weren't able to copy/paste tags.
  • Loading branch information
pomegranited committed Mar 1, 2024
1 parent c1fed5e commit eeb14d3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions cms/djangoapps/contentstore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ def _import_xml_node_to_parent(

from cms.lib.xblock.tagging.tagged_block_mixin import TaggedBlockMixin
if isinstance(new_xblock, TaggedBlockMixin):
new_xblock.read_tags_from_node(node)
new_xblock.add_tags_from_xml()

return new_xblock
Expand Down
26 changes: 22 additions & 4 deletions cms/lib/xblock/tagging/tagged_block_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@
"""
from urllib.parse import quote, unquote

from xblock.fields import Scope, String


def _(text):
"""
A noop underscore function that marks strings for extraction.
"""
return text


class TaggedBlockMixin:
"""
Mixin containing XML serializing and parsing functionality for tagged blocks
"""

tags_v1 = String(
display_name=_("Tags v1"),
name="tags-v1",
help=_("Serialized content tags"),
default="",
scope=Scope.settings
)

def studio_post_duplicate(self, store, source_item):
"""
Duplicates content tags from the source_item.
Expand All @@ -17,7 +35,7 @@ def studio_post_duplicate(self, store, source_item):

if hasattr(source_item, 'serialize_tag_data'):
tags = source_item.serialize_tag_data()
self.xml_attributes['tags-v1'] = tags
self.tags_v1 = tags
self.add_tags_from_xml()

def serialize_tag_data(self):
Expand Down Expand Up @@ -67,17 +85,17 @@ def read_tags_from_node(self, node):
Deserialize and read tag data from node
"""
if 'tags-v1' in node.attrib:
self.xml_attributes['tags-v1'] = str(node.attrib['tags-v1'])
self.tags_v1 = str(node.attrib['tags-v1'])

def add_tags_from_xml(self):
"""
Parse and add tag data from xml_attributes
Parse and add tag data from tags_v1 field
"""
# 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 set_object_tags

tag_data = self.xml_attributes.get('tags-v1', None) if hasattr(self, 'xml_attributes') else None
tag_data = self.tags_v1
if not tag_data:
return

Expand Down
5 changes: 5 additions & 0 deletions openedx/core/lib/xblock_serializer/block_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ def _serialize_normal_block(self, block) -> etree.Element:
with filesystem.open(file_path, 'rb') as fh:
data = fh.read()
self.static_files.append(StaticFile(name=unit_file.name, data=data, url=None))

# Serialize and add tag data if any
if isinstance(block, TaggedBlockMixin):
block.add_tags_to_node(olx_node)

if block.has_children:
self._serialize_children(block, olx_node)
return olx_node
Expand Down

0 comments on commit eeb14d3

Please sign in to comment.