Skip to content

Commit

Permalink
Fixed an inheritance issue where enxrypted fields would fail parsing …
Browse files Browse the repository at this point in the history
…db data to the correct type
  • Loading branch information
Hans de Jong committed Feb 9, 2023
1 parent c04fb4f commit fe5cfc4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
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, ['a@example.com', 'b@example.com']),
(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

0 comments on commit fe5cfc4

Please sign in to comment.