Ticket #17895: 17895-more-docs-improved-api.diff
File 17895-more-docs-improved-api.diff, 3.8 KB (added by , 13 years ago) |
---|
-
docs/ref/signals.txt
477 477 .. data:: django.test.signals.setting_changed 478 478 :module: 479 479 480 Sent when some :ref:`settings are overridden <overriding-settings>` with the480 This signal is sent when the value of a setting is changed through the 481 481 :meth:`django.test.TestCase.setting` context manager or the 482 482 :func:`django.test.utils.override_settings` decorator/context manager. 483 483 484 It's actually sent twice: when the new value is applied (``"setup"``) and when 485 the original value is restored (``"teardown"``). 486 484 487 Arguments sent with this signal: 485 488 486 489 ``sender`` 487 490 The settings handler. 488 491 489 492 ``setting`` 490 Same as sender493 The name of the setting. 491 494 492 495 ``value`` 493 The new setting value. 496 The value of the setting after the change. For settings that initially 497 don't exist, in the teardown phase, ``value`` is ``None``. 494 498 499 ``phase`` 500 ``"setup"`` when the setting is set to its new value, ``"teardown"`` when 501 it's reset to its original value. 502 495 503 template_rendered 496 504 ----------------- 497 505 -
tests/regressiontests/settings_tests/tests.py
76 76 77 77 testvalue = None 78 78 79 def signal_callback(sender, setting, value, **kwargs):79 def signal_callback(sender, setting, value, state, **kwargs): 80 80 if setting == 'TEST': 81 81 global testvalue 82 testvalue = value82 testvalue = (value, state) 83 83 84 84 signals.setting_changed.connect(signal_callback) 85 85 … … 131 131 def test_signal_callback_context_manager(self): 132 132 self.assertRaises(AttributeError, getattr, settings, 'TEST') 133 133 with self.settings(TEST='override'): 134 self.assertEqual(testvalue, 'override') 134 self.assertEqual(testvalue, ('override', 'setup')) 135 self.assertEqual(testvalue, (None, 'teardown')) 135 136 136 137 @override_settings(TEST='override') 137 138 def test_signal_callback_decorator(self): 138 self.assertEqual(testvalue, 'override')139 self.assertEqual(testvalue, ('override', 'setup')) 139 140 140 141 # 141 142 # Regression tests for #10130: deleting settings. -
django/test/utils.py
167 167 delattr(loader, RESTORE_LOADERS_ATTR) 168 168 169 169 170 class OverrideSettingsHolder(UserSettingsHolder):171 """172 A custom setting holder that sends a signal upon change.173 """174 def __setattr__(self, name, value):175 UserSettingsHolder.__setattr__(self, name, value)176 setting_changed.send(sender=self.__class__, setting=name, value=value)177 178 179 170 class override_settings(object): 180 171 """ 181 172 Acts as either a decorator, or a context manager. If it's a decorator it … … 215 206 return inner 216 207 217 208 def enable(self): 218 override = OverrideSettingsHolder(settings._wrapped)209 override = UserSettingsHolder(settings._wrapped) 219 210 for key, new_value in self.options.items(): 220 211 setattr(override, key, new_value) 221 212 settings._wrapped = override 213 for key, new_value in self.options.items(): 214 setting_changed.send(sender=settings._wrapped.__class__, 215 setting=key, value=new_value, state='setup') 222 216 223 217 def disable(self): 224 218 settings._wrapped = self.wrapped 219 for key in self.options: 220 new_value = getattr(settings, key, None) 221 setting_changed.send(sender=settings._wrapped.__class__, 222 setting=key, value=new_value, state='teardown') 223