Skip to content

Commit b488d98

Browse files
aclark4lifetimgraham
authored andcommitted
INTPYTHON-654 Add options parameter to parse_uri()
1 parent fe862bf commit b488d98

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

django_mongodb_backend/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def check_django_compatability():
2828
)
2929

3030

31-
def parse_uri(uri, *, db_name=None, test=None):
31+
def parse_uri(uri, *, db_name=None, options=None, test=None):
3232
"""
3333
Convert the given uri into a dictionary suitable for Django's DATABASES
3434
setting.
@@ -48,14 +48,17 @@ def parse_uri(uri, *, db_name=None, test=None):
4848
db_name = db_name or uri["database"]
4949
if not db_name:
5050
raise ImproperlyConfigured("You must provide the db_name parameter.")
51+
opts = uri.get("options")
52+
if options:
53+
opts.update(options)
5154
settings_dict = {
5255
"ENGINE": "django_mongodb_backend",
5356
"NAME": db_name,
5457
"HOST": host,
5558
"PORT": port,
5659
"USER": uri.get("username"),
5760
"PASSWORD": uri.get("password"),
58-
"OPTIONS": uri.get("options"),
61+
"OPTIONS": opts,
5962
}
6063
if "authSource" not in settings_dict["OPTIONS"] and uri["database"]:
6164
settings_dict["OPTIONS"]["authSource"] = uri["database"]

docs/source/ref/utils.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ following parts can be considered stable.
1212
``parse_uri()``
1313
===============
1414

15-
.. function:: parse_uri(uri, db_name=None, test=None)
15+
.. function:: parse_uri(uri, db_name=None, options=None, test=None)
1616

1717
Parses a MongoDB `connection string`_ into a dictionary suitable for
1818
Django's :setting:`DATABASES` setting.
@@ -32,8 +32,17 @@ following parts can be considered stable.
3232
You can use the parameters to customize the resulting :setting:`DATABASES`
3333
setting:
3434

35+
- Use ``options`` to provide a dictionary of parameters to
36+
:class:`~pymongo.mongo_client.MongoClient`. These will be merged with
37+
(and, in the case of duplicates, take precedence over) any options
38+
specified in the URI.
39+
3540
- Use ``test`` to provide a dictionary of settings for test databases in
3641
the format of :setting:`TEST <DATABASE-TEST>`.
3742

3843
But for maximum flexibility, construct :setting:`DATABASES` manually as
3944
described in :ref:`configuring-databases-setting`.
45+
46+
.. versionchanged:: 5.2b2
47+
48+
The ``options`` parameter was added.

docs/source/releases/5.2.x.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ New features
1111
------------
1212

1313
- Added subquery support for :class:`~.fields.EmbeddedModelArrayField`.
14+
- Added the ``options`` parameter to
15+
:func:`~django_mongodb_backend.utils.parse_uri`.
1416

1517
5.2.0 beta 1
1618
============

tests/backend_/utils/test_parse_uri.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ def test_auth_source_in_query_string_overrides_defaultauthdb(self):
8282
self.assertEqual(settings_dict["NAME"], "db")
8383
self.assertEqual(settings_dict["OPTIONS"], {"authSource": "auth"})
8484

85+
def test_options_kwarg(self):
86+
options = {"authSource": "auth", "retryWrites": True}
87+
settings_dict = parse_uri(
88+
"mongodb://cluster0.example.mongodb.net/myDatabase?retryWrites=false&retryReads=true",
89+
options=options,
90+
)
91+
self.assertEqual(
92+
settings_dict["OPTIONS"],
93+
{"authSource": "auth", "retryWrites": True, "retryReads": True},
94+
)
95+
8596
def test_test_kwarg(self):
8697
settings_dict = parse_uri("mongodb://localhost/db", test={"NAME": "test_db"})
8798
self.assertEqual(settings_dict["TEST"], {"NAME": "test_db"})

0 commit comments

Comments
 (0)