Skip to content

Instantly share code, notes, and snippets.

@sprytnyk
Created November 10, 2017 12:04
Show Gist options
  • Save sprytnyk/c17a8383b8d87fee1e99f67e86370915 to your computer and use it in GitHub Desktop.
Save sprytnyk/c17a8383b8d87fee1e99f67e86370915 to your computer and use it in GitHub Desktop.
Wrap Flask app context to all the class methods
class Wrapper:
def __init__(self, wrapped_class):
self.wrapped_class = wrapped_class()
def __getattr__(self, item):
attr = self.wrapped_class.__getattribute__(item)
if callable(attr):
@wraps(attr)
def wrap(*args, **kwargs):
with app.app_context():
return attr(*args, **kwargs)
return wrap
else:
return attr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment