Ticket #17895: 17895-more-docs-improved-api.diff

File 17895-more-docs-improved-api.diff, 3.8 KB (added by aaugustin, 3 years ago)
  • docs/ref/signals.txt

     
    477477.. data:: django.test.signals.setting_changed
    478478   :module:
    479479
    480 Sent when some :ref:`settings are overridden <overriding-settings>` with the
     480This signal is sent when the value of a setting is changed through the
    481481:meth:`django.test.TestCase.setting` context manager or the
    482482:func:`django.test.utils.override_settings` decorator/context manager.
    483483
     484It's actually sent twice: when the new value is applied (``"setup"``) and when
     485the original value is restored (``"teardown"``).
     486
    484487Arguments sent with this signal:
    485488
    486489``sender``
    487490    The settings handler.
    488491
    489492``setting``
    490     Same as sender
     493    The name of the setting.
    491494
    492495``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``.
    494498
     499``phase``
     500    ``"setup"`` when the setting is set to its new value, ``"teardown"`` when
     501    it's reset to its original value.
     502
    495503template_rendered
    496504-----------------
    497505
  • tests/regressiontests/settings_tests/tests.py

     
    7676
    7777testvalue = None
    7878
    79 def signal_callback(sender, setting, value, **kwargs):
     79def signal_callback(sender, setting, value, state, **kwargs):
    8080    if setting == 'TEST':
    8181        global testvalue
    82         testvalue = value
     82        testvalue = (value, state)
    8383
    8484signals.setting_changed.connect(signal_callback)
    8585
     
    131131    def test_signal_callback_context_manager(self):
    132132        self.assertRaises(AttributeError, getattr, settings, 'TEST')
    133133        with self.settings(TEST='override'):
    134             self.assertEqual(testvalue, 'override')
     134            self.assertEqual(testvalue, ('override', 'setup'))
     135        self.assertEqual(testvalue, (None, 'teardown'))
    135136
    136137    @override_settings(TEST='override')
    137138    def test_signal_callback_decorator(self):
    138         self.assertEqual(testvalue, 'override')
     139        self.assertEqual(testvalue, ('override', 'setup'))
    139140
    140141    #
    141142    # Regression tests for #10130: deleting settings.
  • django/test/utils.py

     
    167167    delattr(loader, RESTORE_LOADERS_ATTR)
    168168
    169169
    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 
    179170class override_settings(object):
    180171    """
    181172    Acts as either a decorator, or a context manager. If it's a decorator it
     
    215206        return inner
    216207
    217208    def enable(self):
    218         override = OverrideSettingsHolder(settings._wrapped)
     209        override = UserSettingsHolder(settings._wrapped)
    219210        for key, new_value in self.options.items():
    220211            setattr(override, key, new_value)
    221212        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')
    222216
    223217    def disable(self):
    224218        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
Back to Top