Skip to content

Commit

Permalink
Fix issue getting IntegerField validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed May 28, 2015
1 parent a692d44 commit 167c67a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGES
master (unreleased)
-------------------

* Fix issue getting IntegerField validators.


0.2 (2015.05.28)
----------------
Expand Down
17 changes: 16 additions & 1 deletion fernet_fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

class EncryptedField(models.Field):
"""A field that encrypts values using Fernet symmetric encryption."""
_internal_type = 'BinaryField'

def __init__(self, *args, **kwargs):
if kwargs.get('primary_key'):
raise ImproperlyConfigured(
Expand Down Expand Up @@ -69,7 +71,7 @@ def fernet(self):
return MultiFernet([Fernet(k) for k in self.fernet_keys])

def get_internal_type(self):
return 'BinaryField'
return self._internal_type

def get_db_prep_save(self, value, connection):
value = super(
Expand All @@ -90,6 +92,19 @@ def from_db_value(self, value, expression, connection, context):
value = bytes(value)
return self.to_python(force_text(self.fernet.decrypt(value)))

@cached_property
def validators(self):
# Temporarily pretend to be whatever type of field we're masquerading
# as, for purposes of constructing validators (needed for
# IntegerField and subclasses).
self.__dict__['_internal_type'] = super(
EncryptedField, self
).get_internal_type()
try:
return super(EncryptedField, self).validators
finally:
del self.__dict__['_internal_type']


class EncryptedTextField(EncryptedField, models.TextField):
pass
Expand Down
6 changes: 6 additions & 0 deletions fernet_fields/test/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_not_allowed(self, key):
with pytest.raises(ImproperlyConfigured):
fields.EncryptedIntegerField(**{key: True})

def test_get_integer_field_validators(self):
f = fields.EncryptedIntegerField()

# Raises no error
f.validators


@pytest.mark.parametrize(
'model,vals',
Expand Down

0 comments on commit 167c67a

Please sign in to comment.