def test_navigate_to_first_page_complex(self): """Navigate to the first page, even if this doesn't have sequence 1 """ page_1_id = Test.get_first_page_for(self.test1.id) self.test1.page_set.first().delete() first_page_id = Test.get_first_page_for(self.test1.id) self.assertNotEqual(page_1_id, first_page_id) response = self._get_response(test_id=self.test1.id, page_id=first_page_id) self.assertEqual(200, response.status_code)
def is_page_sequence_valid(test_id, page_id, last_test_id, last_page_id): """Checks whether the current test page is in the right sequence with regards to the last. Given the current test_id and page_id, this function checks the HTTP_REFERER (last page) to see if :param last_test_id: :param referer: """ # test_session = TestSession(request.session) # if test_session is None: # raise KeyError('Unable to determine page sequence') # last_test_id, last_page_id = test_session.get_last_test_page() #check if first page is right if last_page_id is None: return int(page_id) == Test.get_first_page_for(test_id) if last_page_id == page_id: return True #check if next page is right (next or results page) if int(page_id) != get_next_page(last_test_id, last_page_id): return False return True
def test_navigation_to_first_page_simple(self): """Navigate to first page (by coincidence page 1) """ first_page_id = Test.get_first_page_for(self.test1.id) response = self._get_response(test_id=self.test1.id, page_id=first_page_id) self.assertEqual(200, response.status_code)
def get_next_page(test_id, page_id): """Determines the next test page. Either the next page in the test that has questions, or 0 - meaning the results page. :param page_id: id of the testing_app.models.Page :param test_id: if of the testing_app.models.Test """ return Test.get_next_page_for(test_id, page_id)
def test_get_to_next_page(self): first_page_path = reverse('pages', kwargs={'test_id': self.test1.id, 'page_id': self.t1_page1.id}) first_page_answers = { str(self.t1_p1_question1.as_form_id()): str( self.t1_p1_q1_answer1.id)} response = self.client.post(first_page_path, first_page_answers, follow=True) next_page_id = Test.get_next_page_for(self.test1.id, self.t1_page1.id) next_page_path = reverse('pages', kwargs={'test_id': self.test1.id, 'page_id': next_page_id}) self.assertRedirects(response, next_page_path)
def pages_view(request, test_id, page_id=1): """Handles the logic for the test pages view: "testing_app/test_page.html" :param request :param test_id :param page_id """ #Determine next page: either normal page, or results page transaction.set_autocommit(False) next_page_id = get_next_page(test_id, page_id) if request.method == 'POST': old_questions = Question.objects.filter(page__test_id=test_id, page_id=page_id) form = create_form_for_questions(old_questions)(request.POST) if form.is_valid(): if int(page_id) == Test.get_last_page_for(test_id): redirect_to = reverse('results', kwargs={'test_id': test_id}) transaction.commit() else: redirect_to = reverse('pages', kwargs={'test_id': test_id, 'page_id': next_page_id}) transaction.commit() return HttpResponseRedirect(redirect_to) else: questions = Question.objects.filter(page__test_id=test_id, page_id=page_id) form = create_form_for_questions(questions)() context = {'test_id': test_id, 'page_id': page_id, 'form': form} if form.submittable is False: context['parent_url'] = request.META.get('HTTP_REFERER') result = render(request, 'testing_app/test_page.html', context) transaction.commit() return result