Also allowed a non-overridden setting to be deleted

Refs #20032, #18824. Thanks ztorstri at gmail.com for the report.
This commit is contained in:
Claude Paroz 2014-04-12 15:31:37 +02:00
parent 3e3a7372f5
commit 3417ba0309
3 changed files with 13 additions and 2 deletions

View File

@ -153,11 +153,12 @@ class UserSettingsHolder(BaseSettings):
def __setattr__(self, name, value): def __setattr__(self, name, value):
self._deleted.discard(name) self._deleted.discard(name)
return super(UserSettingsHolder, self).__setattr__(name, value) super(UserSettingsHolder, self).__setattr__(name, value)
def __delattr__(self, name): def __delattr__(self, name):
self._deleted.add(name) self._deleted.add(name)
return super(UserSettingsHolder, self).__delattr__(name) if hasattr(self, name):
super(UserSettingsHolder, self).__delattr__(name)
def __dir__(self): def __dir__(self):
return list(self.__dict__) + dir(self.default_settings) return list(self.__dict__) + dir(self.default_settings)

View File

@ -1193,6 +1193,11 @@ have been overridden, like this::
del settings.LOGIN_URL del settings.LOGIN_URL
... ...
.. versionchanged:: 1.7
Previously, you could only simulate the deletion of a setting which was
explicitely overridden.
When overriding settings, make sure to handle the cases in which your app's When overriding settings, make sure to handle the cases in which your app's
code uses a cache or similar feature that retains state even if the setting is code uses a cache or similar feature that retains state even if the setting is
changed. Django provides the :data:`django.test.signals.setting_changed` changed. Django provides the :data:`django.test.signals.setting_changed`

View File

@ -232,10 +232,15 @@ class SettingsTests(TestCase):
Allow deletion of a setting in an overridden settings set (#18824) Allow deletion of a setting in an overridden settings set (#18824)
""" """
previous_i18n = settings.USE_I18N previous_i18n = settings.USE_I18N
previous_l10n = settings.USE_L10N
with self.settings(USE_I18N=False): with self.settings(USE_I18N=False):
del settings.USE_I18N del settings.USE_I18N
self.assertRaises(AttributeError, getattr, settings, 'USE_I18N') self.assertRaises(AttributeError, getattr, settings, 'USE_I18N')
# Should also work for a non-overridden setting
del settings.USE_L10N
self.assertRaises(AttributeError, getattr, settings, 'USE_L10N')
self.assertEqual(settings.USE_I18N, previous_i18n) self.assertEqual(settings.USE_I18N, previous_i18n)
self.assertEqual(settings.USE_L10N, previous_l10n)
def test_override_settings_nested(self): def test_override_settings_nested(self):
""" """