Skip to content

Commit 088ffb2

Browse files
authored
Merge branch 'main' into expr-support-getfield
2 parents edc0746 + 94eefad commit 088ffb2

File tree

20 files changed

+422
-407
lines changed

20 files changed

+422
-407
lines changed

.github/workflows/codeql.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ jobs:
5050
ref: ${{ inputs.ref }}
5151
persist-credentials: false
5252
- name: Set up Python
53-
uses: actions/setup-python@v5
53+
uses: actions/setup-python@v6
5454
with:
5555
python-version: 3.x
5656

5757
# Initializes the CodeQL tools for scanning.
5858
- name: Initialize CodeQL
59-
uses: github/codeql-action/init@3c3833e0f8c1c83d449a7478aa59c036a9165498 # v3.29.11
59+
uses: github/codeql-action/init@f1f6e5f6af878fb37288ce1c627459e94dbf7d01 # v3.30.1
6060
with:
6161
languages: ${{ matrix.language }}
6262
build-mode: none
@@ -72,6 +72,6 @@ jobs:
7272
pip install -e .
7373
7474
- name: Perform CodeQL Analysis
75-
uses: github/codeql-action/analyze@3c3833e0f8c1c83d449a7478aa59c036a9165498 # v3.29.11
75+
uses: github/codeql-action/analyze@f1f6e5f6af878fb37288ce1c627459e94dbf7d01 # v3.30.1
7676
with:
7777
category: "/language:${{ matrix.language }}"

.github/workflows/dist.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
ref: ${{ inputs.ref }}
2525
persist-credentials: false
2626
- name: Set up Python
27-
uses: actions/setup-python@v5
27+
uses: actions/setup-python@v6
2828
with:
2929
python-version: 3.x
3030
- name: Install dependencies

.github/workflows/linters.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v5
1616
with:
1717
persist-credentials: false
18-
- uses: actions/setup-python@v5
18+
- uses: actions/setup-python@v6
1919
with:
2020
python-version: '3.10'
2121
cache: 'pip'
@@ -35,7 +35,7 @@ jobs:
3535
- uses: actions/checkout@v5
3636
with:
3737
persist-credentials: false
38-
- uses: actions/setup-python@v5
38+
- uses: actions/setup-python@v6
3939
with:
4040
cache: 'pip'
4141
cache-dependency-path: 'pyproject.toml'

.github/workflows/mongodb_settings.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import os
22

3-
from django_mongodb_backend import parse_uri
3+
from pymongo.uri_parser import parse_uri
44

55
if mongodb_uri := os.getenv("MONGODB_URI"):
6-
db_settings = parse_uri(mongodb_uri, db_name="dummy")
7-
6+
db_settings = {
7+
"ENGINE": "django_mongodb_backend",
8+
"HOST": mongodb_uri,
9+
}
810
# Workaround for https://github.com/mongodb-labs/mongo-orchestration/issues/268
9-
if db_settings["USER"] and db_settings["PASSWORD"]:
10-
db_settings["OPTIONS"].update({"tls": True, "tlsAllowInvalidCertificates": True})
11+
uri = parse_uri(mongodb_uri)
12+
if uri.get("username") and uri.get("password"):
13+
db_settings["OPTIONS"] = {"tls": True, "tlsAllowInvalidCertificates": True}
1114
DATABASES = {
1215
"default": {**db_settings, "NAME": "djangotests"},
1316
"other": {**db_settings, "NAME": "djangotests-other"},

.github/workflows/release-python.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ jobs:
8080
name: all-dist-${{ github.run_id }}
8181
path: dist/
8282
- name: Publish package distributions to TestPyPI
83-
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # release/v1
83+
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # 1.13.0
8484
with:
8585
repository-url: https://test.pypi.org/legacy/
8686
skip-existing: true
8787
attestations: ${{ env.DRY_RUN }}
8888
- name: Publish package distributions to PyPI
8989
if: startsWith(env.DRY_RUN, 'false')
90-
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # release/v1
90+
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # 1.13.0
9191

9292
post-publish:
9393
needs: [publish]

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ setting like so:
4545

4646
```python
4747
DATABASES = {
48-
"default": django_mongodb_backend.parse_uri(
49-
"<CONNECTION_STRING_URI>", db_name="example"
50-
),
48+
"default": {
49+
"ENGINE": "django_mongodb_backend",
50+
"HOST": "<CONNECTION_STRING_URI>",
51+
"NAME": "db_name",
52+
},
5153
}
5254
```
5355

django_mongodb_backend/base.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pymongo.collection import Collection
1212
from pymongo.driver_info import DriverInfo
1313
from pymongo.mongo_client import MongoClient
14+
from pymongo.uri_parser import parse_uri
1415

1516
from . import __version__ as django_mongodb_backend_version
1617
from . import dbapi as Database
@@ -157,6 +158,18 @@ def __init__(self, settings_dict, alias=DEFAULT_DB_ALIAS):
157158
self.in_atomic_block_mongo = False
158159
# Current number of nested 'atomic' calls.
159160
self.nested_atomics = 0
161+
# If database "NAME" isn't specified, try to get it from HOST, if it's
162+
# a connection string.
163+
if self.settings_dict["NAME"] == "": # Empty string = unspecified; None = _nodb_cursor()
164+
name_is_missing = True
165+
host = self.settings_dict["HOST"]
166+
if host.startswith(("mongodb://", "mongodb+srv://")):
167+
uri = parse_uri(host)
168+
if database := uri.get("database"):
169+
self.settings_dict["NAME"] = database
170+
name_is_missing = False
171+
if name_is_missing:
172+
raise ImproperlyConfigured('settings.DATABASES is missing the "NAME" value.')
160173

161174
def get_collection(self, name, **kwargs):
162175
collection = Collection(self.database, name, **kwargs)
@@ -183,15 +196,19 @@ def init_connection_state(self):
183196

184197
def get_connection_params(self):
185198
settings_dict = self.settings_dict
186-
if not settings_dict["NAME"]:
187-
raise ImproperlyConfigured('settings.DATABASES is missing the "NAME" value.')
188-
return {
199+
params = {
189200
"host": settings_dict["HOST"] or None,
190-
"port": int(settings_dict["PORT"] or 27017),
191-
"username": settings_dict.get("USER"),
192-
"password": settings_dict.get("PASSWORD"),
193201
**settings_dict["OPTIONS"],
194202
}
203+
# MongoClient uses any of these parameters (including "OPTIONS" above)
204+
# to override any corresponding values in a connection string "HOST".
205+
if user := settings_dict.get("USER"):
206+
params["username"] = user
207+
if password := settings_dict.get("PASSWORD"):
208+
params["password"] = password
209+
if port := settings_dict.get("PORT"):
210+
params["port"] = int(port)
211+
return params
195212

196213
@async_unsafe
197214
def get_new_connection(self, conn_params):

django_mongodb_backend/query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.db.models.sql.where import AND, OR, XOR, ExtraWhere, NothingNode, WhereNode
1212
from pymongo.errors import BulkWriteError, DuplicateKeyError, PyMongoError
1313

14-
from django_mongodb_backend.query_conversion.query_optimizer import QueryOptimizer
14+
from .query_conversion.query_optimizer import convert_expr_to_match
1515

1616

1717
def wrap_database_errors(func):
@@ -90,7 +90,7 @@ def get_pipeline(self):
9090
for query in self.subqueries or ():
9191
pipeline.extend(query.get_pipeline())
9292
if self.match_mql:
93-
pipeline.extend(self.query_optimizer.convert_expr_to_match(self.match_mql))
93+
pipeline.extend(convert_expr_to_match(self.match_mql))
9494
if self.aggregation_pipeline:
9595
pipeline.extend(self.aggregation_pipeline)
9696
if self.project_fields:

0 commit comments

Comments
 (0)