Skip to content

Commit

Permalink
Fixed #21587 Adds deprecation warning to RedirectView and adds test f…
Browse files Browse the repository at this point in the history
…or it.
  • Loading branch information
kcphysics committed Mar 14, 2014
1 parent 37f7f23 commit 964e34a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions django/views/generic/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals

import logging
import warnings
from functools import update_wrapper

from django import http
Expand Down Expand Up @@ -164,6 +165,20 @@ class RedirectView(View):
pattern_name = None
query_string = False

def __init__(self, *args, **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:

This comment has been minimized.

Copy link
@Gwildor

Gwildor Mar 14, 2014

I'm not sure if it's going to work like that (but correct me if I'm wrong!), because what if the user has set the flag as an attribute on the view instead of passing it along when initializing the view?

My suggestion:

permanent = None

def __init__(self, *args, **kwargs):
    super(RedirectView, self).__init__(*args, **kwargs)

    if self.permanent is None:
        self.permanent = True  # set old value for the backwards compatibility
        # warning here
warnings.warn(
"Deprecation Warning: Default value of permanent for RedirectViews will change from True to False in" \
" the next release",
DeprecationWarning
)

super(RedirectView, self).__init__(*args, **kwargs)

def get_redirect_url(self, *args, **kwargs):
"""
Return the URL redirect to. Keyword arguments from the
Expand Down
16 changes: 16 additions & 0 deletions tests/generic_views/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import time
import unittest
import warnings

from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
Expand Down Expand Up @@ -439,6 +440,21 @@ def test_direct_instantiation(self):
response = view.dispatch(self.rf.head('/foo/'))
self.assertEqual(response.status_code, 410)

def test_permanent_default_deprecation_warning(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 = RedirectView(url='/bar/')
view = RedirectView.as_view(url='/bar/')(self.rf.request(PATH_INFO='/foo/'))

self.assertEqual(
len(warns),
2,
msg="Failed to generate deprecation warning when calling RedirectView without permanent kwargs"
)

class GetContextDataTest(unittest.TestCase):

Expand Down

0 comments on commit 964e34a

Please sign in to comment.