Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error when setting a removed rest_framework setting for #3644 #3715

Merged
merged 17 commits into from
Dec 18, 2015
Merged
17 changes: 16 additions & 1 deletion rest_framework/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"""
from __future__ import unicode_literals

import warnings

from django.conf import settings
from django.test.signals import setting_changed
from django.utils import six
Expand Down Expand Up @@ -135,6 +137,12 @@
)


# List of settings that have been removed
REMOVED_SETTINGS = (
"PAGINATE_BY", "PAGINATE_BY_PARAM", "MAX_PAGINATE_BY",
)


def perform_import(val, setting_name):
"""
If the given setting is a string import notation,
Expand Down Expand Up @@ -177,7 +185,7 @@ class APISettings(object):
"""
def __init__(self, user_settings=None, defaults=None, import_strings=None):
if user_settings:
self._user_settings = user_settings
self._user_settings = self.__check_user_settings(user_settings)
self.defaults = defaults or DEFAULTS
self.import_strings = import_strings or IMPORT_STRINGS

Expand Down Expand Up @@ -206,6 +214,13 @@ def __getattr__(self, attr):
setattr(self, attr, val)
return val

def __check_user_settings(self, user_settings):
SETTINGS_DOC = "http://www.django-rest-framework.org/api-guide/settings/"
for setting in REMOVED_SETTINGS:
if setting in user_settings:
warnings.warn("The '%s' setting has been removed. Please refer to '%s' for available settings." % (setting, SETTINGS_DOC), DeprecationWarning)
return user_settings


api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import warnings

from django.test import TestCase

from rest_framework.settings import APISettings
Expand All @@ -18,6 +20,19 @@ def test_import_error_message_maintained(self):
with self.assertRaises(ImportError):
settings.DEFAULT_RENDERER_CLASSES

def test_warning_raised_on_removed_setting(self):
"""
Make sure user is alerted with an error when a removed setting
is set.
"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
APISettings({
'MAX_PAGINATE_BY': 100
})
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))


class TestSettingTypes(TestCase):
def test_settings_consistently_coerced_to_list(self):
Expand Down