Example #1
0
    def is_valid_question(self, reportbyproj_pk, question_pk, answer_pk):
        question = None
        try:
            question = QuestionModel.objects.get(pk=question_pk)
        except:
            return [{
                'pk': question_pk,
                'number': 'Undefined',
                'problem': 'Question object doesn\'t exist'
            }]

        permission = get_question_permission(question)
        if permission == 1:
            if answer_pk is not None:
                try:
                    answer = Answer.objects.get(pk=answer_pk)
                    if not answer.text:
                        return [{
                            'pk': question_pk,
                            'number': question.order,
                            'problem': 'Blank answer'
                        }]
                except:
                    return [{
                        'pk': question_pk,
                        'number': question.order,
                        'problem': 'No answer'
                    }]
            else:
                return [{
                    'pk': question_pk,
                    'number': question.order,
                    'problem': 'No answer'
                }]
Example #2
0
    def get(self, request, *args, **kwargs):
        """
            TODO: docstring
        """
        self.request = request
        self.form_number = random.randint(self.start_number, self.end_number)
        reportbyproj, self.question, self.answer = get_reportbyproj_question_answer(
            kwargs['report_pk'], kwargs['question_pk'], kwargs['answer_pk'])
        form = self.get_form(instance=self.answer)
        parameters = {
            'name': self.name,
            'form': form,
            'question': self.question,
            'report': reportbyproj,
            'question_number': self.question.order,
            'answer': self.answer,
            'form_number': str(self.form_number),
            'observations': self.get_observations(request, args, kwargs),
            'required': get_question_permission(self.question)
        }
        additional = self.additional_template_parameters(**parameters)
        if additional:
            parameters.update(additional)

        return render(request, self.template_name, parameters)
Example #3
0
    def post(self, request, *args, **kwargs):
        """
            TODO: docstring
        """
        self.request = request
        self.form_number = random.randint(self.start_number, self.end_number)
        reportbyproj, self.question, self.answer = get_reportbyproj_question_answer(
            kwargs['report_pk'], kwargs['question_pk'], kwargs['answer_pk'])
        if self.answer is None:
            self.answer = Answer()
        self.answer.question = self.question
        self.answer.report = reportbyproj
        self.answer.user = request.user
        self.answer.text = ''
        self.answer.display_text = '\n'

        form = self.get_form(request.POST, instance=self.answer)
        if form.is_valid():
            answer = form.save(False)
            answer.report = reportbyproj
            answer.question = self.question
            answer.user = request.user
            self.answer = answer
            self.save(answer)
            return HttpResponse(str(answer.pk))

        parameters = {
            'name': self.name,
            'form': form,
            'question': self.question,
            'report': reportbyproj,
            'question_number': self.question.order,
            'answer': self.answer,
            'form_number': str(self.form_number),
            'observations': self.get_observations(request, args, kwargs),
            'required': get_question_permission(self.question)
        }
        additional = self.additional_template_parameters(**parameters)
        if additional:
            parameters.update(additional)
        return render(request, self.template_name, parameters)
Example #4
0
    def is_valid_question(self, reportbyproj_pk, question_pk, answer_pk):
        '''
            Checks if the question has been answered correctly
            Used when checking if the report is complete before to submit for revision or when the user request the report status

            In the case that the question has not been answered correctly or answered at all, returns the error info
        '''
        question = None
        try:
            question = QuestionModel.objects.get(pk=question_pk)
        except:
            return [{
                'pk': question_pk,
                'number': 'Undefined',
                'problem': 'Question object doesn\'t exist'
            }]

        permission = get_question_permission(question)
        if permission == 1:
            if answer_pk is not None:
                try:
                    answer = Answer.objects.get(pk=answer_pk)
                    if not answer.text:
                        return [{
                            'pk': question_pk,
                            'number': question.order,
                            'problem': 'Blank answer'
                        }]
                except:
                    return [{
                        'pk': question_pk,
                        'number': question.order,
                        'problem': 'No answer'
                    }]
            else:
                return [{
                    'pk': question_pk,
                    'number': question.order,
                    'problem': 'No answer'
                }]
Example #5
0
 def clean_text(self):
     text = self.cleaned_data['text']
     required = get_question_permission(self.instance.question)
     if required == 1 and not text:
         raise ValidationError(_('This field is required'), code='required')
     return text
Example #6
0
    def post(self, request, *args, **kwargs):
        '''
            Handles the requests using the *POST* HTTP verb triggered by the responsable
            The context passed to the template contains (at least) the next elements:
               - name
               - form
               - question
               - question_number
               - answer
               - reportbyproj
               - form_number
               - observations
               - requirement
            Returns the answer pk when the POST request is processed correctly
            Return the rendered template (with errors) when the form presents errors
       '''
        self.request = request
        self.form_number = random.randint(self.start_number, self.end_number)
        self.question = get_object_or_404(QuestionModel,
                                          pk=kwargs['question_pk'])
        reportbyproj = get_object_or_404(ReportByProject,
                                         report__pk=kwargs['report_pk'])
        if Answer.objects.filter(report=reportbyproj,
                                 question=self.question).exists():
            self.answer = Answer.objects.get(report=reportbyproj,
                                             question=self.question)

        if self.answer is None:
            self.answer = Answer()

        self.answer.question = self.question
        self.answer.user = request.user

        self.answer.text = ''
        self.answer.display_text = '\n'

        form = self.get_form(post=request.POST, instance=self.answer)

        if form.is_valid():
            answer = form.save(False)
            answer.report = reportbyproj
            answer.question = self.question
            answer.user = request.user
            self.answer = answer
            self.save(answer)
            return HttpResponse(str(answer.pk))

        parameters = {
            'name': self.name,
            'form': form,
            'question': self.question,
            'report': reportbyproj,
            'question_number': self.question.order,
            'answer': self.answer,
            'form_number': str(self.form_number),
            # 'observations': self.get_observations(request, args, kwargs),
            'required': get_question_permission(self.question)
        }
        extra = self.additional_template_parameters(**parameters)
        if extra:
            parameters.update(extra)
        return render(request, self.template_name, parameters)