Skip to content

Commit 568292d

Browse files
committed
Merge pull request #118 from mtsgrd/master
Make the config easier to override.
2 parents 45beb56 + 6bf1733 commit 568292d

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

flask_mongoengine/__init__.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,23 @@ def init_app(self, app, config=None):
7171
# potentially new configuration would not be loaded.
7272
raise Exception('Extension already initialized')
7373

74-
if config:
75-
# If passed an explicit config then we must make sure to ignore
76-
# anything set in the application config.
77-
connection = _create_connection(config)
74+
if not config:
75+
# If not passed a config then we read the connection settings
76+
# from the app config.
77+
config = app.config
78+
79+
if 'MONGODB_SETTINGS' in config:
80+
# Connection settings provided as a dictionary.
81+
connection = _create_connection(config['MONGODB_SETTINGS'])
7882
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)
83+
# Connection settings provided in standard format.
84+
settings = {'alias': config.get('MONGODB_ALIAS', None),
85+
'db': config.get('MONGODB_DB', None),
86+
'host': config.get('MONGODB_HOST', None),
87+
'password': config.get('MONGODB_PASSWORD', None),
88+
'port': config.get('MONGODB_PORT', None),
89+
'username': config.get('MONGODB_USERNAME', None)}
90+
connection = _create_connection(settings)
9491

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

0 commit comments

Comments
 (0)