From d2242a78c69c74d3e7f44135d9144848f69f4aab Mon Sep 17 00:00:00 2001 From: esp Date: Fri, 21 Aug 2015 20:58:04 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=90=8E=E7=AB=AF]=E4=BF=AE=E6=94=B9=E6=88=91?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E4=BA=A4=E9=A1=B5=E9=9D=A2,=E5=8E=BB?= =?UTF-8?q?=E6=8E=89=E4=BA=86=E5=86=97=E4=BD=99=E8=AF=AD=E5=8F=A5,?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- submission/tests.py | 43 ++++++++++++++++++++++++++++++++++++++++++- submission/views.py | 11 +++-------- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/submission/tests.py b/submission/tests.py index 7ce503c2..c31741bc 100644 --- a/submission/tests.py +++ b/submission/tests.py @@ -1,3 +1,44 @@ from django.test import TestCase +from account.models import User, REGULAR_USER +from submission.models import Submission +from rest_framework.test import APITestCase, APIClient -# Create your tests here. + +class SubmissionsListPageTest(TestCase): + def setUp(self): + self.client = APIClient() + self.user = User.objects.create(username="gogoing", admin_type=REGULAR_USER) + self.user2 = User.objects.create(username="cool", admin_type=REGULAR_USER) + self.user2.set_password("666666") + self.user.set_password("666666") + self.user.save() + # self.client.login(username="gogoing", password="666666") + self.submission = Submission.objects.create(user_id=self.user.id, + language=1, + code='#include "stdio.h"\nint main(){\n\treturn 0;\n}', + problem_id=1) + + def test_visit_submissionsListPage_successfully(self): + self.client.login(username="gogoing", password="666666") + response = self.client.get('/my_submissions/1/') + self.assertEqual(response.status_code, 200) + + def test_visit_submissionsListPage_without_page_successfully(self): + self.client.login(username="gogoing", password="666666") + response = self.client.get('/my_submissions/') + self.assertEqual(response.status_code, 200) + + def test_submissionsListPage_does_not_exist(self): + self.client.login(username="gogoing", password="666666") + response = self.client.get('/my_submissions/5/') + self.assertTemplateUsed(response, "utils/error.html") + + def test_submissionsListPage_page_not_exist(self): + self.client.login(username="gogoing", password="666666") + response = self.client.get('/my_submissions/5/') + self.assertTemplateUsed(response, "utils/error.html") + + def test_submissionsListPage_have_no_submission(self): + self.client.login(username="cool", password="666666") + response = self.client.get('/my_submissions/') + self.assertEqual(response.status_code, 200) diff --git a/submission/views.py b/submission/views.py index a3319849..5e0c7b69 100644 --- a/submission/views.py +++ b/submission/views.py @@ -106,10 +106,9 @@ class SubmissionAdminAPIView(APIView): @login_required def my_submission_list_page(request, page = 1): - try: - submissions = Submission.objects.filter(user_id=request.user.id) - except Submission.DoesNotExist: - return error_page(request, u"你还没有提交过任何问题") + if not page: + page = 1 + submissions = Submission.objects.filter(user_id=request.user.id) paginator = Paginator(submissions, 20) try: current_page = paginator.page(int(page)) @@ -120,7 +119,6 @@ def my_submission_list_page(request, page = 1): previous_page = current_page.previous_page_number() except Exception: pass - try: next_page = current_page.next_page_number() except Exception: @@ -129,6 +127,3 @@ def my_submission_list_page(request, page = 1): return render(request, "oj/submission/my_submissions_list.html", {"submissions": current_page, "page": int(page), "previous_page": previous_page, "next_page": next_page}) - - - return render(request, "oj/submission/my_submissions_list.html", {"submissions": submission})