16
16
from .wtf import WtfBaseField
17
17
18
18
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
-
46
19
def _patch_base_field (obj , name ):
47
20
"""
48
21
If the object submitted has a class whose base class is
@@ -59,6 +32,8 @@ def _patch_base_field(obj, name):
59
32
@param obj: MongoEngine instance in which we should locate the class.
60
33
@param name: Name of an attribute which may or may not be a BaseField.
61
34
"""
35
+ # TODO is there a less hacky way to accomplish the same level of
36
+ # extensibility/control?
62
37
63
38
# get an attribute of the MongoEngine class and return if it's not
64
39
# a class
@@ -79,7 +54,6 @@ def _patch_base_field(obj, name):
79
54
# re-assign the class back to the MongoEngine instance
80
55
delattr (obj , name )
81
56
setattr (obj , name , cls )
82
- redirect_connection_calls (cls )
83
57
84
58
85
59
def _include_mongoengine (obj ):
@@ -99,10 +73,7 @@ def _include_mongoengine(obj):
99
73
100
74
101
75
def 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."""
106
77
me = current_app .extensions .get ('mongoengine' , {})
107
78
for k , v in me .items ():
108
79
if isinstance (k , MongoEngine ):
@@ -139,36 +110,22 @@ def init_app(self, app, config=None):
139
110
raise Exception ('Extension already initialized' )
140
111
141
112
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.
145
115
config = app .config
146
116
147
- # Obtain db connection
148
- connection = create_connection (config , app )
117
+ # Obtain db connection(s)
118
+ connections = create_connections (config )
149
119
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 }
154
123
app .extensions ['mongoengine' ][self ] = s
155
124
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
-
168
125
@property
169
126
def connection (self ):
170
127
"""
171
- Return MongoDB connection associated with this MongoEngine
128
+ Return MongoDB connection(s) associated with this MongoEngine
172
129
instance.
173
130
"""
174
131
return current_app .extensions ['mongoengine' ][self ]['conn' ]
0 commit comments