Skip to content

Commit 519d6c7

Browse files
committed
Resolved issue "working outside of application context" #220.
1 parent f07233b commit 519d6c7

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

flask_mongoengine/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _patch_base_field(obj, name):
5454
@param obj: The object whose footprint to locate the class.
5555
@param name: Name of the class to locate.
5656
"""
57-
57+
5858
# locate class
5959
cls = getattr(obj, name)
6060
if not inspect.isclass(cls):
@@ -111,6 +111,10 @@ def __init__(self, app=None, config=None):
111111
self.init_app(app, config)
112112

113113
def init_app(self, app, config=None):
114+
from flask import Flask
115+
if not app or not isinstance(app, Flask):
116+
raise Exception('Invalid Flask application instance')
117+
114118
app.extensions = getattr(app, 'extensions', {})
115119

116120
# Make documents JSON serializable
@@ -131,7 +135,7 @@ def init_app(self, app, config=None):
131135
config = app.config
132136

133137
# Obtain db connection
134-
connection = create_connection(config)
138+
connection = create_connection(config, app)
135139

136140
# Store objects in application instance
137141
# so that multiple apps do not end up

flask_mongoengine/connection.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
_tmpdir = None
2020
_conn = None
2121
_process = None
22+
_app_instance = current_app
2223

2324
class InvalidSettingsError(Exception):
2425
pass
@@ -97,9 +98,9 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
9798
conn_settings.pop('password', None)
9899
conn_settings.pop('authentication_source', None)
99100

100-
is_test = current_app.config.get('TESTING', False)
101-
temp_db = current_app.config.get('TEMP_DB', False)
102-
preserved = current_app.config.get('PRESERVE_TEMP_DB', False)
101+
is_test = _app_instance.config.get('TESTING', False)
102+
temp_db = _app_instance.config.get('TEMP_DB', False)
103+
preserved = _app_instance.config.get('PRESERVE_TEMP_DB', False)
103104

104105
# Validation
105106
_validate_settings(is_test, temp_db, preserved, conn_host)
@@ -313,7 +314,7 @@ def fetch_connection_settings(config, removePass=True):
313314
# Connection settings provided in standard format.
314315
return _resolve_settings(config, removePass)
315316

316-
def create_connection(config):
317+
def create_connection(config, app):
317318
"""
318319
Connection is created based on application configuration
319320
setting. Application settings which is enabled as TESTING
@@ -346,9 +347,10 @@ def create_connection(config):
346347
>> app.config['TEMP_DB_LOC'] = '/path/to/temp_dir/'
347348
348349
@param config: Flask-MongoEngine application configuration.
349-
350+
@param app: instance of flask.Flask
350351
"""
351-
global _connection_settings
352+
global _connection_settings, _app_instance
353+
_app_instance = app
352354

353355
if config is None or not isinstance(config, dict):
354356
raise Exception("Invalid application configuration");

0 commit comments

Comments
 (0)