Example #1
0
def assign(request, batch_id):
    batch = Batch.objects.get(id=batch_id)

    if batch.is_open():
        error_message = "Questions cannot be assigned to open batch: %s." % batch.name.capitalize()
        messages.error(request, error_message)
        return HttpResponseRedirect("/batches/%s/questions/" % batch_id)

    batch_questions_form = BatchQuestionsForm(batch=batch)

    groups = HouseholdMemberGroup.objects.all().exclude(name='REGISTRATION GROUP')
    batch = Batch.objects.get(id=batch_id)
    if request.method == 'POST':
        batch_question_form = BatchQuestionsForm(batch=batch, data=request.POST, instance=batch)
        if batch_question_form.is_valid():
            batch_question_form.save()
            success_message = "Questions successfully assigned to batch: %s." % batch.name.capitalize()
            messages.success(request, success_message)
            return HttpResponseRedirect("/batches/%s/questions/" % batch_id)
    all_modules = QuestionModule.objects.all()
    context = {'batch_questions_form': batch_questions_form, 'batch': batch,
               'button_label': 'Save', 'id': 'assign-question-to-batch-form', 'groups': groups,
               'modules': all_modules}
    return render(request, 'batches/assign.html',
                  context)
Example #2
0
 def test_invalid(self):
     some_question_id_that_does_not_exist = 1234
     form_data = {'questions': some_question_id_that_does_not_exist}
     batch_questions_form = BatchQuestionsForm(batch=self.batch,
                                               data=form_data)
     self.assertFalse(batch_questions_form.is_valid())
     message = 'Enter a list of values.'
     self.assertEquals([message], batch_questions_form.errors['questions'])
Example #3
0
 def test_invalid(self):
     some_question_id_that_does_not_exist = 1234
     form_data = {
         'questions': some_question_id_that_does_not_exist
     }
     batch_questions_form = BatchQuestionsForm(batch=self.batch, data=form_data)
     self.assertFalse(batch_questions_form.is_valid())
     message = 'Enter a list of values.'
     self.assertEquals([message], batch_questions_form.errors['questions'])
Example #4
0
class BatchQuestionsFormTest(TestCase):
    def setUp(self):
        self.q1 = Question.objects.create(text="question1",
                                          answer_type=Question.NUMBER)
        self.q2 = Question.objects.create(text="question2",
                                          answer_type=Question.TEXT)
        self.form_data = {
            'questions': [self.q1.id, self.q2.id],
        }
        self.batch = Batch.objects.create(name="Batch A", order=2)
        self.batch_questions_form = BatchQuestionsForm(batch=self.batch,
                                                       data=self.form_data,
                                                       instance=self.batch)

    def test_valid(self):
        self.assertTrue(self.batch_questions_form.is_valid())

    def test_invalid(self):
        some_question_id_that_does_not_exist = 1234
        form_data = {'questions': some_question_id_that_does_not_exist}
        batch_questions_form = BatchQuestionsForm(batch=self.batch,
                                                  data=form_data)
        self.assertFalse(batch_questions_form.is_valid())
        message = 'Enter a list of values.'
        self.assertEquals([message], batch_questions_form.errors['questions'])

    def test_has_only_questions_not_subquestions_in_the_form(self):
        question1 = Question.objects.create(text="question1",
                                            answer_type=Question.NUMBER)
        question2 = Question.objects.create(text="question2",
                                            answer_type=Question.TEXT)
        sub_question1 = Question.objects.create(text="sub-question1",
                                                answer_type=Question.TEXT,
                                                parent=question1,
                                                subquestion=True)
        batch_form = BatchQuestionsForm(batch=self.batch)

        question_choices = batch_form.fields['questions']._queryset
        self.assertIn(question1, question_choices)
        self.assertIn(question2, question_choices)

        self.assertNotIn(sub_question1, question_choices)

    def test_should_save_order_when_form_save_is_called_with_questions(self):
        self.batch_questions_form.save()

        self.assertIn(self.batch, self.q1.batches.all())
        self.assertIn(self.batch, self.q2.batches.all())
        all_batch_question_orders = BatchQuestionOrder.objects.all()
        self.assertEqual(2, len(all_batch_question_orders))
        self.assertIsNotNone(self.q1.question_batch_order.all())
        self.assertIsNotNone(self.q2.question_batch_order.all())
Example #5
0
class BatchQuestionsFormTest(TestCase):
    def setUp(self):
        self.q1 = Question.objects.create(text="question1", answer_type=Question.NUMBER)
        self.q2 = Question.objects.create(text="question2", answer_type=Question.TEXT)
        self.form_data = {
            'questions': [self.q1.id, self.q2.id],
        }
        self.batch = Batch.objects.create(name="Batch A", order=2)
        self.batch_questions_form = BatchQuestionsForm(batch=self.batch, data=self.form_data, instance=self.batch)

    def test_valid(self):
        self.assertTrue(self.batch_questions_form.is_valid())

    def test_invalid(self):
        some_question_id_that_does_not_exist = 1234
        form_data = {
            'questions': some_question_id_that_does_not_exist
        }
        batch_questions_form = BatchQuestionsForm(batch=self.batch, data=form_data)
        self.assertFalse(batch_questions_form.is_valid())
        message = 'Enter a list of values.'
        self.assertEquals([message], batch_questions_form.errors['questions'])

    def test_has_only_questions_not_subquestions_in_the_form(self):
        question1 = Question.objects.create(text="question1", answer_type=Question.NUMBER)
        question2 = Question.objects.create(text="question2", answer_type=Question.TEXT)
        sub_question1 = Question.objects.create(text="sub-question1", answer_type=Question.TEXT, parent=question1,
                                                subquestion=True)
        batch_form = BatchQuestionsForm(batch=self.batch)

        question_choices = batch_form.fields['questions']._queryset
        self.assertIn(question1, question_choices)
        self.assertIn(question2, question_choices)

        self.assertNotIn(sub_question1, question_choices)

    def test_should_save_order_when_form_save_is_called_with_questions(self):
        self.batch_questions_form.save()

        self.assertIn(self.batch, self.q1.batches.all())
        self.assertIn(self.batch, self.q2.batches.all())
        all_batch_question_orders = BatchQuestionOrder.objects.all()
        self.assertEqual(2, len(all_batch_question_orders))
        self.assertIsNotNone(self.q1.question_batch_order.all())
        self.assertIsNotNone(self.q2.question_batch_order.all())