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_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 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)
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())
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())
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)
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 assign(request, qset_id): batch = QuestionSet.get(id=qset_id) if batch.interviews.count(): error_message = "Questions cannot be assigned \ interviews has already been conducted: %s." % \ batch.name.capitalize() messages.error(request, error_message) return HttpResponseRedirect( reverse('qset_questions_page', args=(batch.pk, ))) if request.method == 'POST': data = dict(request.POST) last_question = batch.last_question_inline() lib_questions = QuestionTemplate.objects.filter( identifier__in=data.get('identifier', '')) if lib_questions: for lib_question in lib_questions: question = Question.objects.create( identifier=lib_question.identifier, text=lib_question.text, answer_type=lib_question.answer_type, qset=batch, ) # assign the options for option in lib_question.options.all(): QuestionOption.objects.create(question=question, text=option.text, order=option.order) if last_question: QuestionFlow.objects.create(question=last_question, next_question=question) else: batch.start_question = question batch.save() last_question = question #batch_questions_form = BatchQuestionsForm(batch=batch,\ #\data=request.POST, instance=batch) success_message = "Questions successfully assigned to %s: %s." % ( batch.verbose_name(), batch.name.capitalize()) messages.success(request, success_message) return HttpResponseRedirect( reverse('qset_questions_page', args=(batch.pk, ))) used_identifiers = [ question.identifier for question in batch.questions.all() ] library_questions = QuestionTemplate.objects.exclude( identifier__in=used_identifiers).order_by('identifier') question_filter_form = QuestionFilterForm() # library_questions = question_filter_form.filter(library_questions) breadcrumbs = Question.edit_breadcrumbs(qset=batch) page_name = '' if breadcrumbs: if breadcrumbs[0][0] == 'Listing Form': page_name = 'Listing' else: page_name = 'Batch' request.breadcrumbs(breadcrumbs) context = { 'batch_questions_form': unicode(BatchQuestionsForm()), 'batch': batch, 'button_label': 'Save', 'id': 'assign-question-to-batch-form', 'library_questions': library_questions, 'question_filter_form': question_filter_form, 'page_name': page_name, 'redirect_url': '/qsets/%s/questions/' % qset_id } return render(request, 'set_questions/assign.html', context)
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) batch = Batch.objects.get(id=batch_id) groups = HouseholdMemberGroup.objects.all() groups.exists() modules = QuestionModule.objects.all() modules.exists() if request.method == 'POST': data = dict(request.POST) last_question = batch.last_question_inline() lib_questions = QuestionTemplate.objects.filter( identifier__in=data.get('identifier', '')) if lib_questions: for lib_question in lib_questions: question = Question.objects.create( identifier=lib_question.identifier, text=lib_question.text, answer_type=lib_question.answer_type, group=lib_question.group, module=lib_question.module, batch=batch) #assign the options for option in lib_question.options.all(): QuestionOption.objects.create(question=question, text=option.text, order=option.order) if last_question: QuestionFlow.objects.create(question=last_question, next_question=question) else: batch.start_question = question batch.save() last_question = question # batch_questions_form = BatchQuestionsForm(batch=batch, data=request.POST, instance=batch) success_message = "Questions successfully assigned to batch: %s." % batch.name.capitalize( ) messages.success(request, success_message) return HttpResponseRedirect( reverse("batch_questions_page", args=(batch_id, ))) all_modules = QuestionModule.objects.all() used_identifiers = [ question.identifier for question in batch.batch_questions.all() ] library_questions = QuestionTemplate.objects.exclude( identifier__in=used_identifiers).order_by('identifier') question_filter_form = QuestionFilterForm() # library_questions = question_filter_form.filter(library_questions) request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), (batch.survey.name, reverse('batch_index_page', args=(batch.survey.pk, ))), (batch.name, reverse('batch_questions_page', args=(batch.pk, ))), ]) context = { 'batch_questions_form': unicode(batch_questions_form), 'batch': batch, 'button_label': 'Save', 'id': 'assign-question-to-batch-form', 'groups': groups, 'library_questions': library_questions, 'question_filter_form': question_filter_form, 'modules': all_modules } return render(request, 'batches/assign.html', context)