Skip to content

Instantly share code, notes, and snippets.

@ianawilson
Last active December 23, 2015 00:29
Show Gist options
  • Save ianawilson/6553472 to your computer and use it in GitHub Desktop.
Save ianawilson/6553472 to your computer and use it in GitHub Desktop.
temporary setting decorator for testing
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