Skip to content

Commit 2070d26

Browse files
committed
Merge branch 'allow-no-db-name'
2 parents 4353c58 + 0e1fe4f commit 2070d26

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

docs/index.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ You can use :attr:`~flask_pymongo.PyMongo.db` directly in views:
4646
return render_template("index.html",
4747
online_users=online_users)
4848
49+
.. note::
50+
51+
Previous versions of Flask-PyMongo required that the MongoDB URI
52+
contained a database name; as of 2.2, this requirement is lifted. If
53+
there is no database name, the :attr:`~flask_pymongo.PyMongo.db`
54+
attribute will be ``None``.
55+
4956

5057
Compatibility
5158
-------------
@@ -129,13 +136,12 @@ Classes
129136

130137
.. attribute:: cx
131138

132-
The :class:`~flask_pymongo.wrappers.MongoClient` or
133-
:class:`~flask_pymongo.wrappers.MongoReplicaSetClient`, depending on
134-
the type of connection described by the URI.
139+
The :class:`~flask_pymongo.wrappers.MongoClient` connected to the
140+
MongoDB server.
135141

136142
.. attribute:: db
137143

138-
The :class:`~flask_pymongo.wrappers.Database` if a URI was used and it
144+
The :class:`~flask_pymongo.wrappers.Database` if the URI used
139145
named a database, and ``None`` otherwise.
140146

141147

@@ -167,6 +173,12 @@ History and Contributors
167173

168174
Changes:
169175

176+
- 2.2.0: November 1, 2018
177+
178+
- `#114 <https://github.com/dcrosta/flask-pymongo/pull/114>`_ Accept
179+
keyword arguments to :meth:`~flask_pymongo.PyMongo.save_file` (Andrew C.
180+
Hawkins).
181+
170182
- 2.1.0: August 6, 2018
171183

172184
- `#114 <https://github.com/dcrosta/flask-pymongo/pull/114>`_ Accept

flask_pymongo/__init__.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@ class PyMongo(object):
100100
variable, or as an argument to the constructor or ``init_app``. See
101101
:meth:`init_app` for more detail.
102102
103-
.. versionchanged:: 2.0
104-
105-
Flask-PyMongo no longer accepts many of the configuration variables
106-
it did in previous versions. You must now use a MongoDB URI to
107-
configure Flask-PyMongo.
108-
109103
"""
110104

111105
def __init__(self, app=None, uri=None, *args, **kwargs):
@@ -118,18 +112,22 @@ def __init__(self, app=None, uri=None, *args, **kwargs):
118112
def init_app(self, app, uri=None, *args, **kwargs):
119113
"""Initialize this :class:`PyMongo` for use.
120114
121-
Configure a :class:`~pymongo.mongo_client.MongoClient` or
122-
:class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient`
115+
Configure a :class:`~pymongo.mongo_client.MongoClient`
123116
in the following scenarios:
124117
125118
1. If ``uri`` is not ``None``, pass the ``uri`` and any positional
126-
or keyword arguments to MongoClient
119+
or keyword arguments to :class:`~pymongo.mongo_client.MongoClient`
127120
2. If ``uri`` is ``None``, and a Flask config variable named
128121
``MONGO_URI`` exists, use that as the ``uri`` as above.
129122
130123
The caller is responsible for ensuring that additional positional
131124
and keyword arguments result in a valid call.
132125
126+
.. versionchanged:: 2.2
127+
128+
The ``uri`` is no longer required to contain a database name. If it
129+
does not, then the :attr:`db` attribute will be ``None``.
130+
133131
.. versionchanged:: 2.0
134132
135133
Flask-PyMongo no longer accepts many of the configuration variables
@@ -149,16 +147,13 @@ def init_app(self, app, uri=None, *args, **kwargs):
149147
parsed_uri = uri_parser.parse_uri(uri)
150148
database_name = parsed_uri["database"]
151149

152-
# Avoid a more confusing error later when we try to get the DB
153-
if not database_name:
154-
raise ValueError("Your URI must specify a database name")
155-
156150
# Try to delay connecting, in case the app is loaded before forking, per
157151
# http://api.mongodb.com/python/current/faq.html#is-pymongo-fork-safe
158152
kwargs.setdefault("connect", False)
159153

160154
self.cx = MongoClient(*args, **kwargs)
161-
self.db = self.cx[database_name]
155+
if database_name:
156+
self.db = self.cx[database_name]
162157

163158
app.url_map.converters["ObjectId"] = BSONObjectIdConverter
164159

flask_pymongo/tests/test_config.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import contextmanager
12
import time
23

34
import pymongo
@@ -11,6 +12,14 @@ class CouldNotConnect(Exception):
1112
pass
1213

1314

15+
@contextmanager
16+
def doesnt_raise(exc=BaseException):
17+
try:
18+
yield
19+
except exc:
20+
pytest.fail("{} was raised but should not have been".format(exc))
21+
22+
1423
class FlaskPyMongoConfigTest(FlaskRequestTest):
1524

1625
def setUp(self):
@@ -81,11 +90,13 @@ def test_it_doesnt_connect_by_default(self):
8190
with pytest.raises(CouldNotConnect):
8291
_wait_until_connected(mongo, timeout=0.2)
8392

84-
def test_it_requires_db_name_in_uri(self):
93+
def test_it_doesnt_require_db_name_in_uri(self):
8594
uri = "mongodb://localhost:{}".format(self.port)
8695

87-
with pytest.raises(ValueError):
88-
flask_pymongo.PyMongo(self.app, uri)
96+
with doesnt_raise(Exception):
97+
mongo = flask_pymongo.PyMongo(self.app, uri)
98+
99+
assert mongo.db is None
89100

90101

91102
def _wait_until_connected(mongo, timeout=1.0):

0 commit comments

Comments
 (0)