Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix app out of context when create connection #225

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions flask_mongoengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ def _include_mongoengine(obj):
_patch_base_field(obj, key)


def _create_connection(conn_settings):
def _create_connection(conn_settings, testing=False):

# Handle multiple connections recursively
if isinstance(conn_settings, list):
connections = {}
for conn in conn_settings:
connections[conn.get('alias')] = _create_connection(conn)
connections[conn.get('alias')] = _create_connection(conn, testing)
return connections

# Ugly dict comprehention in order to support python 2.6
Expand All @@ -85,8 +85,7 @@ def _create_connection(conn_settings):
conn['replicaSet'] = conn.pop('replicaset')

if (StrictVersion(mongoengine.__version__) >= StrictVersion('0.10.6') and
current_app.config['TESTING'] == True and
conn.get('host', '').startswith('mongomock://')):
testing and conn.get('host', '').startswith('mongomock://')):
pass
# Handle uri style connections
elif "://" in conn.get('host', ''):
Expand Down Expand Up @@ -132,7 +131,7 @@ def init_app(self, app, config=None):

if 'MONGODB_SETTINGS' in config:
# Connection settings provided as a dictionary.
connection = _create_connection(config['MONGODB_SETTINGS'])
connection = _create_connection(config['MONGODB_SETTINGS'], testing=app.config['TESTING'])
else:
# Connection settings provided in standard format.
settings = {'alias': config.get('MONGODB_ALIAS', None),
Expand All @@ -141,7 +140,7 @@ def init_app(self, app, config=None):
'password': config.get('MONGODB_PASSWORD', None),
'port': config.get('MONGODB_PORT', None),
'username': config.get('MONGODB_USERNAME', None)}
connection = _create_connection(settings)
connection = _create_connection(settings, testing=app.config['TESTING'])

# Store objects in application instance so that multiple apps do
# not end up accessing the same objects.
Expand Down