Integrating Restless
Closed this issue · 2 comments
Hello,
Thanks Roman for this great "backbone". I've been working on extending it for a couple of weeks now (https://github.com/RockyRoad29/flask-kit), including documentation.
Now trying to use Flask-Restless I have a new AppFactory problem.
This extension implements init_app, but it takes extra arguments like 'db' or 'session'.
Should I, as you did for gravatar and toolbar, declare it as lambda at the end of ext.py
?
gravatar = lambda app: Gravatar(app, size=50) # has no init_app()
toolbar = lambda app: DebugToolbarExtension(app) # has no init_app()
from flask.ext.restless import APIManager
api_manager = lambda app: APIManager(app, flask_sqlalchemy_db=db) # needs db as init_app argument
But I realize I don't understand how it is supposed to work. Can you help me about that ?
Another option might be to extend AppFactory._bind_extensions()
to accept extra parameters but I think it might get tricky. Any idea ?
I think I got it: the lambda was only used if init_app was absent.
So I replaced:
if getattr(ext, 'init_app', False):
ext.init_app(self.app)
else:
ext(self.app)
with
if callable(ext):
ext(self.app)
elif getattr(ext, 'init_app', False):
ext.init_app(self.app)
else:
raise NoExtensionException('{e_name} extension has no init_app. Can\'t initialize'.format(e_name=e_name))
and then it ext.py
:
api_manager = APIManager()
init_api_manager = lambda app: api_manager.init_app(app, flask_sqlalchemy_db=db) # needs db as init_app argument
and in settings.py
:
EXTENSIONS = ['ext.db',
...
'ext.init_api_manager',
]
\o/ Have a great day :)