Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an inheritance issue where enxrypted fields would fail parsing … #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions fernet_fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'EncryptedDateField',
'EncryptedDateTimeField',
'EncryptedBinaryField',
'EncryptedJSONField',
]


Expand Down Expand Up @@ -76,8 +77,10 @@ def get_db_prep_save(self, value, connection):

def from_db_value(self, value, expression, connection, *args):
if value is not None:
value = bytes(value)
return self.to_python(force_text(self.fernet.decrypt(value)))
value = self.to_python(force_text(self.fernet.decrypt(bytes(value))))
return super(
EncryptedField, self
).from_db_value(value, expression, connection, *args)

@cached_property
def validators(self):
Expand Down Expand Up @@ -134,6 +137,8 @@ class EncryptedDateTimeField(EncryptedField, models.DateTimeField):


class EncryptedBinaryField(EncryptedField, models.BinaryField):
def from_db_value(self, value, expression, connection, *args):
if value is not None:
return self.to_python(self.fernet.decrypt(bytes(value)))
pass


class EncryptedJSONField(EncryptedField, models.JSONField):
pass
4 changes: 4 additions & 0 deletions fernet_fields/test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ class EncryptedDateTime(models.Model):

class EncryptedNullable(models.Model):
value = fields.EncryptedIntegerField(null=True)


class EncryptedJSON(models.Model):
value = fields.EncryptedJSONField()
1 change: 1 addition & 0 deletions fernet_fields/test/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def test_get_integer_field_validators(self):
(models.EncryptedEmail, ['[email protected]', '[email protected]']),
(models.EncryptedInt, [1, 2]),
(models.EncryptedDate, [date(2015, 2, 5), date(2015, 2, 8)]),
(models.EncryptedJSON, [{'test': 1}, {'test': 1}]),
(
models.EncryptedDateTime,
[datetime(2015, 2, 5, 15), datetime(2015, 2, 8, 16)],
Expand Down