Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def save(self, commit=True):
        """ Save the response object """
        # Recover an existing response from the database if any
        #  There is only one response by logged user.
        response = self._get_preexisting_response()

        if not self.survey.editable_answers and response is not None:
            return None

        if response is None:
            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 list(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])
                question = Question.objects.get(pk=q_id)
                answer = self._get_preexisting_answer(question)
                if answer is None:
                    answer = Answer(question=question)
                if question.type == Question.SELECT_IMAGE:
                    value, img_src = field_value.split(":", 1)
                    # TODO
                answer.body = field_value
                data["responses"].append((answer.question.id, answer.body))
                LOGGER.debug(
                    "Creating answer for question %d of type %s : %s",
                    q_id,
                    answer.question.type,
                    field_value,
                )
                answer.response = response
                answer.save()
        survey_completed.send(sender=Response, instance=response, data=data)
        return response
Ejemplo n.º 4
0
 def save(self, commit=True):
     """ Save the response object """
     # Recover an existing response from the database if any
     #  There is only one response by logged user.
     response = self._get_preexisting_response()
     if response is None:
         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.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])
             question = Question.objects.get(pk=q_id)
             answer = self._get_preexisting_answer(question)
             if answer is None:
                 answer = Answer(question=question)
             if question.type == Question.SELECT_IMAGE:
                 value, img_src = field_value.split(":", 1)
                 # TODO
             answer.body = field_value
             data['responses'].append((answer.question.id, answer.body))
             LOGGER.debug(
                 "Creating answer for question %d of type %s : %s", q_id,
                 answer.question.type, field_value
             )
             answer.response = response
             answer.save()
     survey_completed.send(sender=Response, instance=response, data=data)
     return response