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:
parent
2a0f4578ef
commit
4b14546215
|
@ -167,15 +167,6 @@ def restore_template_loaders():
|
|||
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):
|
||||
"""
|
||||
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
|
||||
|
||||
def enable(self):
|
||||
override = OverrideSettingsHolder(settings._wrapped)
|
||||
override = UserSettingsHolder(settings._wrapped)
|
||||
for key, new_value in self.options.items():
|
||||
setattr(override, key, new_value)
|
||||
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):
|
||||
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)
|
||||
|
||||
|
|
|
@ -477,20 +477,24 @@ setting_changed
|
|||
.. data:: django.test.signals.setting_changed
|
||||
: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
|
||||
: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:
|
||||
|
||||
``sender``
|
||||
The settings handler.
|
||||
|
||||
``setting``
|
||||
Same as sender
|
||||
The name of the setting.
|
||||
|
||||
``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
|
||||
-----------------
|
||||
|
|
|
@ -132,6 +132,7 @@ class SettingsTests(TestCase):
|
|||
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
||||
with self.settings(TEST='override'):
|
||||
self.assertEqual(testvalue, 'override')
|
||||
self.assertEqual(testvalue, None)
|
||||
|
||||
@override_settings(TEST='override')
|
||||
def test_signal_callback_decorator(self):
|
||||
|
|
Loading…
Reference in New Issue