Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed #21587 -- deprecation warning for upcoming permanent default #2564

Closed
wants to merge 1 commit into from

3 participants

@arachnegl

No description provided.

@Gwildor

I have the same concern on this change as on #2432: "...because what if the user has set the flag as an attribute on the view instead of passing it along when initializing the view?"

Perhaps checking both kwargs and using hasattr is the way to go.

@timgraham
Owner

Closing this as it needs further work as noted above and in the ticket.

@timgraham timgraham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
16 django/views/generic/base.py
@@ -1,6 +1,7 @@
from __future__ import unicode_literals
import logging
+import warnings
from functools import update_wrapper
from django import http
@@ -8,6 +9,7 @@
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template.response import TemplateResponse
from django.utils.decorators import classonlymethod
+from django.utils.deprecation import RemovedInDjango19Warning
from django.utils import six
logger = logging.getLogger('django.request')
@@ -164,6 +166,20 @@ class RedirectView(View):
pattern_name = None
query_string = False
+ def __init__(self, **kwargs):
+ """
+ When initializing the RedirectView we check if permanent is given,
+ if not we raise a deprecation warning and continue on as normal.
+ """
+ if 'permanent' not in kwargs:
+ warnings.warn(
+ ("Default value of permanent for RedirectViews will change "
+ "from True to False in Django 1.9"),
+ RemovedInDjango19Warning
+ )
+
+ super(RedirectView, self).__init__(**kwargs)
+
def get_redirect_url(self, *args, **kwargs):
"""
Return the URL redirect to. Keyword arguments from the
View
3  docs/ref/class-based-views/base.txt
@@ -220,7 +220,8 @@ RedirectView
Whether the redirect should be permanent. The only difference here is
the HTTP status code returned. If ``True``, then the redirect will use
status code 301. If ``False``, then the redirect will use status code
- 302. By default, ``permanent`` is ``True``.
+ 302. By default, ``permanent`` is ``True``. The default will change from
+ ``True`` to ``False`` in the Django 1.9.
.. attribute:: query_string
View
37 tests/generic_views/test_base.py
@@ -2,10 +2,12 @@
import time
import unittest
+import warnings
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
from django.test import TestCase, RequestFactory, override_settings
+from django.utils.deprecation import RemovedInDjango19Warning
from django.views.generic import View, TemplateView, RedirectView
from . import views
@@ -439,6 +441,41 @@ def test_direct_instantiation(self):
response = view.dispatch(self.rf.head('/foo/'))
self.assertEqual(response.status_code, 410)
+ def test_deprecation_warning_raised_when_permanent_not_passed(self):
+ """
+ Tests to make sure that a DeprecationWarning is raised if 'permanent'
+ is not included during instantiation.
+ """
+ with warnings.catch_warnings(record=True) as warns:
+ # Cause all warnings to always be triggered.
+ warnings.simplefilter('always')
+
+ view_function = RedirectView.as_view(url='/bar/')
+ # should trigger a warning
+ request = self.rf.request(PATH_INFO='/foo/')
+ view_function(request)
+
+ # check warning raised once and is instance of right class
+ self.assertEqual(len(warns), 1)
+ got_warning = warns[0].category
+ self.assertTrue(issubclass(got_warning, RemovedInDjango19Warning))
+
+ def test_no_deprecation_warning_when_permanent_passed(self):
+ """
+ Tests to make sure that a DeprecationWarning is not raised if
+ 'permanent' is not included during instantiation.
+ """
+ with warnings.catch_warnings(record=True) as warns:
+ # Cause all warnings to always be triggered.
+ warnings.simplefilter('always')
+
+ # This shouldn't trigger a warning
+ view_function = RedirectView.as_view(url='/bar/', permanent=False)
+ request = self.rf.request(PATH_INFO='/foo/')
+ view_function(request)
+
+ self.assertEqual(len(warns), 0)
+
class GetContextDataTest(unittest.TestCase):
Something went wrong with that request. Please try again.