Merge pull request #3715 from Cheglader/settings_errors

Raise error when setting a removed rest_framework setting for #3644
This commit is contained in:
Tom Christie 2015-12-18 11:19:06 +00:00
commit 61e7f7b0cc
2 changed files with 31 additions and 1 deletions

View File

@ -19,6 +19,8 @@ back to the defaults.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
import warnings
from django.conf import settings from django.conf import settings
from django.test.signals import setting_changed from django.test.signals import setting_changed
from django.utils import six from django.utils import six
@ -135,6 +137,12 @@ IMPORT_STRINGS = (
) )
# List of settings that have been removed
REMOVED_SETTINGS = (
"PAGINATE_BY", "PAGINATE_BY_PARAM", "MAX_PAGINATE_BY",
)
def perform_import(val, setting_name): def perform_import(val, setting_name):
""" """
If the given setting is a string import notation, If the given setting is a string import notation,
@ -177,7 +185,7 @@ class APISettings(object):
""" """
def __init__(self, user_settings=None, defaults=None, import_strings=None): def __init__(self, user_settings=None, defaults=None, import_strings=None):
if user_settings: if user_settings:
self._user_settings = user_settings self._user_settings = self.__check_user_settings(user_settings)
self.defaults = defaults or DEFAULTS self.defaults = defaults or DEFAULTS
self.import_strings = import_strings or IMPORT_STRINGS self.import_strings = import_strings or IMPORT_STRINGS
@ -206,6 +214,13 @@ class APISettings(object):
setattr(self, attr, val) setattr(self, attr, val)
return 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) api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)

View File

@ -1,5 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import warnings
from django.test import TestCase from django.test import TestCase
from rest_framework.settings import APISettings from rest_framework.settings import APISettings
@ -18,6 +20,19 @@ class TestSettings(TestCase):
with self.assertRaises(ImportError): with self.assertRaises(ImportError):
settings.DEFAULT_RENDERER_CLASSES 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): class TestSettingTypes(TestCase):
def test_settings_consistently_coerced_to_list(self): def test_settings_consistently_coerced_to_list(self):