mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 11:33:59 +03:00
Merge pull request #3715 from Cheglader/settings_errors
Raise error when setting a removed rest_framework setting for #3644
This commit is contained in:
commit
61e7f7b0cc
|
@ -19,6 +19,8 @@ back to the defaults.
|
|||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import warnings
|
||||
|
||||
from django.conf import settings
|
||||
from django.test.signals import setting_changed
|
||||
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):
|
||||
"""
|
||||
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):
|
||||
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
|
||||
|
||||
|
@ -206,6 +214,13 @@ class APISettings(object):
|
|||
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)
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import warnings
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.settings import APISettings
|
||||
|
@ -18,6 +20,19 @@ class TestSettings(TestCase):
|
|||
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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user