fix for Answer.choice_str and modified answer format
parent
1e7b103220
commit
db90300a95
|
@ -322,13 +322,30 @@ class Answer(models.Model):
|
|||
def choice_str(self, secondary = False):
|
||||
choice_string = ""
|
||||
choices = self.question.get_choices()
|
||||
split_answers = self.answer.split()
|
||||
|
||||
for choice in choices:
|
||||
for split_answer in split_answers:
|
||||
for split_answer in self.split_answer():
|
||||
if str(split_answer) == choice.value:
|
||||
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):
|
||||
"Confirm that the supplied answer matches what we expect"
|
||||
return True
|
||||
|
|
|
@ -479,17 +479,7 @@ def export_csv(request, qid): # questionnaire_id
|
|||
row = ["--"] * len(columns)
|
||||
row[0] = "%s/%s" % (subject.id, subject.state)
|
||||
row[1] = runid
|
||||
try:
|
||||
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]
|
||||
ans = answer.split_answer()
|
||||
for choice in ans:
|
||||
col = None
|
||||
if type(choice) == list:
|
||||
|
|
Loading…
Reference in New Issue