Skip to content

Commit 0fea5a6

Browse files
authored
Merge pull request #44 from aclark4life/main
Misc updates
2 parents 453f9eb + 5d33260 commit 0fea5a6

File tree

5 files changed

+51
-29
lines changed

5 files changed

+51
-29
lines changed

django_mongodb_cli/project.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ def migrate_project(
228228
None,
229229
help="Optional MongoDB connection URI. Falls back to $MONGODB_URI if not provided.",
230230
),
231+
database: str = typer.Option(
232+
None,
233+
help="Specify the database to migrate.",
234+
),
231235
):
232236
"""
233237
Run Django migrations using django-admin instead of manage.py.
@@ -238,7 +242,8 @@ def migrate_project(
238242
cmd.append(app_label)
239243
if migration_name:
240244
cmd.append(migration_name)
241-
245+
if database:
246+
cmd.append(f"--database={database}")
242247
typer.echo(f"📦 Applying migrations for project '{name}'")
243248
_django_manage_command(
244249
name, directory, *cmd, extra_env=_build_mongodb_env(mongodb_uri)
@@ -277,6 +282,10 @@ def manage_command(
277282
mongodb_uri: str = typer.Option(
278283
None, "--mongodb-uri", help="MongoDB connection URI"
279284
),
285+
database: str = typer.Option(
286+
None,
287+
help="Specify the database to use.",
288+
),
280289
):
281290
"""
282291
Run any django-admin command for a project.
@@ -297,6 +306,9 @@ def manage_command(
297306

298307
os.environ["MONGODB_URI"] = mongodb_uri
299308

309+
if database:
310+
args.append(f"--database={database}")
311+
300312
if command:
301313
typer.echo(f"⚙️ Running django-admin {command} {' '.join(args)} for '{name}'")
302314
_django_manage_command(name, directory, command, *args)
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
from django_mongodb_backend.utils import model_has_encrypted_fields
2+
3+
14
class EncryptedRouter:
2-
def db_for_read(self, model, **hints):
3-
if model._meta.app_label == "django_mongodb_demo":
4-
return "encrypted"
5+
def allow_migrate(self, db, app_label, model_name=None, **hints):
6+
if hints.get("model"):
7+
if model_has_encrypted_fields(hints["model"]):
8+
return db == "encrypted"
9+
else:
10+
return db == "default"
511
return None
612

7-
db_for_write = db_for_read
13+
def db_for_read(self, model, **hints):
14+
if model_has_encrypted_fields(model):
15+
return "encrypted"
16+
return "default"
817

9-
def allow_migrate(self, db, app_label, model_name=None, **hints):
10-
if app_label == "django_mongodb_demo":
11-
print("allow_migrate for django_mongodb_demo:", db)
12-
return db == "encrypted"
13-
# Don't create other app's models in the encrypted database.
14-
if db == "encrypted":
15-
return False
18+
def kms_provider(self, model):
19+
if model_has_encrypted_fields(model):
20+
return "local"
1621
return None
1722

18-
def kms_provider(self, model, **hints):
19-
return "local"
23+
db_for_write = db_for_read

django_mongodb_cli/templates/project_template/project_name/settings/project_name.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
"NAME": "{{ project_name }}_encrypted",
1717
"OPTIONS": {
1818
"auto_encryption_opts": AutoEncryptionOpts(
19-
kms_providers={"local": {"key": os.urandom(96)}}, # noqa
20-
key_vault_namespace="keyvault.__keyVault",
19+
kms_providers={
20+
"local": {
21+
"key": b"tO\x7f\xf2L0\x9e\xab\xcd'\xd3\xd4'P\xf5;3\x94\xde8\xd7\xa4\xc5J\xe9\xb7\xc6\t\xbe\xa3<\xb3\xbe\xb3\xe5E\xb1\xdf[\xfb\x94\x8c`\x9e\xa20*\x82\x16\x98\xa32\x11\xa6\xeb\xfa\x05e\x08/\xe2\x01\xe8\xf1'#\xf9E\xde\xb0\x07Z\x93V\x84.\xf5\xb9\xdbR\xf6\xf6!\xd7;\xa9c\x087\xa1f\x9c\x1b\x86\xe8D"
22+
}
23+
},
24+
key_vault_namespace="{{ project_name }}_encrypted.__keyVault",
2125
),
2226
},
2327
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ repos = [
8686
"mongo @ git+ssh://[email protected]/mongodb/mongo@master",
8787
"drivers-evergreen-tools @ git+ssh://[email protected]/mongodb-labs/drivers-evergreen-tools@master",
8888
"docs @ git+ssh://[email protected]/mongodb/docs@main",
89+
"docs-sample-apps @ git+ssh://[email protected]/mongodb/docs-sample-apps@main",
8990
"flask-pymongo @ git+ssh://[email protected]/mongodb-labs/flask-pymongo",
9091
"mongo-arrow @ git+ssh://[email protected]/mongodb-labs/mongo-arrow@main",
9192
"mongo-orchestration @ git+ssh://[email protected]/mongodb-labs/mongo-orchestration@master",

test/settings/qe.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77

88
MONGODB_URI = os.environ.get("MONGODB_URI", "mongodb://localhost:27017")
9+
10+
KMS_CREDENTIALS = {
11+
"local": {
12+
"key": os.urandom(96),
13+
},
14+
}
915
DATABASES = {
1016
"default": {
1117
"ENGINE": "django_mongodb_backend",
@@ -24,26 +30,14 @@
2430
"OPTIONS": {
2531
"auto_encryption_opts": AutoEncryptionOpts(
2632
key_vault_namespace="djangotests_encrypted.__keyVault",
27-
kms_providers={
28-
"local": {"key": os.urandom(96)},
29-
},
33+
kms_providers=KMS_CREDENTIALS,
3034
),
3135
},
3236
},
3337
}
3438

3539

3640
class EncryptedRouter:
37-
def db_for_read(self, model, **hints):
38-
if model_has_encrypted_fields(model):
39-
return "encrypted"
40-
return "default"
41-
42-
def db_for_write(self, model, **hints):
43-
if model_has_encrypted_fields(model):
44-
return "encrypted"
45-
return "default"
46-
4741
def allow_migrate(self, db, app_label, model_name=None, **hints):
4842
if hints.get("model"):
4943
if model_has_encrypted_fields(hints["model"]):
@@ -52,11 +46,18 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
5246
return db == "default"
5347
return None
5448

49+
def db_for_read(self, model, **hints):
50+
if model_has_encrypted_fields(model):
51+
return "encrypted"
52+
return "default"
53+
5554
def kms_provider(self, model):
5655
if model_has_encrypted_fields(model):
5756
return "local"
5857
return None
5958

59+
db_for_write = db_for_read
60+
6061

6162
DATABASE_ROUTERS = [EncryptedRouter()]
6263
DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField"

0 commit comments

Comments
 (0)