fix for Answer.choice_str and modified answer format

EmailTemplateFixes
Ian Ward 2010-04-18 13:20:11 -04:00
parent 1e7b103220
commit db90300a95
2 changed files with 20 additions and 13 deletions

View File

@ -322,13 +322,30 @@ class Answer(models.Model):
def choice_str(self, secondary = False): def choice_str(self, secondary = False):
choice_string = "" choice_string = ""
choices = self.question.get_choices() choices = self.question.get_choices()
split_answers = self.answer.split()
for choice in choices: for choice in choices:
for split_answer in split_answers: for split_answer in self.split_answer():
if str(split_answer) == choice.value: if str(split_answer) == choice.value:
choice_string += str(choice.text) + " " choice_string += str(choice.text) + " "
def split_answer(self):
"""
Decode stored answer value and return as a list of choices.
Any freeform value will be returned in a list as the last item.
Calling code should be tolerant of freeform answers outside
of additional [] if data has been stored in plain text format
"""
try:
return json.loads(self.answer)
except ValueError:
# this was likely saved as plain text, try to guess what the
# value(s) were
if 'multiple' in self.question.type:
return self.answer.split('; ')
else:
return [self.answer]
def check_answer(self): def check_answer(self):
"Confirm that the supplied answer matches what we expect" "Confirm that the supplied answer matches what we expect"
return True return True

View File

@ -479,17 +479,7 @@ def export_csv(request, qid): # questionnaire_id
row = ["--"] * len(columns) row = ["--"] * len(columns)
row[0] = "%s/%s" % (subject.id, subject.state) row[0] = "%s/%s" % (subject.id, subject.state)
row[1] = runid row[1] = runid
try: ans = answer.split_answer()
ans = loads(answer.answer)
except ValueError:
# this was likely saved as plain text, try to guess what the
# value(s) were
if 'multiple' in answer.question.type:
ans = answer.answer.split('; ')
else:
# code below is tolerant of freeform answers outside of
# additional [], as they would be saved in json format
ans = [answer.answer]
for choice in ans: for choice in ans:
col = None col = None
if type(choice) == list: if type(choice) == list: