mirror of
https://github.com/jazzband/django-fernet-encrypted-fields.git
synced 2026-03-16 22:40:27 +00:00
Fix get_prep_value and to_python function
This commit is contained in:
parent
9ec3563bfa
commit
025afa1987
2 changed files with 21 additions and 22 deletions
|
|
@ -2,7 +2,7 @@ import base64
|
|||
from django.utils import timezone
|
||||
|
||||
import warnings
|
||||
from cryptography.fernet import Fernet, MultiFernet
|
||||
from cryptography.fernet import Fernet, MultiFernet, InvalidToken
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives import hashes
|
||||
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
||||
|
|
@ -49,6 +49,7 @@ class EncryptedFieldMixin(object):
|
|||
return "TextField"
|
||||
|
||||
def get_prep_value(self, value):
|
||||
value = super().get_prep_value(value)
|
||||
if value:
|
||||
if not isinstance(value, str):
|
||||
value = str(value)
|
||||
|
|
@ -70,7 +71,12 @@ class EncryptedFieldMixin(object):
|
|||
or hasattr(self, "_already_decrypted")
|
||||
):
|
||||
return value
|
||||
value = self.f.decrypt(bytes(value, "utf-8")).decode("utf-8")
|
||||
try:
|
||||
value = self.f.decrypt(bytes(value, "utf-8")).decode("utf-8")
|
||||
except InvalidToken:
|
||||
pass
|
||||
except UnicodeEncodeError:
|
||||
pass
|
||||
return super(EncryptedFieldMixin, self).to_python(value)
|
||||
|
||||
def clean(self, value, model_instance):
|
||||
|
|
@ -97,18 +103,6 @@ class EncryptedDateTimeField(EncryptedFieldMixin, models.DateTimeField):
|
|||
|
||||
|
||||
class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):
|
||||
def get_prep_value(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
value = int(value)
|
||||
except (TypeError, ValueError) as e:
|
||||
raise e.__class__(
|
||||
"Field '%s' expected a number but got %r." % (self.name, value),
|
||||
) from e
|
||||
else:
|
||||
return super().get_prep_value(value)
|
||||
|
||||
@cached_property
|
||||
def validators(self):
|
||||
return [*self.default_validators, *self._validators]
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import re
|
|||
from django.db import connection
|
||||
from django.test import TestCase, override_settings
|
||||
from django.utils import timezone
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from .models import TestModel
|
||||
|
||||
|
|
@ -64,8 +65,9 @@ class FieldTest(TestCase):
|
|||
|
||||
plaintext = "text"
|
||||
|
||||
model.datetime = plaintext
|
||||
model.save()
|
||||
with self.assertRaises(ValidationError):
|
||||
model.datetime = plaintext
|
||||
model.save()
|
||||
|
||||
def test_integer_field_encrypted(self):
|
||||
plaintext = 42
|
||||
|
|
@ -103,8 +105,9 @@ class FieldTest(TestCase):
|
|||
|
||||
plaintext = "text"
|
||||
|
||||
model.date = plaintext
|
||||
model.save()
|
||||
with self.assertRaises(ValidationError):
|
||||
model.date = plaintext
|
||||
model.save()
|
||||
|
||||
def test_float_field_encrypted(self):
|
||||
plaintext = 42.44
|
||||
|
|
@ -123,8 +126,9 @@ class FieldTest(TestCase):
|
|||
|
||||
plaintext = "text"
|
||||
|
||||
model.floating = plaintext
|
||||
model.save()
|
||||
with self.assertRaises(ValueError):
|
||||
model.floating = plaintext
|
||||
model.save()
|
||||
|
||||
def test_email_field_encrypted(self):
|
||||
plaintext = "test@gmail.com"
|
||||
|
|
@ -168,8 +172,9 @@ class FieldTest(TestCase):
|
|||
|
||||
plaintext = "text"
|
||||
|
||||
model.boolean = plaintext
|
||||
model.save()
|
||||
with self.assertRaises(ValidationError):
|
||||
model.boolean = plaintext
|
||||
model.save()
|
||||
|
||||
|
||||
class RotatedSaltTestCase(TestCase):
|
||||
|
|
|
|||
Loading…
Reference in a new issue