没有登录的时候自动跳转到登录页面,修改相关测试

This commit is contained in:
virusdefender
2015-08-23 20:45:02 +08:00
parent b590ee8577
commit f682aa1fb3
4 changed files with 10 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
# coding=utf-8 # coding=utf-8
from functools import wraps from functools import wraps
from django.http import HttpResponse from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from utils.shortcuts import error_response, error_page from utils.shortcuts import error_response, error_page
@@ -18,7 +18,7 @@ def login_required(func):
if request.is_ajax(): if request.is_ajax():
return error_response(u"请先登录") return error_response(u"请先登录")
else: else:
return error_page(request, u"请先登录") return HttpResponseRedirect("/login/")
return check return check
@@ -31,5 +31,5 @@ def admin_required(func):
if request.is_ajax(): if request.is_ajax():
return error_response(u"需要管理员权限") return error_response(u"需要管理员权限")
else: else:
return error_page(request, u"需要管理员权限") return error_page(request, u"需要管理员权限,如果没有登录,请先登录")
return check return check

View File

@@ -1,5 +1,6 @@
# coding=utf-8 # coding=utf-8
from django.conf.urls import include, url from django.conf.urls import include, url
from django.views.generic import TemplateView
from .tests import (LoginRequiredCBVTestWithArgs, LoginRequiredCBVTestWithoutArgs, from .tests import (LoginRequiredCBVTestWithArgs, LoginRequiredCBVTestWithoutArgs,
AdminRequiredCBVTestWithArgs, AdminRequiredCBVTestWithoutArgs) AdminRequiredCBVTestWithArgs, AdminRequiredCBVTestWithoutArgs)
@@ -15,4 +16,5 @@ urlpatterns = [
url(r'^admin_required_test/fbv/(?P<problem_id>\d+)/$', "account.tests.admin_required_FBC_test_with_args"), url(r'^admin_required_test/fbv/(?P<problem_id>\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/1/$', AdminRequiredCBVTestWithoutArgs.as_view()),
url(r'^admin_required_test/cbv/(?P<problem_id>\d+)/$', AdminRequiredCBVTestWithArgs.as_view()), url(r'^admin_required_test/cbv/(?P<problem_id>\d+)/$', AdminRequiredCBVTestWithArgs.as_view()),
url(r'^login/$', TemplateView.as_view(template_name="oj/account/login.html"), name="user_login_page"),
] ]

View File

@@ -260,7 +260,7 @@ class LoginRequiredDecoratorTest(TestCase):
def test_fbv_without_args(self): def test_fbv_without_args(self):
# 没登陆 # 没登陆
response = self.client.get("/login_required_test/fbv/1/") 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") self.client.login(username="test", password="test")
@@ -270,7 +270,7 @@ class LoginRequiredDecoratorTest(TestCase):
def test_fbv_with_args(self): def test_fbv_with_args(self):
# 没登陆 # 没登陆
response = self.client.get("/login_required_test/fbv/1024/") 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") self.client.login(username="test", password="test")
@@ -353,7 +353,7 @@ class AdminRequiredDecoratorTest(TestCase):
def test_cbv_without_args(self): def test_cbv_without_args(self):
# 没登陆 # 没登陆
response = self.client.get("/admin_required_test/cbv/1/") 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") self.client.login(username="test", password="test")

View File

@@ -1,7 +1,7 @@
# coding=utf-8 # coding=utf-8
from functools import wraps from functools import wraps
from django.http import HttpResponse from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from django.utils.timezone import now from django.utils.timezone import now
@@ -30,7 +30,7 @@ def check_user_contest_permission(func):
if request.is_ajax(): if request.is_ajax():
return error_response(u"请先登录") return error_response(u"请先登录")
else: else:
return error_page(request, u"请先登录") return HttpResponseRedirect("/login/")
# kwargs 就包含了url 里面的播或参数 # kwargs 就包含了url 里面的播或参数
if "contest_id" in kwargs: if "contest_id" in kwargs: