11# -*- coding: utf-8 -*-
22from __future__ import absolute_import
33
4- from flask import abort
4+ from flask import abort , current_app
55
66import mongoengine
77
@@ -23,6 +23,14 @@ def _include_mongoengine(obj):
2323
2424
2525def _create_connection (conn_settings ):
26+
27+ # Handle multiple connections recursively
28+ if isinstance (conn_settings , list ):
29+ connections = {}
30+ for conn in conn_settings :
31+ connections [conn .get ('alias' )] = _create_connection (conn )
32+ return connections
33+
2634 conn = dict ([(k .lower (), v ) for k , v in conn_settings .items () if v ])
2735
2836 if 'replicaset' in conn :
@@ -38,40 +46,60 @@ def _create_connection(conn_settings):
3846
3947class MongoEngine (object ):
4048
41- def __init__ (self , app = None ):
49+ def __init__ (self , app = None , config = None ):
4250
4351 _include_mongoengine (self )
4452
4553 self .Document = Document
4654 self .DynamicDocument = DynamicDocument
4755
4856 if app is not None :
49- self .init_app (app )
57+ self .init_app (app , config )
5058
51- def init_app (self , app ):
59+ def init_app (self , app , config = None ):
5260
53- conn_settings = app . config . get ( 'MONGODB_SETTINGS ' , None )
61+ app . extensions = getattr ( app , 'extensions ' , {} )
5462
55- if not conn_settings :
56- conn_settings = {
57- 'db' : app .config .get ('MONGODB_DB' , None ),
58- 'username' : app .config .get ('MONGODB_USERNAME' , None ),
59- 'password' : app .config .get ('MONGODB_PASSWORD' , None ),
60- 'host' : app .config .get ('MONGODB_HOST' , None ),
61- 'port' : int (app .config .get ('MONGODB_PORT' , 0 )) or None
62- }
63+ # Make documents JSON serializable
64+ overide_json_encoder (app )
6365
64- if isinstance (conn_settings , list ):
65- self .connection = {}
66- for conn in conn_settings :
67- self .connection [conn .get ('alias' )] = _create_connection (conn )
68- else :
69- self .connection = _create_connection (conn_settings )
66+ if not 'mongoengine' in app .extensions :
67+ app .extensions ['mongoengine' ] = {}
7068
71- app .extensions = getattr (app , 'extensions' , {})
72- app .extensions ['mongoengine' ] = self
73- self .app = app
74- overide_json_encoder (app )
69+ if self in app .extensions ['mongoengine' ]:
70+ # Raise an exception if extension already initialized as
71+ # potentially new configuration would not be loaded.
72+ raise Exception ('Extension already initialized' )
73+
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 )
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 )
94+
95+ # Store objects in application instance so that multiple apps do
96+ # not end up accessing the same objects.
97+ app .extensions ['mongoengine' ] = {self : {'app' : app ,
98+ 'conn' : connection }}
99+
100+ @property
101+ def connection (self ):
102+ return current_app .extensions ['mongoengine' ][self ]['conn' ]
75103
76104
77105class BaseQuerySet (QuerySet ):
0 commit comments