Skip to content

Commit 352670e

Browse files
committed
Adding a new scheme for settings, MONGODB_SETTINGS, which is a dict. Allowing fallback to existing config scheme.
1 parent 0dcb1e9 commit 352670e

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

example/simpleapp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
app = flask.Flask(__name__)
1717
app.config.from_object(__name__)
18-
app.config['MONGODB_DB'] = 'testing'
18+
app.config['MONGODB_SETTINGS'] = {'DB': 'testing'}
1919
app.config['TESTING'] = True
2020
app.config['SECRET_KEY'] = 'flask+mongoengine=<3'
2121
app.debug = True
@@ -55,4 +55,4 @@ def index():
5555
return flask.render_template('index.html', todos=todos)
5656

5757
if __name__ == "__main__":
58-
app.run(host="0.0.0.0", port=4000)
58+
app.run(host="0.0.0.0", port=4000)

flask_mongoengine/__init__.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ def __init__(self, app=None):
3131

3232
def init_app(self, app):
3333

34-
conn_settings = {
35-
'db': app.config.get('MONGODB_DB', None),
36-
'username': app.config.get('MONGODB_USERNAME', None),
37-
'password': app.config.get('MONGODB_PASSWORD', None),
38-
'host': app.config.get('MONGODB_HOST', None),
39-
'port': int(app.config.get('MONGODB_PORT', 0)) or None
40-
}
41-
42-
conn_settings = dict([(k, v) for k, v in conn_settings.items() if v])
34+
conn_settings = app.config.get('MONGODB_SETTINGS', None)
35+
36+
if not conn_settings:
37+
conn_settings = {
38+
'db': app.config.get('MONGODB_DB', None),
39+
'username': app.config.get('MONGODB_USERNAME', None),
40+
'password': app.config.get('MONGODB_PASSWORD', None),
41+
'host': app.config.get('MONGODB_HOST', None),
42+
'port': int(app.config.get('MONGODB_PORT', 0)) or None
43+
}
44+
45+
conn_settings = dict([(k.lower(), v) for k, v in conn_settings.items() if v])
4346

4447
self.connection = mongoengine.connect(**conn_settings)
4548

tests.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ def show(id):
5353
self.app = app
5454
self.db = db
5555

56+
def test_connection_kwargs(self):
57+
app = flask.Flask(__name__)
58+
app.config['MONGODB_SETTINGS'] = {
59+
'DB': 'testing_tz_aware',
60+
'alias': 'tz_aware_true',
61+
'TZ_AWARE': True,
62+
}
63+
app.config['TESTING'] = True
64+
db = MongoEngine()
65+
db.init_app(app)
66+
self.assertTrue(db.connection.tz_aware)
67+
68+
app.config['MONGODB_SETTINGS'] = {
69+
'DB': 'testing',
70+
'alias': 'tz_aware_false',
71+
}
72+
db.init_app(app)
73+
self.assertFalse(db.connection.tz_aware)
74+
5675
def test_with_id(self):
5776
c = self.app.test_client()
5877
resp = c.get('/show/38783728378090/')
@@ -77,7 +96,6 @@ def test_request_context(self):
7796
todo.save()
7897
self.assertEqual(self.Todo.objects.count(), 1)
7998

80-
8199
class WTFormsAppTestCase(unittest.TestCase):
82100

83101
def setUp(self):

0 commit comments

Comments
 (0)