Skip to content

Commit

Permalink
Merge pull request #3715 from Cheglader/settings_errors
Browse files Browse the repository at this point in the history
Raise error when setting a removed rest_framework setting for #3644
  • Loading branch information
tomchristie committed Dec 18, 2015
2 parents 6eb3a42 + c389aeb commit 61e7f7b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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

0 comments on commit 61e7f7b

Please sign in to comment.