Skip to content

Instantly share code, notes, and snippets.

@plaes
Created August 25, 2012 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plaes/3470780 to your computer and use it in GitHub Desktop.
Save plaes/3470780 to your computer and use it in GitHub Desktop.
Flask context decorator for Celery tasks
def push_context(fn):
@wraps(fn)
def decorator(*args, **kwargs):
app_host = app.config.get('SERVER_NAME')
app_root = app.config.get('APPLICATION_ROOT')
base_url = 'https://{}/'.format(app_host if app_host else 'localhost')
if app_root:
base_url += app.root.lstrip('/')
app.test_request_context(base_url=base_url).push()
return fn(*args, **kwargs)
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment