Beispiel #1
0
class CreateQuestion(PermissionRequiredMixin, CreateView):
    permission_required = 'auth.can_edit_questionnaire'

    def __init__(self, **kwargs):
        super(CreateQuestion, self).__init__(**kwargs)
        self.template_name = 'questions/new.html'
        self.object = Question
        self.model = Question
        self.form_class = QuestionForm
        self.form = None

    def get_context_data(self, **kwargs):
        context = super(CreateQuestion, self).get_context_data(**kwargs)
        context.update({'btn_label': 'CREATE', 'id': 'id-new-question-form',
                        'cancel_url': reverse('list_questions_page'), 'title': 'New Question'})
        return context

    def post(self, request, *args, **kwargs):
        region = self.request.user.user_profile.region
        self.form = QuestionForm(region=region, data=request.POST)
        if self.form.is_valid():
            return self._form_valid()
        return self._form_invalid()

    def _form_valid(self):
        self.form.save()
        messages.success(self.request, "Question successfully created.")
        return HttpResponseRedirect(reverse('list_questions_page'))

    def _form_invalid(self):
        messages.error(self.request, "Question NOT created. See errors below.")
        context = {'form': self.form, 'btn_label': "CREATE", 'id': 'id-new-question-form',
                   'cancel_url': reverse('list_questions_page'), 'title': 'New Question'}
        return self.render_to_response(context)
Beispiel #2
0
class CreateQuestion(CreateView):
    def __init__(self, **kwargs):
        super(CreateQuestion, self).__init__(**kwargs)
        self.template_name = 'questions/new.html'
        self.object = Question
        self.model = Question
        self.form_class = QuestionForm
        self.form = None

    def get_context_data(self, **kwargs):
        context = super(CreateQuestion, self).get_context_data(**kwargs)
        context.update({'btn_label': 'CREATE', 'id': 'id-new-question-form'})
        return context

    def post(self, request, *args, **kwargs):
        self.form = QuestionForm(data=request.POST)
        if self.form.is_valid():
            return self._form_valid()
        return self._form_invalid()

    def _form_valid(self):
        self.form.save()
        messages.success(self.request, "Question successfully created.")
        return HttpResponseRedirect(reverse('list_questions_page'))

    def _form_invalid(self):
        messages.error(self.request, "Question NOT created. See errors below.")
        context = {'form': self.form, 'btn_label': "CREATE", 'id': 'id-new-question-form'}
        return self.render_to_response(context)
Beispiel #3
0
class CreateQuestion(CreateView):
    def __init__(self, **kwargs):
        super(CreateQuestion, self).__init__(**kwargs)
        self.template_name = 'questions/new.html'
        self.object = Question
        self.model = Question
        self.form_class = QuestionForm
        self.form = None

    def get_context_data(self, **kwargs):
        context = super(CreateQuestion, self).get_context_data(**kwargs)
        context.update({'btn_label': 'CREATE', 'id': 'id-new-question-form'})
        return context

    def post(self, request, *args, **kwargs):
        self.form = QuestionForm(data=request.POST)
        if self.form.is_valid():
            return self._form_valid()
        return self._form_invalid()

    def _form_valid(self):
        self.form.save()
        messages.success(self.request, "Question successfully created.")
        return HttpResponseRedirect(reverse('list_questions_page'))

    def _form_invalid(self):
        messages.error(self.request, "Question NOT created. See errors below.")
        context = {
            'form': self.form,
            'btn_label': "CREATE",
            'id': 'id-new-question-form'
        }
        return self.render_to_response(context)
Beispiel #4
0
    def test_editing_question_used_in_an_unpublished_questionnaire_updates_question(self):
        data = self.form_data.copy()
        history_form = QuestionForm(data=data, instance=self.question1)
        history_form.is_valid()

        history_form.save()

        questions = Question.objects.filter(UID=self.question1.UID)

        self.assertEqual(1, questions.count())
        self.failUnless(self.question1.id, questions[0].id)
Beispiel #5
0
    def test_editing_question_used_in_a_published_questionnaire_creates_a_duplicate_question(self):
        self.questionnaire.status = Questionnaire.PUBLISHED
        self.questionnaire.save()
        data = self.form_data.copy()
        history_form = QuestionForm(data=data, instance=self.question1)

        self.assertTrue(history_form.is_valid())

        history_form.save()

        questions = Question.objects.filter(UID=self.question1.UID)

        self.assertEqual(2, questions.count())
        self.failUnless(questions.filter(**data))
