Skip to content

Commit a7dc87a

Browse files
committed
Ignore app config if passed explicit config.
1 parent 3a93887 commit a7dc87a

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

flask_mongoengine/__init__.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,6 @@ def init_app(self, app, config=None):
6060

6161
app.extensions = getattr(app, 'extensions', {})
6262

63-
if not config:
64-
config = {}
65-
66-
# Set default config
67-
config.setdefault('db', app.config.get('MONGODB_DB', None))
68-
config.setdefault('host', app.config.get('MONGODB_HOST', None))
69-
config.setdefault('port', app.config.get('MONGODB_PORT', None))
70-
config.setdefault('username',
71-
app.config.get('MONGODB_USERNAME', None))
72-
config.setdefault('password',
73-
app.config.get('MONGODB_PASSWORD', None))
74-
7563
# Make documents JSON serializable
7664
overide_json_encoder(app)
7765

@@ -83,11 +71,26 @@ def init_app(self, app, config=None):
8371
# potentially new configuration would not be loaded.
8472
raise Exception('Extension already initialized')
8573

86-
# Before using default config we check for MONGODB_SETTINGS
87-
if 'MONGODB_SETTINGS' in app.config:
88-
connection = _create_connection(app.config['MONGODB_SETTINGS'])
89-
else:
74+
if config:
75+
# If passed an explicit config then we must make sure to ignore
76+
# anything set in the application config.
9077
connection = _create_connection(config)
78+
else:
79+
# Set default config
80+
config = {}
81+
config.setdefault('db', app.config.get('MONGODB_DB', None))
82+
config.setdefault('host', app.config.get('MONGODB_HOST', None))
83+
config.setdefault('port', app.config.get('MONGODB_PORT', None))
84+
config.setdefault('username',
85+
app.config.get('MONGODB_USERNAME', None))
86+
config.setdefault('password',
87+
app.config.get('MONGODB_PASSWORD', None))
88+
89+
# Before using default config we check for MONGODB_SETTINGS
90+
if 'MONGODB_SETTINGS' in app.config:
91+
connection = _create_connection(app.config['MONGODB_SETTINGS'])
92+
else:
93+
connection = _create_connection(config)
9194

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

0 commit comments

Comments
 (0)