Code

Ticket #18824: 18824-1.diff

File 18824-1.diff, 1.9 KB (added by claudep, 20 months ago)

UserSettingsHolder keeping track of deleted settings

Line 
1diff --git a/django/conf/__init__.py b/django/conf/__init__.py
2index 35f4ec8..f4d17ca 100644
3--- a/django/conf/__init__.py
4+++ b/django/conf/__init__.py
5@@ -152,11 +152,22 @@ class UserSettingsHolder(BaseSettings):
6         Requests for configuration variables not in this class are satisfied
7         from the module specified in default_settings (if possible).
8         """
9+        self.__dict__['_deleted'] = set()
10         self.default_settings = default_settings
11 
12     def __getattr__(self, name):
13+        if name in self._deleted:
14+            raise AttributeError
15         return getattr(self.default_settings, name)
16 
17+    def __setattr__(self, name, value):
18+        self._deleted.discard(name)
19+        return super(UserSettingsHolder, self).__setattr__(name, value)
20+
21+    def __delattr__(self, name):
22+        self._deleted.add(name)
23+        return super(UserSettingsHolder, self).__delattr__(name)
24+
25     def __dir__(self):
26         return list(self.__dict__) + dir(self.default_settings)
27 
28diff --git a/tests/regressiontests/settings_tests/tests.py b/tests/regressiontests/settings_tests/tests.py
29index ffcb79e..7225fb0 100644
30--- a/tests/regressiontests/settings_tests/tests.py
31+++ b/tests/regressiontests/settings_tests/tests.py
32@@ -148,6 +148,16 @@ class SettingsTests(TestCase):
33     def test_settings_delete_wrapped(self):
34         self.assertRaises(TypeError, delattr, settings, '_wrapped')
35 
36+    def test_override_settings_delete(self):
37+        """
38+        Allow deletion of a setting in an overriden settings set (#18824)
39+        """
40+        previous_i18n = settings.USE_I18N
41+        with self.settings(USE_I18N=False):
42+            del settings.USE_I18N
43+            self.assertRaises(AttributeError, getattr, settings, 'USE_I18N')
44+        self.assertEqual(settings.USE_I18N, previous_i18n)
45+
46     def test_allowed_include_roots_string(self):
47         """
48         ALLOWED_INCLUDE_ROOTS is not allowed to be incorrectly set to a string