Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: store tags in TaggedBlockMixin.tags_v1 field #640

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Copy link
Member Author

@pomegranited pomegranited Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This step was missing for normal blocks, and so they weren't getting their tags serialized, only HTML blocks were.

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