diff --git a/account/decorators.py b/account/decorators.py index 33634e55..4113dd01 100644 --- a/account/decorators.py +++ b/account/decorators.py @@ -1,6 +1,6 @@ # coding=utf-8 from functools import wraps -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render from utils.shortcuts import error_response, error_page @@ -18,7 +18,7 @@ def login_required(func): if request.is_ajax(): return error_response(u"请先登录") else: - return error_page(request, u"请先登录") + return HttpResponseRedirect("/login/") return check @@ -31,5 +31,5 @@ def admin_required(func): if request.is_ajax(): return error_response(u"需要管理员权限") else: - return error_page(request, u"需要管理员权限") + return error_page(request, u"需要管理员权限,如果没有登录,请先登录") return check diff --git a/account/test_urls.py b/account/test_urls.py index 9757d901..5d2bdccf 100644 --- a/account/test_urls.py +++ b/account/test_urls.py @@ -1,5 +1,6 @@ # coding=utf-8 from django.conf.urls import include, url +from django.views.generic import TemplateView from .tests import (LoginRequiredCBVTestWithArgs, LoginRequiredCBVTestWithoutArgs, AdminRequiredCBVTestWithArgs, AdminRequiredCBVTestWithoutArgs) @@ -15,4 +16,5 @@ urlpatterns = [ url(r'^admin_required_test/fbv/(?P\d+)/$', "account.tests.admin_required_FBC_test_with_args"), url(r'^admin_required_test/cbv/1/$', AdminRequiredCBVTestWithoutArgs.as_view()), url(r'^admin_required_test/cbv/(?P\d+)/$', AdminRequiredCBVTestWithArgs.as_view()), + url(r'^login/$', TemplateView.as_view(template_name="oj/account/login.html"), name="user_login_page"), ] diff --git a/account/tests.py b/account/tests.py index a84911c0..0b7a8bba 100644 --- a/account/tests.py +++ b/account/tests.py @@ -260,7 +260,7 @@ class LoginRequiredDecoratorTest(TestCase): def test_fbv_without_args(self): # 没登陆 response = self.client.get("/login_required_test/fbv/1/") - self.assertTemplateUsed(response, "utils/error.html") + self.assertRedirects(response, "/login/") # 登陆后 self.client.login(username="test", password="test") @@ -270,7 +270,7 @@ class LoginRequiredDecoratorTest(TestCase): def test_fbv_with_args(self): # 没登陆 response = self.client.get("/login_required_test/fbv/1024/") - self.assertTemplateUsed(response, "utils/error.html") + self.assertRedirects(response, "/login/") # 登陆后 self.client.login(username="test", password="test") @@ -353,7 +353,7 @@ class AdminRequiredDecoratorTest(TestCase): def test_cbv_without_args(self): # 没登陆 response = self.client.get("/admin_required_test/cbv/1/") - self.assertTemplateUsed(response, "utils/error.html") + self.assertRedirects(response, "/login/") # 登陆后 self.client.login(username="test", password="test") diff --git a/contest/decorators.py b/contest/decorators.py index 06a8b703..0b25759f 100644 --- a/contest/decorators.py +++ b/contest/decorators.py @@ -1,7 +1,7 @@ # coding=utf-8 from functools import wraps -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render from django.utils.timezone import now @@ -30,7 +30,7 @@ def check_user_contest_permission(func): if request.is_ajax(): return error_response(u"请先登录") else: - return error_page(request, u"请先登录") + return HttpResponseRedirect("/login/") # kwargs 就包含了url 里面的播或参数 if "contest_id" in kwargs: diff --git a/contest/views.py b/contest/views.py index ae288cc3..42ecd62e 100644 --- a/contest/views.py +++ b/contest/views.py @@ -17,7 +17,7 @@ from account.decorators import login_required from group.models import Group from announcement.models import Announcement -from .models import Contest, ContestProblem +from .models import Contest, ContestProblem, ContestSubmission from .decorators import check_user_contest_permission from .serializers import (CreateContestSerializer, ContestSerializer, EditContestSerializer, CreateContestProblemSerializer, ContestProblemSerializer, @@ -263,17 +263,25 @@ def contest_page(request, contest_id): return render(request, "oj/contest/contest_index.html", {"contest": contest}) +@check_user_contest_permission def contest_problem_page(request, contest_id, contest_problem_id): try: - Contest.objects.get(id=contest_id) + contest = Contest.objects.get(id=contest_id) except Contest.DoesNotExist: return error_page(request, u"比赛不存在") try: contest_problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) except ContestProblem.DoesNotExist: return error_page(request, u"比赛题目不存在") + show_warning = False + try: + submission = ContestSubmission.objects.get(user=request.user, contest=contest, problem=contest_problem) + show_warning = submission.ac + except ContestSubmission.DoesNotExist: + pass return render(request, "oj/contest/contest_problem.html", {"contest_problem": contest_problem, - "samples": json.loads(contest_problem.samples)}) + "samples": json.loads(contest_problem.samples), + "show_warning": show_warning}) @check_user_contest_permission diff --git a/static/src/js/app/oj/account/login.js b/static/src/js/app/oj/account/login.js index bc95cf5b..0a057627 100644 --- a/static/src/js/app/oj/account/login.js +++ b/static/src/js/app/oj/account/login.js @@ -14,8 +14,15 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c //成功登陆 var ref = document.referrer; if(ref){ - if(ref.split("/")[2] == location.hostname){ + // 注册页和本页的来源的跳转回首页,防止死循环 + if(ref.indexOf("register") > -1 || ref.indexOf("login") > -1){ + location.href = "/"; + return; + } + // 判断来源,只有同域下才跳转 + if(ref.split("/")[2].split(":")[0] == location.hostname){ location.href = ref; + return; } } location.href = "/"; diff --git a/submission/views.py b/submission/views.py index eb4a3bb7..6399e22b 100644 --- a/submission/views.py +++ b/submission/views.py @@ -93,8 +93,7 @@ def contest_problem_my_submissions_list_page(request, contest_id, contest_proble contest_problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) except Problem.DoesNotExist: return error_page(request, u"比赛问题不存在") - submissions = Submission.objects.filter(user_id=request.user.id, problem_id=contest_problem.id).order_by( - "-create_time"). \ + submissions = Submission.objects.filter(user_id=request.user.id, problem_id=contest_problem.id).order_by("-create_time"). \ values("id", "result", "create_time", "accepted_answer_time", "language") return render(request, "oj/contest/my_submissions_list.html", {"submissions": submissions, "contest_problem": contest_problem}) @@ -160,7 +159,7 @@ def my_submission_list_page(request, page=1): class ContestSubmissionAPIView(APIView): - # @check_user_contest_permission + @check_user_contest_permission def post(self, request): """ 创建比赛的提交 diff --git a/template/oj/contest/contest_problem.html b/template/oj/contest/contest_problem.html index 69991adb..32b0617a 100644 --- a/template/oj/contest/contest_problem.html +++ b/template/oj/contest/contest_problem.html @@ -81,6 +81,7 @@
+
+ {% if show_warning %} + + {% endif %}
diff --git a/template/oj/contest/contest_problems_list.html b/template/oj/contest/contest_problems_list.html index 1b40095c..23cefd71 100644 --- a/template/oj/contest/contest_problems_list.html +++ b/template/oj/contest/contest_problems_list.html @@ -21,8 +21,6 @@ 排名 - -
@@ -40,11 +38,14 @@ {% for item in contest_problems %} - - {{ item.sort_index }} + + - {{ item.title }} + + {{ item.sort_index }} + + + {{ item.title }} {{ item|accepted_radio }} diff --git a/template/oj/contest/problems.html b/template/oj/contest/problems.html deleted file mode 100644 index 0f4e49e7..00000000 --- a/template/oj/contest/problems.html +++ /dev/null @@ -1,15 +0,0 @@ -{% extends "oj_base.html" %} -{% block body %} -
- -

第一次比赛

- -

开始时间: 2015-6-8 19:00 结束时间: 2015-9-1 12:00

- -
-{% endblock %} \ No newline at end of file diff --git a/template/oj/problem/problem_list.html b/template/oj/problem/problem_list.html index 6c106e9a..a26ae80b 100644 --- a/template/oj/problem/problem_list.html +++ b/template/oj/problem/problem_list.html @@ -22,15 +22,15 @@ # 题目 难度 - 通过率 + 通过率 {% for item in problems %} - {{ item.id }} - {{ item.title }} + {{ item.id }} + {{ item.title }} {{ item.difficulty }} {{ item|accepted_radio }}