feat: Serialize tag data in OLX for blocks (#34145)

This commit is contained in:
Yusuf Musleh
2024-02-14 21:30:23 +03:00
committed by GitHub
parent 5a36fa9163
commit 6e0bc66a77
10 changed files with 475 additions and 5 deletions

View File

@@ -7,6 +7,8 @@ import os
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 @@ class XBlockSerializer:
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")

View File

@@ -6,8 +6,11 @@ from xml.etree import ElementTree
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 @@ And it shouldn't matter if we use entities or numeric codes — Ω ≠
"""
EXPECTED_OPENASSESSMENT_OLX = """
<openassessment
submission_start="2001-01-01T00:00"
submission_due="2029-01-01T00:00"
text_response="required"
text_response_editor="text"
allow_multiple_files="True"
allow_latex="False"
prompts_type="text"
teams_enabled="False"
selected_teamset_id=""
show_rubric_during_response="False"
tags-v1="t1-export-id:%3Cspecial %22%27-%3D%2C. %7C%3D chars %3E tag,anotherTag,normal tag"
>
<title>Open Response Assessment</title>
<assessments>
<assessment name="student-training">
<example>
<answer>
<part>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.</part>
</answer>
<select criterion="Ideas" option="Fair"/>
<select criterion="Content" option="Good"/>
</example>
<example>
<answer>
<part>Replace this text with another sample response, and then specify the options that you would select for this response.</part>
</answer>
<select criterion="Ideas" option="Poor"/>
<select criterion="Content" option="Good"/>
</example>
</assessment>
<assessment name="peer-assessment" must_grade="5" must_be_graded_by="3" enable_flexible_grading="False" start="2001-01-01T00:00" due="2029-01-01T00:00"/>
<assessment name="self-assessment" start="2001-01-01T00:00" due="2029-01-01T00:00"/>
<assessment name="staff-assessment" start="2001-01-01T00:00" due="2029-01-01T00:00" required="False"/>
</assessments>
<prompts>
<prompt>
<description>
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.
</description>
</prompt>
</prompts>
<rubric>
<criterion feedback="optional">
<name>Ideas</name>
<label>Ideas</label>
<prompt>Determine if there is a unifying theme or main idea.</prompt>
<option points="0">
<name>Poor</name>
<label>Poor</label>
<explanation>Difficult for the reader to discern the main idea. Too brief or too repetitive to establish or maintain a focus.</explanation>
</option>
<option points="3">
<name>Fair</name>
<label>Fair</label>
<explanation>Presents a unifying theme or main idea, but may include minor tangents. Stays somewhat focused on topic and task.</explanation>
</option>
<option points="5">
<name>Good</name>
<label>Good</label>
<explanation>Presents a unifying theme or main idea without going off on tangents. Stays completely focused on topic and task.</explanation>
</option>
</criterion>
<criterion>
<name>Content</name>
<label>Content</label>
<prompt>Assess the content of the submission</prompt>
<option points="0">
<name>Poor</name>
<label>Poor</label>
<explanation>Includes little information with few or no details or unrelated details. Unsuccessful in attempts to explore any facets of the topic.</explanation>
</option>
<option points="1">
<name>Fair</name>
<label>Fair</label>
<explanation>Includes little information and few or no details. Explores only one or two facets of the topic.</explanation>
</option>
<option points="3">
<name>Good</name>
<label>Good</label>
<explanation>Includes sufficient information and supporting details. (Details may not be fully developed; ideas may be listed.) Explores some facets of the topic.</explanation>
</option>
<option points="5">
<name>Excellent</name>
<label>Excellent</label>
<explanation>Includes in-depth information and exceptional supporting details that are fully developed. Explores all facets of the topic.</explanation>
</option>
</criterion>
<feedbackprompt>
(Optional) What aspects of this response stood out to you? What did it do well? How could it be improved?
</feedbackprompt>
<feedback_default_text>
I think that this response...
</feedback_default_text>
</rubric>
</openassessment>
"""
@skip_unless_cms
class XBlockSerializationTestCase(SharedModuleStoreTestCase):
"""
@@ -79,6 +188,25 @@ class XBlockSerializationTestCase(SharedModuleStoreTestCase):
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="<special \"'-=,. |= chars > 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 @@ class XBlockSerializationTestCase(SharedModuleStoreTestCase):
</problem>
"""
)
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", "<special \"'-=,. |= chars > 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"""
<vertical
display_name="Tagged Unit"
url_name="Tagged_Unit"
tags-v1="{expected_serialized_tags}"
/>
"""
)
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", "<special \"'-=,. |= chars > 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"""
<html
url_name="Tagged_Non-default_HTML_Block"
display_name="Tagged Non-default HTML Block"
editor="raw"
use_latex_compiler="true"
tags-v1="{expected_serialized_tags}"
><![CDATA[
🍔
]]></html>
"""
)
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="<problem><optionresponse></optionresponse></problem>",
)
python_problem = BlockFactory.create(
parent_location=course.location,
category="problem",
display_name="Tagged Python Problem",
data='<problem>This uses python: <script type="text/python">...</script>...</problem>',
)
# Add a bunch of tags to the problem blocks
tagging_api.tag_object(
object_id=regular_problem.location,
taxonomy=self.taxonomy1,
tags=["normal tag", "<special \"'-=,. |= chars > 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", "<special \"'-=,. |= chars > 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"""
<problem
display_name="Tagged Problem No Python"
url_name="Tagged_Problem_No_Python"
max_attempts="3"
tags-v1="{expected_serialized_tags}"
>
<optionresponse></optionresponse>
</problem>
"""
)
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"""
<problem
display_name="Tagged Python Problem"
url_name="Tagged_Python_Problem"
tags-v1="{expected_serialized_tags}"
>
This uses python: <script type="text/python">...</script>...
</problem>
"""
)
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", "<special \"'-=,. |= chars > 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"""
<library_content
display_name="Tagged LC Block"
max_count="1"
source_library_id="{str(lib.location.library_key)}"
url_name="Tagged_LC_Block"
tags-v1="t1-export-id:%3Cspecial %22%27-%3D%2C. %7C%3D chars %3E tag,anotherTag,normal tag"
/>
"""
)
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", "<special \"'-=,. |= chars > 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,
"""
<video
youtube="1.00:3_yD_cEKoCk"
url_name="Tagged_Video_Block"
display_name="Tagged Video Block"
tags-v1="t1-export-id:%3Cspecial %22%27-%3D%2C. %7C%3D chars %3E tag,anotherTag,normal tag"
/>
"""
)
def test_tagged_openassessment_block(self):
"""
Test openassessment blocks that have applied tags
"""
course = CourseFactory.create(display_name='Tagged OpenAssessment Test course', run="TOTC")
openassessment_block = BlockFactory.create(
parent_location=course.location,
category="openassessment",
display_name="Tagged OpenAssessment Block",
)
# Add a tags to openassessment block
tagging_api.tag_object(
object_id=openassessment_block.location,
taxonomy=self.taxonomy1,
tags=["normal tag", "<special \"'-=,. |= chars > tag", "anotherTag"]
)
# Check that the tags data in included in the OLX and properly escaped
serialized = api.serialize_xblock_to_olx(openassessment_block)
self.assertXmlEqual(
serialized.olx_str,
EXPECTED_OPENASSESSMENT_OLX
)