Skip to content

Commit 3a93887

Browse files
committed
Allow custom config.
1 parent c371615 commit 3a93887

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

flask_mongoengine/__init__.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,30 @@ def _create_connection(conn_settings):
4646

4747
class MongoEngine(object):
4848

49-
def __init__(self, app=None):
49+
def __init__(self, app=None, config=None):
5050

5151
_include_mongoengine(self)
5252

5353
self.Document = Document
5454
self.DynamicDocument = DynamicDocument
5555

5656
if app is not None:
57-
self.init_app(app)
57+
self.init_app(app, config)
5858

59-
def init_app(self, app):
59+
def init_app(self, app, config=None):
6060

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

63-
# Set default settings
64-
settings = {}
65-
settings.setdefault('db', app.config.get('MONGODB_DB', None))
66-
settings.setdefault('host', app.config.get('MONGODB_HOST', None))
67-
settings.setdefault('port', app.config.get('MONGODB_PORT', None))
68-
settings.setdefault('username',
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',
6971
app.config.get('MONGODB_USERNAME', None))
70-
settings.setdefault('password',
72+
config.setdefault('password',
7173
app.config.get('MONGODB_PASSWORD', None))
7274

7375
# Make documents JSON serializable
@@ -81,11 +83,11 @@ def init_app(self, app):
8183
# potentially new configuration would not be loaded.
8284
raise Exception('Extension already initialized')
8385

84-
# Before using default settings we check for MONGODB_SETTINGS
86+
# Before using default config we check for MONGODB_SETTINGS
8587
if 'MONGODB_SETTINGS' in app.config:
8688
connection = _create_connection(app.config['MONGODB_SETTINGS'])
8789
else:
88-
connection = _create_connection(settings)
90+
connection = _create_connection(config)
8991

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

0 commit comments

Comments
 (0)