Beispiel #1
0
    def save(self, commit=True, **kwargs):
        response = kwargs.pop('response')
        for field_name, field_value in self.cleaned_data.iteritems():
            if field_name.startswith("question_"):
                if field_value != '' and field_value != []:
                    print field_value
                    question_id = int(field_name.split("_")[1])
                    question = QuestionContainer.objects.get(pk=question_id)
                    if question.questiontype == QuestionType.objects.get(name='Single Textbox'):
                        answer = AnswerText(question=question)
                        answer.text = field_value
                    elif question.questiontype == QuestionType.objects.get(name='Multiple Choice'):
                        answer = AnswerChoice(question=question)
                        answer.choice = Choice.objects.get(question=question, text=field_value)
                    elif question.questiontype == QuestionType.objects.get(name='Dropdown'):
                        answer = AnswerChoice(question=question)
                        answer.choice = Choice.objects.get(question=question, text=field_value)
                    elif question.questiontype == QuestionType.objects.get(name='Checkbox'):
                        answer = AnswerCheck(question=question)
                        answer.response = response
                        answer.save()
                        for value in field_value:
                            answer.choices.add(Choice.objects.get(question=question, text=value))
                    elif question.questiontype == QuestionType.objects.get(name='Email'):
                        answer = AnswerText(question=question)
                        answer.text = field_value
                    elif question.questiontype == QuestionType.objects.get(name='Date'):
                        answer = AnswerText(question=question)
                        answer.text = field_value
                    elif question.questiontype == QuestionType.objects.get(name='TextArea'):
                        answer = AnswerText(question=question)
                        answer.text = field_value

                    answer.response = response
                    answer.save()
        return response