| 33 | class override_settings_tags(override_settings): |
| 34 | def enable(self): |
| 35 | super(override_settings_tags, self).enable() |
| 36 | # LEVEL_TAGS is a constant defined in the |
| 37 | # django.contrib.messages.storage.base module, so after changing |
| 38 | # settings.MESSAGE_TAGS, we need to update that constant too. |
| 39 | self.old_level_tags = base.LEVEL_TAGS |
| 40 | base.LEVEL_TAGS = utils.get_level_tags() |
| 41 | def disable(self): |
| 42 | super(override_settings_tags, self).disable() |
| 43 | base.LEVEL_TAGS = self.old_level_tags |
| 44 | |
| 45 | |
45 | | self._remembered_settings = {} |
46 | | for setting in self.restore_settings: |
47 | | if hasattr(settings, setting): |
48 | | self._remembered_settings[setting] = getattr(settings, setting) |
49 | | delattr(settings._wrapped, setting) |
50 | | # Backup these manually because we do not want them deleted. |
51 | | self._middleware_classes = settings.MIDDLEWARE_CLASSES |
52 | | self._template_context_processors = \ |
53 | | settings.TEMPLATE_CONTEXT_PROCESSORS |
54 | | self._installed_apps = settings.INSTALLED_APPS |
55 | | self._message_storage = settings.MESSAGE_STORAGE |
56 | | settings.MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__, |
57 | | self.storage_class.__name__) |
58 | | self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS |
59 | | settings.TEMPLATE_DIRS = () |
| 58 | self.settings_override = override_settings_tags( |
| 59 | TEMPLATE_DIRS = (), |
| 60 | MESSAGE_TAGS = '', |
| 61 | MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__, |
| 62 | self.storage_class.__name__), |
| 63 | ) |
| 64 | self.settings_override.enable() |
62 | | for setting in self.restore_settings: |
63 | | self.restore_setting(setting) |
64 | | # Restore these manually (see above). |
65 | | settings.MIDDLEWARE_CLASSES = self._middleware_classes |
66 | | settings.TEMPLATE_CONTEXT_PROCESSORS = \ |
67 | | self._template_context_processors |
68 | | settings.INSTALLED_APPS = self._installed_apps |
69 | | settings.MESSAGE_STORAGE = self._message_storage |
70 | | settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS |
71 | | |
72 | | def restore_setting(self, setting): |
73 | | if setting in self._remembered_settings: |
74 | | value = self._remembered_settings.pop(setting) |
75 | | setattr(settings, setting, value) |
76 | | elif hasattr(settings, setting): |
77 | | delattr(settings._wrapped, setting) |
| 67 | self.settings_override.disable() |
| 212 | @override_settings( |
| 213 | INSTALLED_APPS=filter( |
| 214 | lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS), |
| 215 | MIDDLEWARE_CLASSES=filter( |
| 216 | lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES), |
| 217 | TEMPLATE_CONTEXT_PROCESSORS=filter( |
| 218 | lambda p:'context_processors.messages' not in p, |
| 219 | settings.TEMPLATE_CONTEXT_PROCESSORS), |
| 220 | MESSAGE_LEVEL=constants.DEBUG |
| 221 | ) |
227 | | settings.MESSAGE_LEVEL = constants.DEBUG |
228 | | settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) |
229 | | settings.INSTALLED_APPS.remove( |
230 | | 'django.contrib.messages', |
231 | | ) |
232 | | settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES) |
233 | | settings.MIDDLEWARE_CLASSES.remove( |
234 | | 'django.contrib.messages.middleware.MessageMiddleware', |
235 | | ) |
236 | | settings.TEMPLATE_CONTEXT_PROCESSORS = \ |
237 | | list(settings.TEMPLATE_CONTEXT_PROCESSORS) |
238 | | settings.TEMPLATE_CONTEXT_PROCESSORS.remove( |
239 | | 'django.contrib.messages.context_processors.messages', |
240 | | ) |
| 237 | @override_settings( |
| 238 | INSTALLED_APPS=filter( |
| 239 | lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS), |
| 240 | MIDDLEWARE_CLASSES=filter( |
| 241 | lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES), |
| 242 | TEMPLATE_CONTEXT_PROCESSORS=filter( |
| 243 | lambda p:'context_processors.messages' not in p, |
| 244 | settings.TEMPLATE_CONTEXT_PROCESSORS), |
| 245 | MESSAGE_LEVEL=constants.DEBUG |
| 246 | ) |
256 | | settings.MESSAGE_LEVEL = constants.DEBUG |
257 | | settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) |
258 | | settings.INSTALLED_APPS.remove( |
259 | | 'django.contrib.messages', |
260 | | ) |
261 | | settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES) |
262 | | settings.MIDDLEWARE_CLASSES.remove( |
263 | | 'django.contrib.messages.middleware.MessageMiddleware', |
264 | | ) |
265 | | settings.TEMPLATE_CONTEXT_PROCESSORS = \ |
266 | | list(settings.TEMPLATE_CONTEXT_PROCESSORS) |
267 | | settings.TEMPLATE_CONTEXT_PROCESSORS.remove( |
268 | | 'django.contrib.messages.context_processors.messages', |
269 | | ) |
380 | | # LEVEL_TAGS is a constant defined in the |
381 | | # django.contrib.messages.storage.base module, so after changing |
382 | | # settings.MESSAGE_TAGS, we need to update that constant too. |
383 | | base.LEVEL_TAGS = utils.get_level_tags() |
384 | | try: |
385 | | storage = self.get_storage() |
386 | | storage.level = 0 |
387 | | add_level_messages(storage) |
388 | | tags = [msg.tags for msg in storage] |
389 | | self.assertEqual(tags, |
390 | | ['info', 'custom', 'extra-tag', '', 'bad', 'success']) |
391 | | finally: |
392 | | # Ensure the level tags constant is put back like we found it. |
393 | | self.restore_setting('MESSAGE_TAGS') |
394 | | base.LEVEL_TAGS = utils.get_level_tags() |
| 361 | ) |
| 362 | def test_custom_tags(self): |
| 363 | storage = self.get_storage() |
| 364 | storage.level = 0 |
| 365 | add_level_messages(storage) |
| 366 | tags = [msg.tags for msg in storage] |
| 367 | self.assertEqual(tags, |
| 368 | ['info', 'custom', 'extra-tag', '', 'bad', 'success']) |