Corrected deprecation warnings for RedirectView; refs #21587.

This commit is contained in:
Berker Peksag 2014-11-27 05:44:56 +02:00 committed by Tim Graham
parent 9253042d53
commit 47789410db
6 changed files with 49 additions and 52 deletions

View File

@ -173,11 +173,24 @@ class RedirectView(View):
"from True to False in Django 1.9. Set an explicit value "
"to silence this warning.",
RemovedInDjango19Warning,
stacklevel=3
stacklevel=2
)
self.permanent = True
super(RedirectView, self).__init__(*args, **kwargs)
@classonlymethod
def as_view(cls, **initkwargs):
if 'permanent' not in initkwargs and cls.permanent is _sentinel:
warnings.warn(
"Default value of 'RedirectView.permanent' will change "
"from True to False in Django 1.9. Set an explicit value "
"to silence this warning.",
RemovedInDjango19Warning,
stacklevel=2
)
initkwargs['permanent'] = True
return super(RedirectView, cls).as_view(**initkwargs)
def get_redirect_url(self, *args, **kwargs):
"""
Return the URL redirect to. Keyword arguments from the

View File

@ -22,11 +22,8 @@ rather than the HTML rendered to the end-user.
"""
from __future__ import unicode_literals
import warnings
from django.core import mail
from django.http import HttpResponse
from django.utils.deprecation import RemovedInDjango19Warning
from django.test import Client, TestCase, RequestFactory
from django.test import override_settings
@ -177,18 +174,14 @@ class ClientTest(TestCase):
def test_permanent_redirect(self):
"GET a URL that redirects permanently elsewhere"
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango19Warning)
response = self.client.get('/permanent_redirect_view/')
# Check that the response was a 301 (permanent redirect)
self.assertRedirects(response, 'http://testserver/get_view/', status_code=301)
response = self.client.get('/permanent_redirect_view/')
# Check that the response was a 301 (permanent redirect)
self.assertRedirects(response, 'http://testserver/get_view/', status_code=301)
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango19Warning)
client_providing_host = Client(HTTP_HOST='django.testserver')
response = client_providing_host.get('/permanent_redirect_view/')
# Check that the response was a 301 (permanent redirect) with absolute URI
self.assertRedirects(response, 'http://django.testserver/get_view/', status_code=301)
client_providing_host = Client(HTTP_HOST='django.testserver')
response = client_providing_host.get('/permanent_redirect_view/')
# Check that the response was a 301 (permanent redirect) with absolute URI
self.assertRedirects(response, 'http://django.testserver/get_view/', status_code=301)
def test_temporary_redirect(self):
"GET a URL that does a non-permanent redirect"
@ -198,34 +191,26 @@ class ClientTest(TestCase):
def test_redirect_to_strange_location(self):
"GET a URL that redirects to a non-200 page"
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango19Warning)
response = self.client.get('/double_redirect_view/')
response = self.client.get('/double_redirect_view/')
# Check that the response was a 302, and that
# the attempt to get the redirection location returned 301 when retrieved
self.assertRedirects(response, 'http://testserver/permanent_redirect_view/', target_status_code=301)
# Check that the response was a 302, and that
# the attempt to get the redirection location returned 301 when retrieved
self.assertRedirects(response, 'http://testserver/permanent_redirect_view/', target_status_code=301)
def test_follow_redirect(self):
"A URL that redirects can be followed to termination."
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango19Warning)
response = self.client.get('/double_redirect_view/', follow=True)
self.assertRedirects(response, 'http://testserver/get_view/', status_code=302, target_status_code=200)
response = self.client.get('/double_redirect_view/', follow=True)
self.assertRedirects(response, 'http://testserver/get_view/', status_code=302, target_status_code=200)
self.assertEqual(len(response.redirect_chain), 2)
def test_redirect_http(self):
"GET a URL that redirects to an http URI"
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango19Warning)
response = self.client.get('/http_redirect_view/', follow=True)
response = self.client.get('/http_redirect_view/', follow=True)
self.assertFalse(response.test_was_secure_request)
def test_redirect_https(self):
"GET a URL that redirects to an https URI"
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango19Warning)
response = self.client.get('/https_redirect_view/', follow=True)
response = self.client.get('/https_redirect_view/', follow=True)
self.assertTrue(response.test_was_secure_request)
def test_notfound_response(self):

View File

