Commit f30eca98 by swdanielli

serialize and deserialize more variables

parent 73e4d43e
...@@ -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
...@@ -973,7 +975,15 @@ class RecommenderXBlock(HelperXBlock): ...@@ -973,7 +975,15 @@ class RecommenderXBlock(HelperXBlock):
Serialize the XBlock to XML for exporting. Serialize the XBlock to XML for exporting.
""" """
node.tag = 'recommender' node.tag = 'recommender'
node.text = unicode(json.dumps(self.recommendations))
el = etree.SubElement(node, 'intro_enabled')
el.text = str(self.intro_enabled)
el = etree.SubElement(node, 'client_side_settings')
el.text = unicode(json.dumps(self.client_side_settings))
el = etree.SubElement(node, 'resources')
el.text = unicode(json.dumps(self.recommendations))
@staticmethod @staticmethod
def workbench_scenarios(): def workbench_scenarios():
...@@ -987,14 +997,24 @@ class RecommenderXBlock(HelperXBlock): ...@@ -987,14 +997,24 @@ 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>
[ <resources>
{"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": 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": 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": 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": 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": 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": 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": 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": 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": 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": 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>
<intro_enabled>True</intro_enabled>
<client_side_settings>
{
'DISABLE_DEV_UX': True,
'ENTRIES_PER_PAGE': 5,
'PAGE_SPAN': 2
}
</client_side_settings>
</recommender> </recommender>
</vertical_demo> </vertical_demo>
""" """
...@@ -1008,11 +1028,34 @@ class RecommenderXBlock(HelperXBlock): ...@@ -1008,11 +1028,34 @@ class RecommenderXBlock(HelperXBlock):
""" """
block = runtime.construct_xblock_from_class(cls, keys) block = runtime.construct_xblock_from_class(cls, keys)
lines = json.loads(node.text)
#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.")
el = root.getElementsByTagName('intro_enabled')
if el is None or len(el) != 1:
raise UpdateFromXmlError("XML content must contain an 'intro_enabled' element.")
if el.text == 'False':
block.intro_enabled = False
el = root.getElementsByTagName('client_side_settings')
if el is None or len(el) != 1:
raise UpdateFromXmlError("XML content must contain an 'client_side_settings' element.")
if el.text != '' and el.text is not None:
block.client_side_settings = json.loads(el.text)
el = root.getElementsByTagName('resources')
if el is None or len(el) != 1:
raise UpdateFromXmlError("XML content must contain an 'resources' element.")
if el.text != '' and el.text is not None:
lines = json.loads(el.text)
block.default_recommendations = data_structure_upgrade(lines)
return block return block
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