Commit 77e33c9b by swdanielli

Merge pull request #37 from pmitros/export_xml

parse xml
parents 9b07e807 0effe069
...@@ -4,7 +4,9 @@ students solving a given problem. ...@@ -4,7 +4,9 @@ students solving a given problem.
""" """
import hashlib import hashlib
import json import json
import lxml.etree as etree
import pkg_resources import pkg_resources
import xml.dom.minidom as md
from copy import deepcopy from copy import deepcopy
...@@ -968,6 +970,23 @@ class RecommenderXBlock(HelperXBlock): ...@@ -968,6 +970,23 @@ class RecommenderXBlock(HelperXBlock):
frag.initialize_js('RecommenderXBlock') frag.initialize_js('RecommenderXBlock')
return frag return frag
def add_xml_to_node(self, node):
"""
Serialize the XBlock to XML for exporting.
"""
node.tag = 'recommender'
node.set('intro_enabled', str(self.intro_enabled))
node.set('DISABLE_DEV_UX', str(self.client_side_settings['DISABLE_DEV_UX']))
node.set('ENTRIES_PER_PAGE', str(self.client_side_settings['ENTRIES_PER_PAGE']))
node.set('PAGE_SPAN', str(self.client_side_settings['PAGE_SPAN']))
el = etree.SubElement(node, 'resources')
## Note: The line below does not work in edX platform.
## We should figure out if the appropriate scope is available during import/export
## TODO: Talk to Cale
el.text = unicode(json.dumps(self.recommendations))
@staticmethod @staticmethod
def workbench_scenarios(): def workbench_scenarios():
""" """
...@@ -979,14 +998,19 @@ class RecommenderXBlock(HelperXBlock): ...@@ -979,14 +998,19 @@ class RecommenderXBlock(HelperXBlock):
""" """
<vertical_demo> <vertical_demo>
<html_demo><img class="question" src="http://people.csail.mit.edu/swli/edx/recommendation/img/pset.png"></img></html_demo> <html_demo><img class="question" src="http://people.csail.mit.edu/swli/edx/recommendation/img/pset.png"></img></html_demo>
<recommender> <recommender intro_enabled="True" DISABLE_DEV_UX="True" ENTRIES_PER_PAGE="3" PAGE_SPAN="1">
{"id": 1, "title": "Covalent bonding and periodic trends", "upvotes" : 15, "downvotes" : 5, "url" : "https://courses.edx.org/courses/MITx/3.091X/2013_Fall/courseware/SP13_Week_4/SP13_Periodic_Trends_and_Bonding/", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/videopage1.png", "descriptionText" : "short description for Covalent bonding and periodic trends"} <resources>
{"id": 2, "title": "Polar covalent bonds and electronegativity", "upvotes" : 10, "downvotes" : 7, "url" : "https://courses.edx.org/courses/MITx/3.091X/2013_Fall/courseware/SP13_Week_4/SP13_Covalent_Bonding/", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/videopage2.png", "descriptionText" : "short description for Polar covalent bonds and electronegativity"} [
{"id": 3, "title": "Longest wavelength able to to break a C-C bond ...", "upvotes" : 1230, "downvotes" : 7, "url" : "https://answers.yahoo.com/question/index?qid=20081112142253AA1kQN1", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/dispage1.png", "descriptionText" : "short description for Longest wavelength able to to break a C-C bond ..."} {"id": 1, "title": "Covalent bonding and periodic trends", "upvotes" : 15, "downvotes" : 5, "url" : "https://courses.edx.org/courses/MITx/3.091X/2013_Fall/courseware/SP13_Week_4/SP13_Periodic_Trends_and_Bonding/", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/videopage1.png", "descriptionText" : "short description for Covalent bonding and periodic trends"},
{"id": 4, "title": "Calculate the maximum wavelength of light for ...", "upvotes" : 10, "downvotes" : 3457, "url" : "https://answers.yahoo.com/question/index?qid=20100110115715AA6toHw", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/dispage2.png", "descriptionText" : "short description for Calculate the maximum wavelength of light for ..."} {"id": 2, "title": "Polar covalent bonds and electronegativity", "upvotes" : 10, "downvotes" : 7, "url" : "https://courses.edx.org/courses/MITx/3.091X/2013_Fall/courseware/SP13_Week_4/SP13_Covalent_Bonding/", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/videopage2.png", "descriptionText" : "short description for Polar covalent bonds and electronegativity"},
{"id": 5, "title": "Covalent bond - wave mechanical concept", "upvotes" : 10, "downvotes" : 7, "url" : "http://people.csail.mit.edu/swli/edx/recommendation/img/textbookpage1.png", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/textbookpage1.png", "descriptionText" : "short description for Covalent bond - wave mechanical concept"} {"id": 3, "title": "Longest wavelength able to to break a C-C bond ...", "upvotes" : 1230, "downvotes" : 7, "url" : "https://answers.yahoo.com/question/index?qid=20081112142253AA1kQN1", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/dispage1.png", "descriptionText" : "short description for Longest wavelength able to to break a C-C bond ..."},
{"id": 6, "title": "Covalent bond - Energetics of covalent bond", "upvotes" : 10, "downvotes" : 7, "url" : "http://people.csail.mit.edu/swli/edx/recommendation/img/textbookpage2.png", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/textbookpage2.png", "descriptionText" : "short description for Covalent bond - Energetics of covalent bond"} {"id": 4, "title": "Calculate the maximum wavelength of light for ...", "upvotes" : 10, "downvotes" : 3457, "url" : "https://answers.yahoo.com/question/index?qid=20100110115715AA6toHw", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/dispage2.png", "descriptionText" : "short description for Calculate the maximum wavelength of light for ..."},
{"id": 5, "title": "Covalent bond - wave mechanical concept", "upvotes" : 10, "downvotes" : 7, "url" : "http://people.csail.mit.edu/swli/edx/recommendation/img/textbookpage1.png", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/textbookpage1.png", "descriptionText" : "short description for Covalent bond - wave mechanical concept"},
{"id": 6, "title": "Covalent bond - Energetics of covalent bond", "upvotes" : 10, "downvotes" : 7, "url" : "http://people.csail.mit.edu/swli/edx/recommendation/img/textbookpage2.png", "description" : "http://people.csail.mit.edu/swli/edx/recommendation/img/textbookpage2.png", "descriptionText" : "short description for Covalent bond - Energetics of covalent bond"}
]
</resources>
</recommender> </recommender>
<recommender />
</vertical_demo> </vertical_demo>
""" """
), ),
...@@ -999,11 +1023,35 @@ class RecommenderXBlock(HelperXBlock): ...@@ -999,11 +1023,35 @@ class RecommenderXBlock(HelperXBlock):
""" """
block = runtime.construct_xblock_from_class(cls, keys) block = runtime.construct_xblock_from_class(cls, keys)
lines = []
for line in node.text.split('\n'):
line = line.strip()
if len(line) > 2:
lines.append(json.loads(line))
block.default_recommendations = data_structure_upgrade(lines) node_xml = etree.tostring(node, encoding='utf-8')
try:
dom = md.parseString(node_xml.encode('utf-8'))
except Exception:
raise UpdateFromXmlError("An error occurred while parsing the XML content.")
root = dom.documentElement
if root.tagName != 'recommender':
raise UpdateFromXmlError("XML content must contain an 'recommender' root element.")
block.intro_enabled = (root.getAttribute('intro_enabled').lower().strip() != "false")
block.client_side_settings['DISABLE_DEV_UX'] = (root.getAttribute('DISABLE_DEV_UX').lower().strip() != "false")
for tag in ['ENTRIES_PER_PAGE', 'PAGE_SPAN']:
if root.getAttribute(tag) is not None and len(root.getAttribute(tag)) > 0:
block.client_side_settings[tag] = int(root.getAttribute(tag))
el = root.getElementsByTagName('resources')
if el is not None and len(el) == 1:
text = el[0].childNodes[0].nodeValue
if text != '' and text is not None:
lines = json.loads(text)
block.default_recommendations = data_structure_upgrade(lines)
return block return block
class UpdateFromXmlError(Exception):
"""
Error occurred while deserializing the TaggedText XBlock content from XML.
"""
pass
\ No newline at end of file
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