Commit d6511edd by Usman Khalid

Added property prompts to xblock.

TNL-708
parent 86c1e53a
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Data Conversion utility methods for handling ORA2 XBlock data transformations. Data Conversion utility methods for handling ORA2 XBlock data transformations.
""" """
import json
def convert_training_examples_list_to_dict(examples_list): def convert_training_examples_list_to_dict(examples_list):
...@@ -56,13 +57,42 @@ def convert_training_examples_list_to_dict(examples_list): ...@@ -56,13 +57,42 @@ def convert_training_examples_list_to_dict(examples_list):
] ]
def create_rubric_dict(prompt, criteria): def create_prompts_list(prompt_or_serialized_prompts):
"""
Construct a list of prompts.
Initially a block had a single prompt which was saved as a simple string.
In that case a new prompt dict is constructed from it.
Args:
prompt_or_serialized_prompts (unicode): A string which can either
be a single prompt text or json for a list of prompts.
Returns:
list of dict
"""
if prompt_or_serialized_prompts is None:
prompt_or_serialized_prompts = ''
try:
prompts = json.loads(prompt_or_serialized_prompts)
except ValueError:
prompts = [
{
'description': prompt_or_serialized_prompts,
}
]
return prompts
def create_rubric_dict(prompts, criteria):
""" """
Construct a serialized rubric model in the format expected Construct a serialized rubric model in the format expected
by the assessments app. by the assessments app.
Args: Args:
prompt (unicode): The rubric prompt. prompts (list of dict): The rubric prompts.
criteria (list of dict): The serialized rubric criteria. criteria (list of dict): The serialized rubric criteria.
Returns: Returns:
...@@ -70,7 +100,7 @@ def create_rubric_dict(prompt, criteria): ...@@ -70,7 +100,7 @@ def create_rubric_dict(prompt, criteria):
""" """
return { return {
"prompt": prompt, "prompts" : prompts,
"criteria": criteria "criteria": criteria
} }
......
...@@ -31,7 +31,7 @@ from openassessment.workflow.errors import AssessmentWorkflowError ...@@ -31,7 +31,7 @@ from openassessment.workflow.errors import AssessmentWorkflowError
from openassessment.xblock.student_training_mixin import StudentTrainingMixin from openassessment.xblock.student_training_mixin import StudentTrainingMixin
from openassessment.xblock.validation import validator from openassessment.xblock.validation import validator
from openassessment.xblock.resolve_dates import resolve_dates, DISTANT_PAST, DISTANT_FUTURE from openassessment.xblock.resolve_dates import resolve_dates, DISTANT_PAST, DISTANT_FUTURE
from openassessment.xblock.data_conversion import create_rubric_dict from openassessment.xblock.data_conversion import create_prompts_list, create_rubric_dict
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -144,7 +144,7 @@ class OpenAssessmentBlock( ...@@ -144,7 +144,7 @@ class OpenAssessmentBlock(
prompt = String( prompt = String(
default=DEFAULT_PROMPT, default=DEFAULT_PROMPT,
scope=Scope.content, scope=Scope.content,
help="A prompt to display to a student (plain text)." help="The prompts to display to a student."
) )
rubric_criteria = List( rubric_criteria = List(
...@@ -445,6 +445,40 @@ class OpenAssessmentBlock( ...@@ -445,6 +445,40 @@ class OpenAssessmentBlock(
return i18nService.ugettext return i18nService.ugettext
@property @property
def prompts(self):
"""
Return the prompts.
Initially a block had a single prompt which was saved as a simple
string in the prompt field. Now prompts are saved as a serialized
list of dicts in the same field. If prompt field contains valid json,
parse and return it. Otherwise, assume it is a simple string prompt
and return it in a list of dict.
Returns:
list of dict
"""
return create_prompts_list(self.prompt)
@prompts.setter
def prompts(self, value):
"""
Serialize the prompts and save to prompt field.
Args:
value (list of dict): The prompts to set.
"""
if value is None:
self.prompt = ''
elif len(value) == 1:
# For backwards compatibility. To be removed after all code
# is migrated to use prompts property instead of prompt field.
self.prompt = value[0]['description']
else:
self.prompt = json.dumps(value)
@property
def valid_assessments(self): def valid_assessments(self):
""" """
Return a list of assessment dictionaries that we recognize. Return a list of assessment dictionaries that we recognize.
......
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