mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-25 19:43:47 +03:00
Fix override_settings
compat (#5668)
* Add test checking override_settings compat * Refresh APISettings, rather than replace Fix suggested by @daggaz https://github.com/encode/django-rest-framework/issues/2466#issuecomment-344297213
This commit is contained in:
parent
1692feb535
commit
4a200d5e66
|
@ -200,6 +200,7 @@ class APISettings(object):
|
||||||
self._user_settings = self.__check_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
|
||||||
|
self._cached_attrs = set()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def user_settings(self):
|
def user_settings(self):
|
||||||
|
@ -223,6 +224,7 @@ class APISettings(object):
|
||||||
val = perform_import(val, attr)
|
val = perform_import(val, attr)
|
||||||
|
|
||||||
# Cache the result
|
# Cache the result
|
||||||
|
self._cached_attrs.add(attr)
|
||||||
setattr(self, attr, val)
|
setattr(self, attr, val)
|
||||||
return val
|
return val
|
||||||
|
|
||||||
|
@ -233,15 +235,21 @@ class APISettings(object):
|
||||||
raise RuntimeError("The '%s' setting has been removed. Please refer to '%s' for available settings." % (setting, SETTINGS_DOC))
|
raise RuntimeError("The '%s' setting has been removed. Please refer to '%s' for available settings." % (setting, SETTINGS_DOC))
|
||||||
return user_settings
|
return user_settings
|
||||||
|
|
||||||
|
def reload(self):
|
||||||
|
for attr in self._cached_attrs:
|
||||||
|
delattr(self, attr)
|
||||||
|
self._cached_attrs.clear()
|
||||||
|
if hasattr(self, '_user_settings'):
|
||||||
|
delattr(self, '_user_settings')
|
||||||
|
|
||||||
|
|
||||||
api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)
|
api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)
|
||||||
|
|
||||||
|
|
||||||
def reload_api_settings(*args, **kwargs):
|
def reload_api_settings(*args, **kwargs):
|
||||||
global api_settings
|
setting = kwargs['setting']
|
||||||
setting, value = kwargs['setting'], kwargs['value']
|
|
||||||
if setting == 'REST_FRAMEWORK':
|
if setting == 'REST_FRAMEWORK':
|
||||||
api_settings = APISettings(value, DEFAULTS, IMPORT_STRINGS)
|
api_settings.reload()
|
||||||
|
|
||||||
|
|
||||||
setting_changed.connect(reload_api_settings)
|
setting_changed.connect(reload_api_settings)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase, override_settings
|
||||||
|
|
||||||
from rest_framework.settings import APISettings
|
from rest_framework.settings import APISettings, api_settings
|
||||||
|
|
||||||
|
|
||||||
class TestSettings(TestCase):
|
class TestSettings(TestCase):
|
||||||
|
@ -28,6 +28,23 @@ class TestSettings(TestCase):
|
||||||
'MAX_PAGINATE_BY': 100
|
'MAX_PAGINATE_BY': 100
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def test_compatibility_with_override_settings(self):
|
||||||
|
"""
|
||||||
|
Ref #5658 & #2466: Documented usage of api_settings
|
||||||
|
is bound at import time:
|
||||||
|
|
||||||
|
from rest_framework.settings import api_settings
|
||||||
|
|
||||||
|
setting_changed signal hook must ensure bound instance
|
||||||
|
is refreshed.
|
||||||
|
"""
|
||||||
|
assert api_settings.PAGE_SIZE is None, "Checking a known default should be None"
|
||||||
|
|
||||||
|
with override_settings(REST_FRAMEWORK={'PAGE_SIZE': 10}):
|
||||||
|
assert api_settings.PAGE_SIZE == 10, "Setting should have been updated"
|
||||||
|
|
||||||
|
assert api_settings.PAGE_SIZE is None, "Setting should have been restored"
|
||||||
|
|
||||||
|
|
||||||
class TestSettingTypes(TestCase):
|
class TestSettingTypes(TestCase):
|
||||||
def test_settings_consistently_coerced_to_list(self):
|
def test_settings_consistently_coerced_to_list(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user