From 0f89bffd927f5e4ad6d8ffc37f46a2e7678f7364 Mon Sep 17 00:00:00 2001 From: Misha Date: Tue, 19 Sep 2017 16:16:44 +0400 Subject: [PATCH 1/2] Added support for application factory pattern This will allow to initialize the application when application context is available. --- flask_mongorest/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/flask_mongorest/__init__.py b/flask_mongorest/__init__.py index f7da37f2..4598acdd 100644 --- a/flask_mongorest/__init__.py +++ b/flask_mongorest/__init__.py @@ -1,13 +1,22 @@ -from flask import Blueprint +from flask import Flask, Blueprint from flask_mongorest.methods import Create, BulkUpdate, List class MongoRest(object): - def __init__(self, app, **kwargs): + def __init__(self, app=None, **kwargs): + self.app = None + + if app is not None: + self.init_app(app, config) + + def init_app(self, app): + if not app or not isinstance(app, Flask): + raise Exception('Invalid Flask application instance') + self.app = app self.url_prefix = kwargs.pop('url_prefix', '') app.register_blueprint(Blueprint(self.url_prefix, __name__, template_folder='templates')) - + def register(self, **kwargs): def decorator(klass): # Construct a url based on a 'name' kwarg with a fallback to the From 66ecd62408bcaad70f8a0665b656ca08e7efefaf Mon Sep 17 00:00:00 2001 From: Misha Date: Tue, 19 Sep 2017 17:41:36 +0400 Subject: [PATCH 2/2] Update __init__.py --- flask_mongorest/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/flask_mongorest/__init__.py b/flask_mongorest/__init__.py index 4598acdd..8450ac9e 100644 --- a/flask_mongorest/__init__.py +++ b/flask_mongorest/__init__.py @@ -5,18 +5,18 @@ class MongoRest(object): def __init__(self, app=None, **kwargs): self.app = None - + self.url_prefix = kwargs.pop('url_prefix', '') + if app is not None: - self.init_app(app, config) - + self.init_app(app) + def init_app(self, app): if not app or not isinstance(app, Flask): raise Exception('Invalid Flask application instance') - self.app = app - self.url_prefix = kwargs.pop('url_prefix', '') + app.register_blueprint(Blueprint(self.url_prefix, __name__, template_folder='templates')) - + def register(self, **kwargs): def decorator(klass): # Construct a url based on a 'name' kwarg with a fallback to the