Skip to content

Commit 10a361e

Browse files
committed
Move encrypted_fields_map to schema (1/2)
1 parent eab2f2e commit 10a361e

File tree

3 files changed

+5
-23
lines changed

3 files changed

+5
-23
lines changed

django_mongodb_backend/fields/encryption.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33

44
class EncryptedCharField(models.CharField):
5-
def __init__(self, *args, **kwargs):
6-
super().__init__(*args, **kwargs)
7-
self.encrypted = True
5+
encrypted = True

django_mongodb_backend/models.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,8 @@ def save(self, *args, **kwargs):
1616
raise NotSupportedError("EmbeddedModels cannot be saved.")
1717

1818

19-
class EncryptedModelBase(models.base.ModelBase):
20-
def __new__(cls, name, bases, attrs, **kwargs):
21-
new_class = super().__new__(cls, name, bases, attrs, **kwargs)
19+
class EncryptedModel(models.Model):
20+
encrypted = True
2221

23-
# Build a map of encrypted fields
24-
encrypted_fields = {
25-
"fields": {
26-
field.name: field.__class__.__name__
27-
for field in new_class._meta.fields
28-
if getattr(field, "encrypted", False)
29-
}
30-
}
31-
32-
# Store it as a class-level attribute
33-
new_class.encrypted_fields_map = encrypted_fields
34-
return new_class
35-
36-
37-
class EncryptedModel(models.Model, metaclass=EncryptedModelBase):
3822
class Meta:
3923
abstract = True

django_mongodb_backend/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def _create_collection(self, model):
424424
Create a collection or encrypted collection for the model.
425425
"""
426426

427-
if hasattr(model, "encrypted_fields_map"):
427+
if hasattr(model, "encrypted"):
428428
auto_encryption_opts = self.connection.settings_dict.get("OPTIONS", {}).get(
429429
"auto_encryption_opts"
430430
)
@@ -433,7 +433,7 @@ def _create_collection(self, model):
433433
client_encryption.create_encrypted_collection(
434434
client.database,
435435
model._meta.db_table,
436-
model.encrypted_fields_map,
436+
{"fields": []},
437437
"local", # TODO: KMS provider should be configurable
438438
)
439439
else:

0 commit comments

Comments
 (0)