Created
November 10, 2017 12:04
-
-
Save sprytnyk/c17a8383b8d87fee1e99f67e86370915 to your computer and use it in GitHub Desktop.
Wrap Flask app context to all the class methods
This file contains hidden or 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
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