コード例 #1
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
コード例 #2
0
ファイル: forms.py プロジェクト: Ben2pop/SoftScores-Final
 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
コード例 #3
0
def pass_view(request, id):
    if "org_id" not in request.session:
        return HttpResponseRedirect(
            reverse('survey:start', kwargs={'survey_id': id}))

    survey = get_object_or_404(Survey, pk=id)
    if request.method == 'POST':
        org_id = request.session.pop('org_id')
        answer = Answer(user=request.user,
                        country=request.user.country,
                        organization_id=org_id,
                        survey_id=id)
        if request.session.get('region'):
            answer.region_id = request.session.pop('region')
        answer.body = request.POST.urlencode()
        answer.save()
        return HttpResponseRedirect(
            reverse('survey:thanks', kwargs={'survey_id': survey.slug}))

    questions = survey.questions.prefetch_related('options')
    return render(request, 'survey/pass.html', {'questions': questions})