From 9a2bdaac86d889c9024add390a9977ca1488f9de Mon Sep 17 00:00:00 2001 From: Yusuf Musleh Date: Tue, 30 Jan 2024 18:56:06 +0300 Subject: [PATCH] feat: Serialize tag data in OLX for blocks --- cms/envs/common.py | 2 + cms/lib/xblock/tagging/tagged_block_mixin.py | 57 +++ .../lib/xblock_serializer/block_serializer.py | 6 + .../core/lib/xblock_serializer/test_api.py | 400 +++++++++++++++++- requirements/edx/base.txt | 2 +- requirements/edx/development.txt | 2 +- requirements/edx/doc.txt | 2 +- requirements/edx/testing.txt | 2 +- xmodule/tests/test_export.py | 1 + xmodule/xml_block.py | 6 + 10 files changed, 475 insertions(+), 5 deletions(-) create mode 100644 cms/lib/xblock/tagging/tagged_block_mixin.py diff --git a/cms/envs/common.py b/cms/envs/common.py index 8f974e73e521..c45de127143a 100644 --- a/cms/envs/common.py +++ b/cms/envs/common.py @@ -129,6 +129,7 @@ from lms.djangoapps.lms_xblock.mixin import LmsBlockMixin from cms.lib.xblock.authoring_mixin import AuthoringMixin +from cms.lib.xblock.tagging.tagged_block_mixin import TaggedBlockMixin from xmodule.modulestore.edit_info import EditInfoMixin from openedx.core.djangoapps.theming.helpers_dirs import ( get_themes_unchecked, @@ -975,6 +976,7 @@ XModuleMixin, EditInfoMixin, AuthoringMixin, + TaggedBlockMixin, ) XBLOCK_EXTRA_MIXINS = () diff --git a/cms/lib/xblock/tagging/tagged_block_mixin.py b/cms/lib/xblock/tagging/tagged_block_mixin.py new file mode 100644 index 000000000000..dba1a16c8856 --- /dev/null +++ b/cms/lib/xblock/tagging/tagged_block_mixin.py @@ -0,0 +1,57 @@ +# lint-amnesty, pylint: disable=missing-module-docstring +from urllib.parse import quote + + +class TaggedBlockMixin: + """ + Mixin containing XML serializing and parsing functionality for tagged blocks + """ + + def serialize_tag_data(self): + """ + Serialize block's tag data to include in the xml, escaping special characters + + Example tags: + LightCast Skills Taxonomy: ["Typing", "Microsoft Office"] + Open Canada Skills Taxonomy: ["MS Office", ""] + + Example serialized tags: + lightcast-skills:Typing,Microsoft Office;open-canada-skills:MS Office,%3Csome%3A%3B%2Cskill%2F%7C%3D%3E + """ + # 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) + + serialized_tags = [] + taxonomies_and_tags = {} + for tag in content_tags: + taxonomy_export_id = tag.taxonomy.export_id + + if not taxonomies_and_tags.get(taxonomy_export_id): + taxonomies_and_tags[taxonomy_export_id] = [] + + # Escape special characters in tag values, except spaces (%20) for better readability + escaped_tag = quote(tag.value).replace("%20", " ") + taxonomies_and_tags[taxonomy_export_id].append(escaped_tag) + + for taxonomy in taxonomies_and_tags: + merged_tags = ','.join(taxonomies_and_tags.get(taxonomy)) + serialized_tags.append(f"{taxonomy}:{merged_tags}") + + return ";".join(serialized_tags) + + def add_tags_to_node(self, node): + """ + Serialize and add tag data (if any) to node + """ + tag_data = self.serialize_tag_data() + if tag_data: + node.set('tags-v1', tag_data) + + def add_xml_to_node(self, node): + """ + Include the serialized tag data in XML when adding to node + """ + super().add_xml_to_node(node) + self.add_tags_to_node(node) diff --git a/openedx/core/lib/xblock_serializer/block_serializer.py b/openedx/core/lib/xblock_serializer/block_serializer.py index b91ca1594cd9..deb46b07cecb 100644 --- a/openedx/core/lib/xblock_serializer/block_serializer.py +++ b/openedx/core/lib/xblock_serializer/block_serializer.py @@ -7,6 +7,8 @@ from lxml import etree +from cms.lib.xblock.tagging.tagged_block_mixin import TaggedBlockMixin + from .data import StaticFile from . import utils @@ -113,6 +115,10 @@ def _serialize_html_block(self, block) -> etree.Element: if block.use_latex_compiler: olx_node.attrib["use_latex_compiler"] = "true" + # Serialize and add tag data if any + if isinstance(block, TaggedBlockMixin): + block.add_tags_to_node(olx_node) + # Escape any CDATA special chars escaped_block_data = block.data.replace("]]>", "]]>") olx_node.text = etree.CDATA("\n" + escaped_block_data + "\n") diff --git a/openedx/core/lib/xblock_serializer/test_api.py b/openedx/core/lib/xblock_serializer/test_api.py index c589b9a9e32f..19e48bd9eefe 100644 --- a/openedx/core/lib/xblock_serializer/test_api.py +++ b/openedx/core/lib/xblock_serializer/test_api.py @@ -6,8 +6,11 @@ from openedx.core.djangolib.testing.utils import skip_unless_cms from xmodule.modulestore.django import contentstore, modulestore from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase, upload_file_to_course -from xmodule.modulestore.tests.factories import BlockFactory, CourseFactory, ToyCourseFactory +from xmodule.modulestore.tests.factories import BlockFactory, CourseFactory, ToyCourseFactory, LibraryFactory from xmodule.util.sandboxing import DEFAULT_PYTHON_LIB_FILENAME +from openedx_tagging.core.tagging.models import Tag +from openedx.core.djangoapps.content_tagging.models import TaxonomyOrg +from openedx.core.djangoapps.content_tagging import api as tagging_api from . import api @@ -65,6 +68,112 @@ """ +EXPECTED_OPENASSESSMENT_OLX = """ + + Open Response Assessment + + + + + Replace this text with your own sample response for this assignment. Then, under Response Score to the right, select an option for each criterion. Learners practice performing peer assessments by assessing this response and comparing the options that they select in the rubric with the options that you specified. + + + + + + Replace this text with another sample response, and then specify the options that you would select for this response. + + + + + + + + + + + + Censorship in the Libraries + + 'All of us can think of a book that we hope none of our children or any other children have taken off the shelf. But if I have the right to remove that book from the shelf -- that work I abhor -- then you also have exactly the same right and so does everyone else. And then we have no books left on the shelf for any of us.' --Katherine Paterson, Author + + Write a persuasive essay to a newspaper reflecting your views on censorship in libraries. Do you believe that certain materials, such as books, music, movies, magazines, etc., should be removed from the shelves if they are found offensive? Support your position with convincing arguments from your own experience, observations, and/or reading. + + Read for conciseness, clarity of thought, and form. + + + + + + Ideas + + Determine if there is a unifying theme or main idea. + + + + + + Content + + Assess the content of the submission + + + + + + +(Optional) What aspects of this response stood out to you? What did it do well? How could it be improved? + + +I think that this response... + + + +""" + + @skip_unless_cms class XBlockSerializationTestCase(SharedModuleStoreTestCase): """ @@ -79,6 +188,25 @@ def setUpClass(cls): super().setUpClass() cls.course = ToyCourseFactory.create() + # Create taxonomies and tags for testing + cls.taxonomy1 = tagging_api.create_taxonomy(name="t1", enabled=True, export_id="t1-export-id") + TaxonomyOrg.objects.create( + taxonomy=cls.taxonomy1, + rel_type=TaxonomyOrg.RelType.OWNER, + ) + cls.taxonomy2 = tagging_api.create_taxonomy(name="t2", enabled=True, export_id="t2-export-id") + TaxonomyOrg.objects.create( + taxonomy=cls.taxonomy2, + rel_type=TaxonomyOrg.RelType.OWNER, + ) + root1 = Tag.objects.create(taxonomy=cls.taxonomy1, value="ROOT1") + root2 = Tag.objects.create(taxonomy=cls.taxonomy2, value="ROOT2") + Tag.objects.create(taxonomy=cls.taxonomy1, value="normal tag", parent=root1) + Tag.objects.create(taxonomy=cls.taxonomy1, value=" tag", parent=root1) + Tag.objects.create(taxonomy=cls.taxonomy1, value="anotherTag", parent=root1) + Tag.objects.create(taxonomy=cls.taxonomy2, value="tag", parent=root2) + Tag.objects.create(taxonomy=cls.taxonomy2, value="other tag", parent=root2) + def assertXmlEqual(self, xml_str_a: str, xml_str_b: str) -> bool: """ Assert that the given XML strings are equal, ignoring attribute order and some whitespace variations. """ self.assertEqual( @@ -287,3 +415,273 @@ def test_jsinput_extra_files(self): """ ) + + def test_tagged_units(self): + """ + Test units (vertical blocks) that have applied tags + """ + course = CourseFactory.create(display_name='Tagged Unit Course', run="TUC") + unit = BlockFactory( + parent_location=course.location, + category="vertical", + display_name="Tagged Unit", + ) + + # Add a bunch of tags + tagging_api.tag_object( + object_id=unit.location, + taxonomy=self.taxonomy1, + tags=["normal tag", " tag", "anotherTag"] + ) + tagging_api.tag_object( + object_id=unit.location, + taxonomy=self.taxonomy2, + tags=["tag", "other tag"] + ) + + # Check that the tags data in included in the OLX and properly escaped + serialized = api.serialize_xblock_to_olx(unit) + expected_serialized_tags = ( + "t1-export-id:%3Cspecial %22%27-%3D%2C. %7C%3D chars %3E tag,anotherTag,normal tag;" + "t2-export-id:other tag,tag" + ) + self.assertXmlEqual( + serialized.olx_str, + f""" + + """ + ) + + def test_tagged_html_block(self): + """ + Test html blocks that have applied tags + """ + course = CourseFactory.create(display_name='Tagged HTML Block Test Course', run="THBTC") + + # Create html block + html_block = BlockFactory.create( + parent_location=course.location, + category="html", + display_name="Tagged Non-default HTML Block", + editor="raw", + use_latex_compiler=True, + data="🍔", + ) + + # Add a bunch of tags + tagging_api.tag_object( + object_id=html_block.location, + taxonomy=self.taxonomy1, + tags=["normal tag", " tag", "anotherTag"] + ) + tagging_api.tag_object( + object_id=html_block.location, + taxonomy=self.taxonomy2, + tags=["tag", "other tag"] + ) + + # Check that the tags data in included in the OLX and properly escaped + serialized = api.serialize_xblock_to_olx(html_block) + expected_serialized_tags = ( + "t1-export-id:%3Cspecial %22%27-%3D%2C. %7C%3D chars %3E tag,anotherTag,normal tag;" + "t2-export-id:other tag,tag" + ) + self.assertXmlEqual( + serialized.olx_str, + f""" + + """ + ) + + def test_tagged_problem_blocks(self): + """ + Test regular problem block + problem block with dependancy that + have applied tags + """ + course = CourseFactory.create(display_name='Tagged Python Testing course', run="TPY") + upload_file_to_course( + course_key=course.id, + contentstore=contentstore(), + source_file='./common/test/data/uploads/python_lib.zip', + target_filename=DEFAULT_PYTHON_LIB_FILENAME, + ) + + regular_problem = BlockFactory.create( + parent_location=course.location, + category="problem", + display_name="Tagged Problem No Python", + max_attempts=3, + data="", + ) + + python_problem = BlockFactory.create( + parent_location=course.location, + category="problem", + display_name="Tagged Python Problem", + data='This uses python: ...', + ) + + # Add a bunch of tags to the problem blocks + tagging_api.tag_object( + object_id=regular_problem.location, + taxonomy=self.taxonomy1, + tags=["normal tag", " tag", "anotherTag"] + ) + tagging_api.tag_object( + object_id=regular_problem.location, + taxonomy=self.taxonomy2, + tags=["tag", "other tag"] + ) + tagging_api.tag_object( + object_id=python_problem.location, + taxonomy=self.taxonomy1, + tags=["normal tag", " tag", "anotherTag"] + ) + tagging_api.tag_object( + object_id=python_problem.location, + taxonomy=self.taxonomy2, + tags=["tag", "other tag"] + ) + + # Check that the tags data in included in the OLX and properly escaped + serialized = api.serialize_xblock_to_olx(regular_problem) + expected_serialized_tags = ( + "t1-export-id:%3Cspecial %22%27-%3D%2C. %7C%3D chars %3E tag,anotherTag,normal tag;" + "t2-export-id:other tag,tag" + ) + self.assertXmlEqual( + serialized.olx_str, + f""" + + + + """ + ) + + serialized = api.serialize_xblock_to_olx(python_problem) + expected_serialized_tags = ( + "t1-export-id:%3Cspecial %22%27-%3D%2C. %7C%3D chars %3E tag,anotherTag,normal tag;" + "t2-export-id:other tag,tag" + ) + self.assertXmlEqual( + serialized.olx_str, + f""" + + This uses python: ... + + """ + ) + + def test_tagged_library_content_blocks(self): + """ + Test library content blocks that have applied tags + """ + course = CourseFactory.create(display_name='Tagged Library Content course', run="TLCC") + lib = LibraryFactory() + lc_block = BlockFactory( + parent_location=course.location, + category="library_content", + source_library_id=str(lib.location.library_key), + display_name="Tagged LC Block", + max_count=1, + ) + + # Add a bunch of tags to the library content block + tagging_api.tag_object( + object_id=lc_block.location, + taxonomy=self.taxonomy1, + tags=["normal tag", " tag", "anotherTag"] + ) + + # Check that the tags data in included in the OLX and properly escaped + serialized = api.serialize_xblock_to_olx(lc_block) + self.assertXmlEqual( + serialized.olx_str, + f""" + + """ + ) + + def test_tagged_video_block(self): + """ + Test video blocks that have applied tags + """ + course = CourseFactory.create(display_name='Tagged Video Test course', run="TVTC") + video_block = BlockFactory.create( + parent_location=course.location, + category="video", + display_name="Tagged Video Block", + ) + + # Add tags to video block + tagging_api.tag_object( + object_id=video_block.location, + taxonomy=self.taxonomy1, + tags=["normal tag", " tag", "anotherTag"] + ) + + # Check that the tags data in included in the OLX and properly escaped + serialized = api.serialize_xblock_to_olx(video_block) + self.assertXmlEqual( + serialized.olx_str, + """ +