Beispiel #6
0
    def test_edit_removes_deleted_options_from_the_question(self):
        question = QuestionFactory(text='whats up?', answer_type=AnswerTypes.MULTI_CHOICE)

        QuestionOptionFactory(question=question, text='Yes', order=1)
        QuestionOptionFactory(question=question, text='NR', order=2)
        QuestionOptionFactory(question=question, text='No', order=3)

        changed_options = ['Yes', 'No', 'Maybe']

        data = {'text': 'changed text',
                'instructions': 'Some instructions',
                'export_label': 'blah',
                'answer_type': 'MultiChoice',
                'theme': self.theme.id,
                'options': changed_options}

        history_form = QuestionForm(data=data, instance=question)

        self.assertTrue(history_form.is_valid())
        edited_question = history_form.save()

        question1_options = edited_question.options.all()

        self.assertEqual(3, question1_options.count())
        [self.assertIn(question_option.text, changed_options) for question_option in question1_options]
Beispiel #7
0
 def test_increments_uid_of_existing_question_by_one_upon_save_given_instance(self):
     Question.objects.create(text='B. Number of cases tested',
                             instructions="Enter the total number of cases", UID='00001', answer_type='Number',
                             answer_sub_type="Integer")
     question_form = QuestionForm(data=self.form_data)
     question = question_form.save(commit=True)
     self.assertEqual('00002', question.UID)
Beispiel #8
0
    def test_editing_question_used_in_a_published_questionnaire_give_the_duplicate_question_old_question_order(self):
        self.questionnaire.status = Questionnaire.PUBLISHED
        self.questionnaire.save()
        self.question1.orders.create(order=1, question_group=self.parent_group)

        draft_questionnaire = Questionnaire.objects.create(name="draft qnaire",description="haha",
                                                           status=Questionnaire.DRAFT)
        section_1 = Section.objects.create(title="section 1", order=1, questionnaire=draft_questionnaire, name="ha")
        sub_section_1 = SubSection.objects.create(title="subs1", order=1, section=section_1)
        parent_group_d = QuestionGroup.objects.create(subsection=sub_section_1, name="group")
        parent_group_d.question.add(self.question1)
        self.question1.orders.create(order=2, question_group=parent_group_d)

        finalized_questionnaire = Questionnaire.objects.create(name="finalized qnaire",description="haha",
                                                           status=Questionnaire.FINALIZED)
        section_1_f = Section.objects.create(title="section 1", order=1, questionnaire=finalized_questionnaire, name="ha")
        sub_section_1_f = SubSection.objects.create(title="subs1", order=1, section=section_1_f)
        parent_group_f = QuestionGroup.objects.create(subsection=sub_section_1_f, name="group")
        parent_group_f.question.add(self.question1)
        self.question1.orders.create(order=3, question_group=parent_group_f)

        data = self.form_data.copy()
        history_form = QuestionForm(data=data, instance=self.question1)

        self.assertTrue(history_form.is_valid())
        duplicate_question = history_form.save()

        self.assertEqual(1, self.question1.orders.get(question_group=self.parent_group).order)
        self.assertEqual(0, duplicate_question.orders.filter(question_group=self.parent_group).count())

        self.assertEqual(2, duplicate_question.orders.get(question_group=parent_group_d).order)
        self.assertEqual(0, self.question1.orders.filter(question_group=parent_group_d).count())

        self.assertEqual(3, duplicate_question.orders.get(question_group=parent_group_f).order)
        self.assertEqual(0, self.question1.orders.filter(question_group=parent_group_f).count())
Beispiel #9
0
 def test_increments_uid_of_existing_question_by_one_upon_save_given_instance(
         self):
     Question.objects.create(text='B. Number of cases tested',
                             instructions="Enter the total number of cases",
                             UID='00001',
                             answer_type='Number')
     question_form = QuestionForm(data=self.form_data)
     question = question_form.save(commit=True)
     self.assertEqual('00002', question.UID)
Beispiel #10
0
 def test_assigns_region_on_save_if_region_is_given(self):
     region = Region.objects.create(name="ASEAN")
     form = {'text': 'How many kids were immunised this year?',
             'instructions': 'Some instructions',
             'short_instruction': 'short version',
             'export_label': 'blah',
             'answer_type': 'Text',
             'theme': self.theme.id}
     question_form = QuestionForm(region=region, data=form)
     question = question_form.save(commit=True)
     self.assertEqual(region, question.region)
Beispiel #11
0
 def test_assigns_region_on_save_if_region_is_given(self):
     global_admin, country, region = self.create_user_with_no_permissions()
     form = {'text': 'How many kids were immunised this year?',
             'instructions': 'Some instructions',
             'short_instruction': 'short version',
             'export_label': 'blah',
             'answer_type': 'Text',
             'theme': self.theme.id}
     question_form = QuestionForm(region=region, data=form)
     question = question_form.save(commit=True)
     self.assertEqual(region, question.region)
Beispiel #12
0
    def test_duplicate_question_maintains_region(self):
        self.questionnaire.status = Questionnaire.PUBLISHED
        self.questionnaire.save()
        region = Region.objects.create(name="AFR")
        self.question1.region = region
        self.question1.save()
        data = self.form_data.copy()
        history_form = QuestionForm(data=data, instance=self.question1)

        self.assertTrue(history_form.is_valid())
        duplicate_question = history_form.save()

        self.assertEqual(region, duplicate_question.region)
Beispiel #13
0
    def test_save_multichoice_question_saves_options(self):
        options = ['Yes', 'No', 'Maybe']
        form = {'text': 'How many kids were immunised this year?',
                'instructions': 'Some instructions',
                'short_instruction': 'short version',
                'export_label': 'blah',
                'answer_type': 'MultiChoice',
                'options': options}

        question_form = QuestionForm(data=form)
        question = question_form.save(commit=True)
        question_options = QuestionOption.objects.filter(question=question)

        self.assertEqual(3, question_options.count())
        [self.assertIn(question_option.text, options) for question_option in question_options]
Beispiel #14
0
    def test_save__multipleresponse_question_saves_listed_options(self):
        options = ['', 'Yes', 'No', 'Maybe']
        form = {'text': 'How many kids were immunised this year?',
                'instructions': 'Some instructions',
                'short_instruction': 'short version',
                'export_label': 'blah',
                'answer_type': AnswerTypes.MULTIPLE_RESPONSE,
                'options': options,
                'theme': self.theme.id}

        question_form = QuestionForm(data=form)
        question = question_form.save(commit=True)
        question_options = QuestionOption.objects.filter(question=question)

        self.assertEqual(3, question_options.count())
        [self.assertIn(question_option.text, ['Yes', 'No', 'Maybe']) for question_option in question_options]
Beispiel #15
0
    def test_multichoice_options_are_only_edited_in_the_duplicate_questions(self):
        self.questionnaire.status = Questionnaire.PUBLISHED
        self.questionnaire.save()
        self.question1.orders.create(order=1, question_group=self.parent_group)
        self.question1.answer_type = 'MultiChoice'
        self.question1.save()
        question1_options_texts = ["Yes", "No", "DK"]
        for text in question1_options_texts:
            self.question1.options.create(text=text)

        draft_questionnaire = Questionnaire.objects.create(name="draft qnaire", description="haha",
                                                           status=Questionnaire.DRAFT)
        section_1 = Section.objects.create(title="section 1", order=1, questionnaire=draft_questionnaire, name="ha")
        sub_section_1 = SubSection.objects.create(title="subs1", order=1, section=section_1)
        parent_group_d = QuestionGroup.objects.create(subsection=sub_section_1, name="group")
        parent_group_d.question.add(self.question1)
        self.question1.orders.create(order=2, question_group=parent_group_d)

        finalized_questionnaire = Questionnaire.objects.create(name="finalized qnaire", description="haha",
                                                               status=Questionnaire.FINALIZED)
        section_1_f = Section.objects.create(title="section 1", order=1, questionnaire=finalized_questionnaire,
                                             name="ha")
        sub_section_1_f = SubSection.objects.create(title="subs1", order=1, section=section_1_f)
        parent_group_f = QuestionGroup.objects.create(subsection=sub_section_1_f, name="group")
        parent_group_f.question.add(self.question1)
        self.question1.orders.create(order=3, question_group=parent_group_f)

        changed_options = ['', 'haha', 'hehe', 'hihi']
        data = {'text': 'changed text',
                'instructions': 'Some instructions',
                'export_label': 'blah',
                'answer_type': 'MultiChoice',
                'theme': self.theme.id,
                'options': changed_options}

        history_form = QuestionForm(data=data, instance=self.question1)

        self.assertTrue(history_form.is_valid())
        duplicate_question = history_form.save()

        question1_options = self.question1.options.all()
        self.assertEqual(3, question1_options.count())
        [self.assertIn(question_option.text, question1_options_texts) for question_option in question1_options]

        duplicate_question_options = duplicate_question.options.all()
        self.assertEqual(3, duplicate_question_options.count())
        [self.assertIn(question_option.text, changed_options) for question_option in duplicate_question_options]
Beispiel #16
0
    def test_save_multichoice_question_saves_options(self):
        options = ['Yes', 'No', 'Maybe']
        form = {
            'text': 'How many kids were immunised this year?',
            'instructions': 'Some instructions',
            'short_instruction': 'short version',
            'export_label': 'blah',
            'answer_type': 'MultiChoice',
            'options': options
        }

        question_form = QuestionForm(data=form)
        question = question_form.save(commit=True)
        question_options = QuestionOption.objects.filter(question=question)

        self.assertEqual(3, question_options.count())
        [
            self.assertIn(question_option.text, options)
            for question_option in question_options
        ]