Skip to content

Commit 61e7f7b

Browse files
committed
Merge pull request #3715 from Cheglader/settings_errors
Raise error when setting a removed rest_framework setting for #3644
2 parents 6eb3a42 + c389aeb commit 61e7f7b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

rest_framework/settings.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"""
2020
from __future__ import unicode_literals
2121

22+
import warnings
23+
2224
from django.conf import settings
2325
from django.test.signals import setting_changed
2426
from django.utils import six
@@ -135,6 +137,12 @@
135137
)
136138

137139

140+
# List of settings that have been removed
141+
REMOVED_SETTINGS = (
142+
"PAGINATE_BY", "PAGINATE_BY_PARAM", "MAX_PAGINATE_BY",
143+
)
144+
145+
138146
def perform_import(val, setting_name):
139147
"""
140148
If the given setting is a string import notation,
@@ -177,7 +185,7 @@ class APISettings(object):
177185
"""
178186
def __init__(self, user_settings=None, defaults=None, import_strings=None):
179187
if user_settings:
180-
self._user_settings = user_settings
188+
self._user_settings = self.__check_user_settings(user_settings)
181189
self.defaults = defaults or DEFAULTS
182190
self.import_strings = import_strings or IMPORT_STRINGS
183191

@@ -206,6 +214,13 @@ def __getattr__(self, attr):
206214
setattr(self, attr, val)
207215
return val
208216

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

210225
api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)
211226

tests/test_settings.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import unicode_literals
22

3+
import warnings
4+
35
from django.test import TestCase
46

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

23+
def test_warning_raised_on_removed_setting(self):
24+
"""
25+
Make sure user is alerted with an error when a removed setting
26+
is set.
27+
"""
28+
with warnings.catch_warnings(record=True) as w:
29+
warnings.simplefilter("always")
30+
APISettings({
31+
'MAX_PAGINATE_BY': 100
32+
})
33+
self.assertEqual(len(w), 1)
34+
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
35+
2136

2237
class TestSettingTypes(TestCase):
2338
def test_settings_consistently_coerced_to_list(self):

0 commit comments

Comments
 (0)