Skip to content

Commit 2bb06df

Browse files
committed
remove broken mongomock support and fix the connection code
1 parent 9465a64 commit 2bb06df

File tree

5 files changed

+107
-477
lines changed

5 files changed

+107
-477
lines changed

flask_mongoengine/__init__.py

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,6 @@
1717
from .wtf import WtfBaseField
1818

1919

20-
def redirect_connection_calls(cls):
21-
"""
22-
Monkey-patch mongoengine's connection methods so that they use
23-
Flask-MongoEngine's equivalents.
24-
25-
Given a random mongoengine class (`cls`), get the module it's in,
26-
and iterate through all of that module's members to find the
27-
particular methods we want to monkey-patch.
28-
"""
29-
# TODO this is so whack... Why don't we pass particular connection
30-
# settings down to mongoengine and just use their original implementation?
31-
32-
# Map of mongoengine method/variable names and flask-mongoengine
33-
# methods they should point to
34-
connection_methods = {
35-
'get_db': get_db,
36-
'DEFAULT_CONNECTION_NAME': DEFAULT_CONNECTION_NAME,
37-
'get_connection': get_connection
38-
}
39-
cls_module = inspect.getmodule(cls)
40-
if cls_module != mongoengine.connection:
41-
for attr in inspect.getmembers(cls_module):
42-
n = attr[0]
43-
if n in connection_methods:
44-
setattr(cls_module, n, connection_methods[n])
45-
46-
4720
def _patch_base_field(obj, name):
4821
"""
4922
If the object submitted has a class whose base class is
@@ -60,6 +33,8 @@ def _patch_base_field(obj, name):
6033
@param obj: MongoEngine instance in which we should locate the class.
6134
@param name: Name of an attribute which may or may not be a BaseField.
6235
"""
36+
# TODO is there a less hacky way to accomplish the same level of
37+
# extensibility/control?
6338

6439
# get an attribute of the MongoEngine class and return if it's not
6540
# a class
@@ -80,7 +55,6 @@ def _patch_base_field(obj, name):
8055
# re-assign the class back to the MongoEngine instance
8156
delattr(obj, name)
8257
setattr(obj, name, cls)
83-
redirect_connection_calls(cls)
8458

8559

8660
def _include_mongoengine(obj):
@@ -100,10 +74,7 @@ def _include_mongoengine(obj):
10074

10175

10276
def current_mongoengine_instance():
103-
"""
104-
Obtain instance of MongoEngine in the
105-
current working app instance.
106-
"""
77+
"""Return a MongoEngine instance associated with current Flask app."""
10778
me = current_app.extensions.get('mongoengine', {})
10879
for k, v in me.items():
10980
if isinstance(k, MongoEngine):
@@ -140,36 +111,22 @@ def init_app(self, app, config=None):
140111
raise Exception('Extension already initialized')
141112

142113
if not config:
143-
# If not passed a config then we
144-
# read the connection settings from
145-
# the app config.
114+
# If not passed a config then we read the connection settings
115+
# from the app config.
146116
config = app.config
147117

148-
# Obtain db connection
149-
connection = create_connection(config, app)
118+
# Obtain db connection(s)
119+
connections = create_connections(config)
150120

151-
# Store objects in application instance
152-
# so that multiple apps do not end up
153-
# accessing the same objects.
154-
s = {'app': app, 'conn': connection}
121+
# Store objects in application instance so that multiple apps do not
122+
# end up accessing the same objects.
123+
s = {'app': app, 'conn': connections}
155124
app.extensions['mongoengine'][self] = s
156125

157-
def disconnect(self):
158-
"""Close all connections to MongoDB."""
159-
conn_settings = fetch_connection_settings(current_app.config)
160-
if isinstance(conn_settings, list):
161-
for setting in conn_settings:
162-
alias = setting.get('alias', DEFAULT_CONNECTION_NAME)
163-
disconnect(alias, setting.get('preserve_temp_db', False))
164-
else:
165-
alias = conn_settings.get('alias', DEFAULT_CONNECTION_NAME)
166-
disconnect(alias, conn_settings.get('preserve_temp_db', False))
167-
return True
168-
169126
@property
170127
def connection(self):
171128
"""
172-
Return MongoDB connection associated with this MongoEngine
129+
Return MongoDB connection(s) associated with this MongoEngine
173130
instance.
174131
"""
175132
return current_app.extensions['mongoengine'][self]['conn']

0 commit comments

Comments
 (0)