diff -r 87cceb6af4c4 django/conf/__init__.py
a
|
b
|
|
16 | 16 | |
17 | 17 | ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE" |
18 | 18 | |
19 | | class LazySettings(LazyObject): |
| 19 | |
| 20 | class LazySettings(object): |
20 | 21 | """ |
21 | 22 | A lazy proxy for either global Django settings or a custom settings object. |
22 | 23 | The user can manually configure settings prior to using them. Otherwise, |
23 | 24 | Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE. |
24 | 25 | """ |
| 26 | def __init__(self): |
| 27 | self.configured = False |
| 28 | |
| 29 | def __getattr__(self, name): |
| 30 | if self.configured: |
| 31 | raise AttributeError("Settings object has no attribute '%s'" % name) |
| 32 | else: |
| 33 | self._setup() |
| 34 | return getattr(self, name) |
| 35 | |
| 36 | def __dir__(self): |
| 37 | if not self.configured: |
| 38 | self._setup() |
| 39 | return self.__dict__.keys() |
| 40 | |
| 41 | # For Python < 2.6: |
| 42 | __members__ = property(lambda self: self.__dir__()) |
| 43 | |
25 | 44 | def _setup(self): |
26 | 45 | """ |
27 | 46 | Load the settings module pointed to by the environment variable. This |
… |
… |
|
37 | 56 | # problems with Python's interactive help. |
38 | 57 | raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE) |
39 | 58 | |
40 | | self._wrapped = Settings(settings_module) |
| 59 | self._load(Settings(settings_module)) |
| 60 | self.configured = True |
| 61 | |
| 62 | def _load(self, obj): |
| 63 | for name in dir(obj): |
| 64 | if not name.startswith('__'): |
| 65 | setattr(self, name, getattr(obj, name)) |
41 | 66 | |
42 | 67 | def configure(self, default_settings=global_settings, **options): |
43 | 68 | """ |
… |
… |
|
45 | 70 | parameter sets where to retrieve any unspecified values from (its |
46 | 71 | argument must support attribute access (__getattr__)). |
47 | 72 | """ |
48 | | if self._wrapped != None: |
| 73 | if self.configured: |
49 | 74 | raise RuntimeError('Settings already configured.') |
50 | | holder = UserSettingsHolder(default_settings) |
51 | | for name, value in options.items(): |
52 | | setattr(holder, name, value) |
53 | | self._wrapped = holder |
| 75 | self._load(default_settings) |
| 76 | self.__dict__.update(options) |
| 77 | self.configured = True |
54 | 78 | |
55 | | def configured(self): |
56 | | """ |
57 | | Returns True if the settings have already been configured. |
58 | | """ |
59 | | return bool(self._wrapped) |
60 | | configured = property(configured) |
61 | 79 | |
62 | 80 | class Settings(object): |
63 | 81 | def __init__(self, settings_module): |
… |
… |
|
114 | 132 | os.environ['TZ'] = self.TIME_ZONE |
115 | 133 | time.tzset() |
116 | 134 | |
117 | | class UserSettingsHolder(object): |
118 | | """ |
119 | | Holder for user configured settings. |
120 | | """ |
121 | | # SETTINGS_MODULE doesn't make much sense in the manually configured |
122 | | # (standalone) case. |
123 | | SETTINGS_MODULE = None |
124 | | |
125 | | def __init__(self, default_settings): |
126 | | """ |
127 | | Requests for configuration variables not in this class are satisfied |
128 | | from the module specified in default_settings (if possible). |
129 | | """ |
130 | | self.default_settings = default_settings |
131 | | |
132 | | def __getattr__(self, name): |
133 | | return getattr(self.default_settings, name) |
134 | | |
135 | | def __dir__(self): |
136 | | return self.__dict__.keys() + dir(self.default_settings) |
137 | | |
138 | | # For Python < 2.6: |
139 | | __members__ = property(lambda self: self.__dir__()) |
140 | 135 | |
141 | 136 | settings = LazySettings() |
142 | 137 | |
diff -r 87cceb6af4c4 django/contrib/messages/tests/base.py
a
|
b
|
|
40 | 40 | for setting in self.restore_settings: |
41 | 41 | if hasattr(settings, setting): |
42 | 42 | self._remembered_settings[setting] = getattr(settings, setting) |
43 | | delattr(settings._wrapped, setting) |
| 43 | delattr(settings, setting) |
44 | 44 | # Backup these manually because we do not want them deleted. |
45 | 45 | self._middleware_classes = settings.MIDDLEWARE_CLASSES |
46 | 46 | self._template_context_processors = \ |
… |
… |
|
65 | 65 | value = self._remembered_settings.pop(setting) |
66 | 66 | setattr(settings, setting, value) |
67 | 67 | elif hasattr(settings, setting): |
68 | | delattr(settings._wrapped, setting) |
| 68 | delattr(settings, setting) |
69 | 69 | |
70 | 70 | def get_request(self): |
71 | 71 | return http.HttpRequest() |
diff -r 87cceb6af4c4 tests/regressiontests/comment_tests/tests/app_api_tests.py
a
|
b
|
|
41 | 41 | del settings.INSTALLED_APPS[-1] |
42 | 42 | settings.COMMENTS_APP = self.old_comments_app |
43 | 43 | if settings.COMMENTS_APP is None: |
44 | | del settings._wrapped.COMMENTS_APP |
| 44 | del settings.COMMENTS_APP |
45 | 45 | |
46 | 46 | def testGetCommentApp(self): |
47 | 47 | from regressiontests.comment_tests import custom_comments |
diff -r 87cceb6af4c4 tests/regressiontests/settings_tests/tests.py
a
|
b
|
|
7 | 7 | # Regression tests for #10130: deleting settings. |
8 | 8 | # |
9 | 9 | |
| 10 | def setUp(self): |
| 11 | self.DEBUG = settings.DEBUG |
| 12 | |
| 13 | def tearDown(self): |
| 14 | settings.DEBUG = self.DEBUG |
| 15 | |
10 | 16 | def test_settings_delete(self): |
11 | 17 | settings.TEST = 'test' |
12 | 18 | self.assertEqual('test', settings.TEST) |
13 | 19 | del settings.TEST |
14 | 20 | self.assertRaises(AttributeError, getattr, settings, 'TEST') |
15 | 21 | |
16 | | def test_settings_delete_wrapped(self): |
17 | | self.assertRaises(TypeError, delattr, settings, '_wrapped') |
| 22 | # Check with something that will definitely already exist |
| 23 | # in the settings module |
| 24 | del settings.DEBUG |
| 25 | self.assertRaises(AttributeError, getattr, settings, 'DEBUG') |