@ -13,10 +13,10 @@ urlpatterns = [
url(r'^raw_post_view/$', views.raw_post_view),
url(r'^redirect_view/$', views.redirect_view),
url(r'^secure_view/$', views.view_with_secure),
url(r'^permanent_redirect_view/$', RedirectView.as_view(url='/get_view/')),
url(r'^permanent_redirect_view/$', RedirectView.as_view(url='/get_view/', permanent=True)),
url(r'^temporary_redirect_view/$', RedirectView.as_view(url='/get_view/', permanent=False)),
url(r'^http_redirect_view/$', RedirectView.as_view(url='/secure_view/')),
url(r'^https_redirect_view/$', RedirectView.as_view(url='https://testserver/secure_view/')),
url(r'^http_redirect_view/$', RedirectView.as_view(url='/secure_view/', permanent=True)),
url(r'^https_redirect_view/$', RedirectView.as_view(url='https://testserver/secure_view/', permanent=True)),
url(r'^double_redirect_view/$', views.double_redirect_view),
url(r'^bad_view/$', views.bad_view),
url(r'^form_view/$', views.form_view),

View File

@ -3,7 +3,7 @@ from django.views.generic import RedirectView
from . import views
# TODO: Remove permanent=True where it doesn't matter in Django 1.9
urlpatterns = [
url(r'', include('test_client.urls')),
@ -15,17 +15,17 @@ urlpatterns = [
url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'),
url(r'^nested_view/$', views.nested_view, name='nested_view'),
url(r'^login_protected_redirect_view/$', views.login_protected_redirect_view),
url(r'^redirects/$', RedirectView.as_view(url='/redirects/further/')),
url(r'^redirects/further/$', RedirectView.as_view(url='/redirects/further/more/')),
url(r'^redirects/further/more/$', RedirectView.as_view(url='/no_template_view/')),
url(r'^redirect_to_non_existent_view/$', RedirectView.as_view(url='/non_existent_view/')),
url(r'^redirect_to_non_existent_view2/$', RedirectView.as_view(url='/redirect_to_non_existent_view/')),
url(r'^redirect_to_self/$', RedirectView.as_view(url='/redirect_to_self/')),
url(r'^redirects/$', RedirectView.as_view(url='/redirects/further/', permanent=True)),
url(r'^redirects/further/$', RedirectView.as_view(url='/redirects/further/more/', permanent=True)),
url(r'^redirects/further/more/$', RedirectView.as_view(url='/no_template_view/', permanent=True)),
url(r'^redirect_to_non_existent_view/$', RedirectView.as_view(url='/non_existent_view/', permanent=True)),
url(r'^redirect_to_non_existent_view2/$', RedirectView.as_view(url='/redirect_to_non_existent_view/', permanent=True)),
url(r'^redirect_to_self/$', RedirectView.as_view(url='/redirect_to_self/', permanent=True)),
url(r'^redirect_to_self_with_changing_query_view/$', views.redirect_to_self_with_changing_query_view),
url(r'^circular_redirect_1/$', RedirectView.as_view(url='/circular_redirect_2/')),
url(r'^circular_redirect_2/$', RedirectView.as_view(url='/circular_redirect_3/')),
url(r'^circular_redirect_3/$', RedirectView.as_view(url='/circular_redirect_1/')),
url(r'^redirect_other_host/$', RedirectView.as_view(url='https://otherserver:8443/no_template_view/')),
url(r'^circular_redirect_1/$', RedirectView.as_view(url='/circular_redirect_2/', permanent=True)),
url(r'^circular_redirect_2/$', RedirectView.as_view(url='/circular_redirect_3/', permanent=True)),
url(r'^circular_redirect_3/$', RedirectView.as_view(url='/circular_redirect_1/', permanent=True)),
url(r'^redirect_other_host/$', RedirectView.as_view(url='https://otherserver:8443/no_template_view/', permanent=True)),
url(r'^set_session/$', views.set_session_view),
url(r'^check_session/$', views.check_session_view),
url(r'^request_methods/$', views.request_methods_view),
@ -33,7 +33,7 @@ urlpatterns = [
url(r'^check_binary/$', views.return_undecodable_binary),
url(r'^parse_unicode_json/$', views.return_json_file),
url(r'^check_headers/$', views.check_headers),
url(r'^check_headers_redirect/$', RedirectView.as_view(url='/check_headers/')),
url(r'^check_headers_redirect/$', RedirectView.as_view(url='/check_headers/', permanent=True)),
url(r'^body/$', views.body),
url(r'^read_all/$', views.read_all),
url(r'^read_buffer/$', views.read_buffer),

View File

@ -6,5 +6,6 @@ urlpatterns = [
url(r'^redirected_to/$', empty_view, name='named-lazy-url-redirected-to'),
url(r'^login/$', empty_view, name='some-login-page'),
url(r'^login_required_view/$', login_required_view),
url(r'^redirect/$', LazyRedirectView.as_view()),
# TODO: Remove permanent=True where it doesn't matter in Django 1.9
url(r'^redirect/$', LazyRedirectView.as_view(permanent=True)),
]

View File

@ -18,7 +18,7 @@ from django.http import HttpRequest, HttpResponseRedirect, HttpResponsePermanent
from django.shortcuts import redirect
from django.test import TestCase, override_settings
from django.utils import six
from django.utils.deprecation import RemovedInDjango19Warning, RemovedInDjango20Warning
from django.utils.deprecation import RemovedInDjango20Warning
from admin_scripts.tests import AdminScriptTestCase
@ -309,10 +309,8 @@ class ResolverTests(unittest.TestCase):
class ReverseLazyTest(TestCase):
def test_redirect_with_lazy_reverse(self):
with warnings.catch_warnings():
warnings.simplefilter("ignore", RemovedInDjango19Warning)
response = self.client.get('/redirect/')
self.assertRedirects(response, "/redirected_to/", status_code=301)
response = self.client.get('/redirect/')
self.assertRedirects(response, "/redirected_to/", status_code=301)
def test_user_permission_with_lazy_reverse(self):
User.objects.create_user('alfred', 'alfred@example.com', password='testpw')