Commit c5879dde by Braden MacDonald

Allow toggling show_title behaviour during upgrade

parent 80f90e37
...@@ -23,7 +23,7 @@ Test that we can upgrade from mentoring v1 to problem builder (v2). ...@@ -23,7 +23,7 @@ Test that we can upgrade from mentoring v1 to problem builder (v2).
import ddt import ddt
from lxml import etree from lxml import etree
from problem_builder import MentoringBlock from problem_builder import MentoringBlock
from problem_builder.v1.xml_changes import convert_xml_v1_to_v2 from problem_builder.v1.xml_changes import convert_xml_to_v2
import os.path import os.path
from StringIO import StringIO from StringIO import StringIO
import unittest import unittest
...@@ -66,7 +66,7 @@ class TestUpgrade(unittest.TestCase): ...@@ -66,7 +66,7 @@ class TestUpgrade(unittest.TestCase):
parser = etree.XMLParser(remove_blank_text=True) parser = etree.XMLParser(remove_blank_text=True)
xml_root = etree.parse(StringIO(old_block.xml_content), parser=parser).getroot() xml_root = etree.parse(StringIO(old_block.xml_content), parser=parser).getroot()
convert_xml_v1_to_v2(xml_root) convert_xml_to_v2(xml_root)
converted_block = self.create_block_from_node(xml_root) converted_block = self.create_block_from_node(xml_root)
with open("{}/{}_new.xml".format(xml_path, file_name)) as xmlfile: with open("{}/{}_new.xml".format(xml_path, file_name)) as xmlfile:
......
...@@ -25,6 +25,9 @@ optimized for editing in Studio. ...@@ -25,6 +25,9 @@ optimized for editing in Studio.
To run the script on devstack: To run the script on devstack:
SERVICE_VARIANT=cms DJANGO_SETTINGS_MODULE="cms.envs.devstack" python -m problem_builder.v1.upgrade [course id here] SERVICE_VARIANT=cms DJANGO_SETTINGS_MODULE="cms.envs.devstack" python -m problem_builder.v1.upgrade [course id here]
You can add "--version=v0" at the end of the command to upgrade from the oldest xblock-mentoring
instance which was kept on gsehub's GitHub account.
""" """
import logging import logging
from lxml import etree from lxml import etree
...@@ -35,10 +38,10 @@ import sys ...@@ -35,10 +38,10 @@ import sys
import warnings import warnings
from courseware.models import StudentModule from courseware.models import StudentModule
from .studio_xml_utils import studio_update_from_node from .studio_xml_utils import studio_update_from_node
from .xml_changes import convert_xml_v1_to_v2 from .xml_changes import convert_xml_to_v2
def upgrade_block(store, block): def upgrade_block(store, block, from_version="v1"):
""" """
Given a MentoringBlock "block" with old-style (v1) data in its "xml_content" field, parse Given a MentoringBlock "block" with old-style (v1) data in its "xml_content" field, parse
the XML and re-create the block with new-style (v2) children and settings. the XML and re-create the block with new-style (v2) children and settings.
...@@ -51,7 +54,7 @@ def upgrade_block(store, block): ...@@ -51,7 +54,7 @@ def upgrade_block(store, block):
assert root.tag == "mentoring" assert root.tag == "mentoring"
with warnings.catch_warnings(record=True) as warnings_caught: with warnings.catch_warnings(record=True) as warnings_caught:
warnings.simplefilter("always") warnings.simplefilter("always")
convert_xml_v1_to_v2(root) convert_xml_to_v2(root, from_version=from_version)
for warning in warnings_caught: for warning in warnings_caught:
print(u" ➔ {}".format(unicode(warning.message))) print(u" ➔ {}".format(unicode(warning.message)))
...@@ -140,6 +143,13 @@ if __name__ == '__main__': ...@@ -140,6 +143,13 @@ if __name__ == '__main__':
except (IndexError, InvalidKeyError): except (IndexError, InvalidKeyError):
sys.exit("Need a course ID argument like 'HarvardX/GSE1.1x/3T2014' or 'course-v1:HarvardX+B101+2015'") sys.exit("Need a course ID argument like 'HarvardX/GSE1.1x/3T2014' or 'course-v1:HarvardX+B101+2015'")
from_version = "v1"
if len(sys.argv) > 2:
if sys.argv[2] == "--version=v0":
from_version = "v0"
else:
sys.exit("invalid second argument: {}".format(' '.join(sys.argv[2:])))
store = modulestore() store = modulestore()
course = store.get_course(course_id) course = store.get_course(course_id)
if course is None: if course is None:
...@@ -196,6 +206,6 @@ if __name__ == '__main__': ...@@ -196,6 +206,6 @@ if __name__ == '__main__':
block = course.runtime.get_block(block_id) block = course.runtime.get_block(block_id)
print(u" ➔ Upgrading block {} of {} - \"{}\"".format(count, total, block.url_name)) print(u" ➔ Upgrading block {} of {} - \"{}\"".format(count, total, block.url_name))
count += 1 count += 1
upgrade_block(store, block) upgrade_block(store, block, from_version)
print(u" ➔ Complete.") print(u" ➔ Complete.")
...@@ -380,8 +380,21 @@ class CommaSeparatedListToJson(Change): ...@@ -380,8 +380,21 @@ class CommaSeparatedListToJson(Change):
self.node.attrib[attribute] = self._convert_value(self.node.attrib[attribute]) self.node.attrib[attribute] = self._convert_value(self.node.attrib[attribute])
class OptionalShowTitleDefaultToFalse(Change):
"""
In recent versions of mentoring, show_title defaults to True. In old versions there were no
titles. If upgrading an old version, show_title should be set False.
"""
@staticmethod
def applies_to(node):
return node.tag in ("pb-answer", "pb-mrq", "pb-mcq", "pb-rating") and ("show_title" not in node.attrib)
def apply(self):
self.node.attrib["show_title"] = "false"
# An *ordered* list of all XML schema changes: # An *ordered* list of all XML schema changes:
xml_changes = ( xml_changes = [
RenameMentoringTag, RenameMentoringTag,
PrefixTags, PrefixTags,
HideTitle, HideTitle,
...@@ -398,13 +411,20 @@ xml_changes = ( ...@@ -398,13 +411,20 @@ xml_changes = (
AlternatingHTMLToQuestions, AlternatingHTMLToQuestions,
SharedHeaderToHTML, SharedHeaderToHTML,
CommaSeparatedListToJson, CommaSeparatedListToJson,
) ]
def convert_xml_v1_to_v2(node): def convert_xml_to_v2(node, from_version="v1"):
""" """
Given an XML node, re-structure it as needed to convert it from v1 style to v2 style XML. Given an XML node, re-structure it as needed to convert it from v1 style to v2 style XML.
If from_version is set to "v0", then the "show_title" attribute on each question will be set
to False, for compatibility with old versions of the mentoring block that didn't have
question titles at all.
""" """
if from_version == "v0":
xml_changes.append(OptionalShowTitleDefaultToFalse)
# Apply each individual type of change one at a time: # Apply each individual type of change one at a time:
for change in xml_changes: for change in xml_changes:
# Walk the XML tree once and figure out all the changes we will need. # Walk the XML tree once and figure out all the changes we will need.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment