Commit 8a0dae01 by Diana Huang

Add in some sorting and validation of the options list

parent dfe438dc
...@@ -852,32 +852,57 @@ class RubricInput(InputTypeBase): ...@@ -852,32 +852,57 @@ class RubricInput(InputTypeBase):
# parse description # parse description
if descriptionxml.tag != 'description': if descriptionxml.tag != 'description':
raise Exception("[extract_category: expected description tag, got {0} instead".format(descriptionxml.tag)) raise Exception("[extract_category]: expected description tag, got {0} instead".format(descriptionxml.tag))
description = descriptionxml.text description = descriptionxml.text
cur_points = 0 cur_points = 0
options = [] options = []
autonumbering = True
# parse options # parse options
for option in optionsxml: for option in optionsxml:
if option.tag != 'option': if option.tag != 'option':
raise Exception("[extract_category: expected option tag, got {0} instead".format(option.tag)) raise Exception("[extract_category]: expected option tag, got {0} instead".format(option.tag))
else: else:
pointstr = option.get("points") pointstr = option.get("points")
if(pointstr): if pointstr:
autonumbering = False
# try to parse this into an int # try to parse this into an int
try: try:
points = int(pointstr) points = int(pointstr)
except ValueError: except ValueError:
raise Exception("[extract_category: expected int to have points, got {0} instead".format(pointstr)) raise Exception("[extract_category]: expected points to have int, got {0} instead".format(pointstr))
else: elif autonumbering:
# use the generated one # use the generated one if we're in the right mode
points = cur_points points = cur_points
cur_points = cur_points + 1 cur_points = cur_points + 1
else:
raise Exception("[extract_category]: missing points attribute. Cannot continue to auto-create points values after a points value is explicitly dfined.")
optiontext = option.text optiontext = option.text
options.append({'text': option.text, 'points': points}) options.append({'text': option.text, 'points': points})
# sort and check for duplicates
options = sorted(options, key=lambda option: option['points'])
validate_options(options)
return {'description': description, 'options': options} return {'description': description, 'options': options}
@staticmethod
def validate_options(options):
'''
Validates a set of options. This can and should be extended to filter out other bad edge cases
'''
if len(options) == 0:
raise Exception("[extract_category]: no options associated with this category")
if len(options) == 1:
return
prev = options[0]['points']
for option in options[1:]:
if prev == option['points']:
raise Exception("[extract_category]: found duplicate point values between two different options")
else:
prev = option['points']
registry.register(RubricInput) registry.register(RubricInput)
......
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