def save(self, response): # response = super(ResponseWizardFrom, self).save(commit=False) for field_name, field_value in self.cleaned_data.iteritems(): if field_name.startswith("question_"): # warning: this way of extracting the id is very fragile and # entirely dependent on the way the question_id is encoded in the # field name in the __init__ method of this form class. q_id = int(field_name.split("_")[1]) q = Question.objects.get(pk=q_id) if q.question_type == Question.TEXT: a = AnswerText(question=q) a.body = field_value elif q.question_type == Question.RADIO: a = AnswerRadio(question=q) a.body = field_value elif q.question_type == Question.SELECT: a = AnswerSelect(question=q) a.body = field_value elif q.question_type == Question.SELECT_MULTIPLE: a = AnswerSelectMultiple(question=q) a.body = field_value elif q.question_type == Question.INTEGER: a = AnswerInteger(question=q) a.body = field_value # print "creating answer to question %d of type %s" % (q_id, a.question.question_type) # print a.question.text # print 'answer value:' # print field_value a.response = response a.save()
def save(self, commit=True): # save the response object response = super(ResponseForm, self).save(commit=False) response.survey = self.survey response.interview_uuid = self.uuid if self.user.is_authenticated(): response.user = self.user response.save() # response "raw" data as dict (for signal) data = { 'survey_id': response.survey.id, 'interview_uuid': response.interview_uuid, 'responses': [] } # create an answer object for each question and associate it with this # response. for field_name, field_value in self.cleaned_data.iteritems(): if field_name.startswith("question_"): # warning: this way of extracting the id is very fragile and # entirely dependent on the way the question_id is encoded in the # field name in the __init__ method of this form class. q_id = int(field_name.split("_")[1]) q = Question.objects.get(pk=q_id) if q.question_type == Question.TEXT or q.question_type == Question.SHORT_TEXT: a = AnswerText(question=q) a.body = field_value elif q.question_type == Question.RADIO: a = AnswerRadio(question=q) a.body = field_value elif q.question_type == Question.SELECT: a = AnswerSelect(question=q) a.body = field_value elif q.question_type == Question.SELECT_IMAGE: a = AnswerSelect(question=q) value, img_src = field_value.split(":", 1) a.body = value elif q.question_type == Question.SELECT_MULTIPLE: a = AnswerSelectMultiple(question=q) a.body = field_value elif q.question_type == Question.INTEGER: a = AnswerInteger(question=q) a.body = field_value data['responses'].append((a.question.id, a.body)) logging.debug("creating answer to question %d of type %s" % (q_id, a.question.question_type)) logging.debug(a.question.text) logging.debug('answer value:') logging.debug(field_value) a.response = response a.save() survey_completed.send(sender=Response, instance=response, data=data) return response
def save(self, commit=True): # save the response object response = super(ResponseForm, self).save(commit=False) response.survey = self.survey response.interview_uuid = self.uuid response.save() # create an answer object for each question and associate it with this # response. for field_name, field_value in self.cleaned_data.items(): if field_name.startswith("question_"): # warning: this way of extracting the id is very fragile and # entirely dependent on the way the question_id is encoded in the # field name in the __init__ method of this form class. q_id = int(field_name.split("_")[1]) q = Question.objects.get(pk=q_id) if q.question_type == Question.TEXT: a = AnswerText(question=q) a.answer = field_value elif q.question_type == Question.RADIO: a = AnswerRadio(question=q) a.answer = field_value elif q.question_type == Question.SELECT: a = AnswerSelect(question=q) a.answer = field_value elif q.question_type == Question.SELECT_MULTIPLE: a = AnswerSelectMultiple(question=q) a.answer = ', '.join([str(i) for i in field_value]) elif q.question_type == Question.INTEGER: a = AnswerInteger(question=q) a.answer = field_value elif q.question_type == Question.RANGE: a = AnswerText(question=q) a.answer = field_value elif q.question_type == Question.IMAGE: a = AnswerRadio(question=q) a.answer = field_value a.response = response a.save() return response