Ticket #17895: 17895.diff

File 17895.diff, 3.4 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
     480Sent when the value of a setting is changed with the
    481481:meth:`django.test.TestCase.setting` context manager or the
    482482:func:`django.test.utils.override_settings` decorator/context manager.
    483483
     
    492492``value``
    493493    The new setting value.
    494494
     495``state``
     496    ``"pre"`` when the setting is set to a new value, ``"post"`` when it's
     497    reset to its original value.
     498
    495499template_rendered
    496500-----------------
    497501
  • 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='pre')
    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='post')
     223
  • 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', 'pre'))
     135        self.assertEqual(testvalue, (None, 'post'))
    135136
    136137    @override_settings(TEST='override')
    137138    def test_signal_callback_decorator(self):
    138         self.assertEqual(testvalue, 'override')
     139        self.assertEqual(testvalue, ('override', 'pre'))
    139140
    140141    #
    141142    # Regression tests for #10130: deleting settings.
Back to Top