Skip to content

Commit 8a7766c

Browse files
committed
Add encryption router
1 parent ed54a9b commit 8a7766c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

django_mongodb_backend/routers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,29 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
1616
except LookupError:
1717
return None
1818
return False if issubclass(model, EmbeddedModel) else None
19+
20+
21+
class EncryptionRouter:
22+
"""
23+
Routes database operations for 'encrypted' models to the 'encryption' DB.
24+
"""
25+
26+
def db_for_read(self, model, **hints):
27+
if getattr(model, "encrypted_fields_map", False):
28+
return "encryption"
29+
return None
30+
31+
def db_for_write(self, model, **hints):
32+
if getattr(model, "encrypted_fields_map", False):
33+
return "encryption"
34+
return None
35+
36+
def allow_migrate(self, db, app_label, model_name=None, **hints):
37+
"""
38+
Ensure that the 'encrypted' models only appear in the 'encryption' DB,
39+
and not in the default DB.
40+
"""
41+
model = hints.get("model")
42+
if model and getattr(model, "encrypted_fields_map", False):
43+
return db == "encryption"
44+
return None

0 commit comments

Comments
 (0)