Last active
December 23, 2015 00:29
-
-
Save ianawilson/6553472 to your computer and use it in GitHub Desktop.
temporary setting decorator for testing
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
from functools import wraps | |
def temporary_setting(setting_name, value): | |
def decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
from django.conf import settings | |
# save and disable the setting | |
old_value = getattr(settings, setting_name) | |
setattr(settings, setting_name, value) | |
# run the actual test | |
try: | |
func(*args, **kwargs) | |
except: | |
# set our value back, then keep raising that exception | |
setattr(settings, setting_name, old_value) | |
raise | |
else: | |
setattr(settings, setting_name, old_value) | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment