Commit 9c616d1f by Piotr Mitros

Moving Studio view to use JSON instead of AST, and to have slightly cleaner validation logic

parent a541d654
import ast
import logging import logging
import operator import operator
import pkg_resources import pkg_resources
...@@ -95,7 +94,8 @@ class CrowdsourceHinter(XBlock): ...@@ -95,7 +94,8 @@ class CrowdsourceHinter(XBlock):
it in a course. It will allow one to define, for example, it in a course. It will allow one to define, for example,
which problem the hinter is for. which problem the hinter is for.
It is currently a dummy function -- we still need to build an authoring view. It is currently incomplete -- we still need to finish building the
authoring view.
""" """
html = self.resource_string("static/html/crowdsourcehinterstudio.html") html = self.resource_string("static/html/crowdsourcehinterstudio.html")
frag = Fragment(html) frag = Fragment(html)
...@@ -110,23 +110,24 @@ class CrowdsourceHinter(XBlock): ...@@ -110,23 +110,24 @@ class CrowdsourceHinter(XBlock):
""" """
Set intial hints, generic hints, and problem element from the Set intial hints, generic hints, and problem element from the
studio view. studio view.
The Studio view is not yet complete.
""" """
# TODO: json.loads, or better yet, yaml. initial_hints = json.loads(data['initial_hints'])
# ast.literal_eval follows Python syntax, which is not what we want. generic_hints = json.loads(data['generic_hints'])
initial = ast.literal_eval(str(data['initial_hints']))
generic = ast.literal_eval(str(data['generic_hints'])) if not isinstance(generic_hints, list):
# TODO: Break into validation and then setting: return {'success': False,
# if type(generic) is not list or type(initial) is not dict: 'error': 'Generic hints should be a list.'}
# return {'success': False} if not isinstance(initial_hints, dict):
# As a matter of style, we're better off using instanceof than return {'success': False,
# comparing type. 'error' : 'Initial hints should be a dict.'}
# ...
if type(generic) is list and type(initial) is dict: self.initial_hints = initial_hints
self.initial_hints = initial self.generic_hints = generic_hints
self.generic_hints = generic if len(str(data['element'])) > 1:
self.Element = str(data['element']) self.Element = str(data['element'])
return {'success': True} return {'success': True}
return {'success': False}
def student_view(self, context=None): def student_view(self, context=None):
""" """
...@@ -396,7 +397,7 @@ class CrowdsourceHinter(XBlock): ...@@ -396,7 +397,7 @@ class CrowdsourceHinter(XBlock):
A minimal working test for parse_xml A minimal working test for parse_xml
""" """
block = runtime.construct_xblock_from_class(cls, keys) block = runtime.construct_xblock_from_class(cls, keys)
xmlText = ast.literal_eval(str(node.text)) xmlText = json.loads(node.text)
if xmlText: if xmlText:
block.generic_hints.append(str(xmlText["generic_hints"])) block.generic_hints.append(str(xmlText["generic_hints"]))
block.initial_hints = copy.copy(xmlText["initial_hints"]) block.initial_hints = copy.copy(xmlText["initial_hints"])
......
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