Created
June 21, 2011 21:47
-
-
Save codysoyland/1038987 to your computer and use it in GitHub Desktop.
Context manager for monkey-patching django settings (useful in unit tests)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example usage: | |
# with temporary_settings(CELERY_ALWAYS_EAGER=True): | |
# run_task.delay() # runs task with eager setting enabled. | |
from contextlib import contextmanager | |
from django.conf import settings | |
@contextmanager | |
def temporary_settings(**temp_settings): | |
orig_settings = {} | |
for key, value in temp_settings.iteritems(): | |
if hasattr(settings, key): | |
orig_settings[key] = getattr(settings, key) | |
setattr(settings, key, value) | |
yield | |
for key, value in temp_settings.iteritems(): | |
if orig_settings.has_key(key): | |
setattr(settings, key, orig_settings[key]) | |
else: | |
delattr(settings, key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment