Fixed #17895 -- Made override_settings send the setting_changed signal both when a setting is overridden and when it's restored.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17708 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2012-03-13 23:02:31 +00:00
parent 2a0f4578ef
commit 4b14546215
3 changed files with 17 additions and 13 deletions

View File

@ -167,15 +167,6 @@ def restore_template_loaders():
delattr(loader, RESTORE_LOADERS_ATTR) delattr(loader, RESTORE_LOADERS_ATTR)
class OverrideSettingsHolder(UserSettingsHolder):
"""
A custom setting holder that sends a signal upon change.
"""
def __setattr__(self, name, value):
UserSettingsHolder.__setattr__(self, name, value)
setting_changed.send(sender=self.__class__, setting=name, value=value)
class override_settings(object): class override_settings(object):
""" """
Acts as either a decorator, or a context manager. If it's a decorator it Acts as either a decorator, or a context manager. If it's a decorator it
@ -215,10 +206,18 @@ class override_settings(object):
return inner return inner
def enable(self): def enable(self):
override = OverrideSettingsHolder(settings._wrapped) override = UserSettingsHolder(settings._wrapped)
for key, new_value in self.options.items(): for key, new_value in self.options.items():
setattr(override, key, new_value) setattr(override, key, new_value)
settings._wrapped = override settings._wrapped = override
for key, new_value in self.options.items():
setting_changed.send(sender=settings._wrapped.__class__,
setting=key, value=new_value)
def disable(self): def disable(self):
settings._wrapped = self.wrapped settings._wrapped = self.wrapped
for key in self.options:
new_value = getattr(settings, key, None)
setting_changed.send(sender=settings._wrapped.__class__,
setting=key, value=new_value)

View File

@ -477,20 +477,24 @@ setting_changed
.. data:: django.test.signals.setting_changed .. data:: django.test.signals.setting_changed
:module: :module:
Sent when some :ref:`settings are overridden <overriding-settings>` with the This signal is sent when the value of a setting is changed through the
:meth:`django.test.TestCase.setting` context manager or the :meth:`django.test.TestCase.setting` context manager or the
:func:`django.test.utils.override_settings` decorator/context manager. :func:`django.test.utils.override_settings` decorator/context manager.
It's actually sent twice: when the new value is applied ("setup") and when the
original value is restored ("teardown").
Arguments sent with this signal: Arguments sent with this signal:
``sender`` ``sender``
The settings handler. The settings handler.
``setting`` ``setting``
Same as sender The name of the setting.
``value`` ``value``
The new setting value. The value of the setting after the change. For settings that initially
don't exist, in the "teardown" phase, ``value`` is ``None``.
template_rendered template_rendered
----------------- -----------------

View File

@ -132,6 +132,7 @@ class SettingsTests(TestCase):
self.assertRaises(AttributeError, getattr, settings, 'TEST') self.assertRaises(AttributeError, getattr, settings, 'TEST')
with self.settings(TEST='override'): with self.settings(TEST='override'):
self.assertEqual(testvalue, 'override') self.assertEqual(testvalue, 'override')
self.assertEqual(testvalue, None)
@override_settings(TEST='override') @override_settings(TEST='override')
def test_signal_callback_decorator(self): def test_signal_callback_decorator(self):