From 47789410dbf351fd17a7854492b7b5b99a66bb1c Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Thu, 27 Nov 2014 05:44:56 +0200 Subject: [PATCH] Corrected deprecation warnings for RedirectView; refs #21587. --- django/views/generic/base.py | 15 ++++++- tests/test_client/tests.py | 45 +++++++------------ tests/test_client/urls.py | 6 +-- tests/test_client_regress/urls.py | 24 +++++----- .../urlpatterns_reverse/reverse_lazy_urls.py | 3 +- tests/urlpatterns_reverse/tests.py | 8 ++-- 6 files changed, 49 insertions(+), 52 deletions(-) diff --git a/django/views/generic/base.py b/django/views/generic/base.py index 594122ef77..8a9b6baa44 100644 --- a/django/views/generic/base.py +++ b/django/views/generic/base.py @@ -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 diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py index 4ed7c6c1f8..cf96b89a62 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -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): diff --git a/tests/test_client/urls.py b/tests/test_client/urls.py index 7073d4590e..45d2859633 100644 --- a/tests/test_client/urls.py +++ b/tests/test_client/urls.py @@ -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), diff --git a/tests/test_client_regress/urls.py b/tests/test_client_regress/urls.py index a69a774555..8914c82902 100644 --- a/tests/test_client_regress/urls.py +++ b/tests/test_client_regress/urls.py @@ -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.+)/$', 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), diff --git a/tests/urlpatterns_reverse/reverse_lazy_urls.py b/tests/urlpatterns_reverse/reverse_lazy_urls.py index 57358f6254..76ff67b2c0 100644 --- a/tests/urlpatterns_reverse/reverse_lazy_urls.py +++ b/tests/urlpatterns_reverse/reverse_lazy_urls.py @@ -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)), ] diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py index 0c4e08369e..18299fe733 100644 --- a/tests/urlpatterns_reverse/tests.py +++ b/tests/urlpatterns_reverse/tests.py @@ -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')