1616from .wtf import WtfBaseField
1717
1818
19- def redirect_connection_calls (cls ):
20- """
21- Monkey-patch mongoengine's connection methods so that they use
22- Flask-MongoEngine's equivalents.
23-
24- Given a random mongoengine class (`cls`), get the module it's in,
25- and iterate through all of that module's members to find the
26- particular methods we want to monkey-patch.
27- """
28- # TODO this is so whack... Why don't we pass particular connection
29- # settings down to mongoengine and just use their original implementation?
30-
31- # Map of mongoengine method/variable names and flask-mongoengine
32- # methods they should point to
33- connection_methods = {
34- 'get_db' : get_db ,
35- 'DEFAULT_CONNECTION_NAME' : DEFAULT_CONNECTION_NAME ,
36- 'get_connection' : get_connection
37- }
38- cls_module = inspect .getmodule (cls )
39- if cls_module != mongoengine .connection :
40- for attr in inspect .getmembers (cls_module ):
41- n = attr [0 ]
42- if n in connection_methods :
43- setattr (cls_module , n , connection_methods [n ])
44-
45-
4619def _patch_base_field (obj , name ):
4720 """
4821 If the object submitted has a class whose base class is
@@ -59,6 +32,8 @@ def _patch_base_field(obj, name):
5932 @param obj: MongoEngine instance in which we should locate the class.
6033 @param name: Name of an attribute which may or may not be a BaseField.
6134 """
35+ # TODO is there a less hacky way to accomplish the same level of
36+ # extensibility/control?
6237
6338 # get an attribute of the MongoEngine class and return if it's not
6439 # a class
@@ -79,7 +54,6 @@ def _patch_base_field(obj, name):
7954 # re-assign the class back to the MongoEngine instance
8055 delattr (obj , name )
8156 setattr (obj , name , cls )
82- redirect_connection_calls (cls )
8357
8458
8559def _include_mongoengine (obj ):
@@ -99,10 +73,7 @@ def _include_mongoengine(obj):
9973
10074
10175def current_mongoengine_instance ():
102- """
103- Obtain instance of MongoEngine in the
104- current working app instance.
105- """
76+ """Return a MongoEngine instance associated with current Flask app."""
10677 me = current_app .extensions .get ('mongoengine' , {})
10778 for k , v in me .items ():
10879 if isinstance (k , MongoEngine ):
@@ -139,36 +110,22 @@ def init_app(self, app, config=None):
139110 raise Exception ('Extension already initialized' )
140111
141112 if not config :
142- # If not passed a config then we
143- # read the connection settings from
144- # the app config.
113+ # If not passed a config then we read the connection settings
114+ # from the app config.
145115 config = app .config
146116
147- # Obtain db connection
148- connection = create_connection (config , app )
117+ # Obtain db connection(s)
118+ connections = create_connections (config )
149119
150- # Store objects in application instance
151- # so that multiple apps do not end up
152- # accessing the same objects.
153- s = {'app' : app , 'conn' : connection }
120+ # Store objects in application instance so that multiple apps do not
121+ # end up accessing the same objects.
122+ s = {'app' : app , 'conn' : connections }
154123 app .extensions ['mongoengine' ][self ] = s
155124
156- def disconnect (self ):
157- """Close all connections to MongoDB."""
158- conn_settings = fetch_connection_settings (current_app .config )
159- if isinstance (conn_settings , list ):
160- for setting in conn_settings :
161- alias = setting .get ('alias' , DEFAULT_CONNECTION_NAME )
162- disconnect (alias , setting .get ('preserve_temp_db' , False ))
163- else :
164- alias = conn_settings .get ('alias' , DEFAULT_CONNECTION_NAME )
165- disconnect (alias , conn_settings .get ('preserve_temp_db' , False ))
166- return True
167-
168125 @property
169126 def connection (self ):
170127 """
171- Return MongoDB connection associated with this MongoEngine
128+ Return MongoDB connection(s) associated with this MongoEngine
172129 instance.
173130 """
174131 return current_app .extensions ['mongoengine' ][self ]['conn' ]
0 commit comments