mirror of
https://github.com/jazzband/django-fernet-encrypted-fields.git
synced 2026-03-16 22:40:27 +00:00
Bug fix for #4
This is a bug fix for issue #4 where Django Admin raises an exception when saving Encrypted fields as the value is not encrypted at the time of the clean process. This PR sets a semaphore property which is checked within the to_python method allowing the decryption to be skipped. It then removes the semaphore property to clean up the field
This commit is contained in:
parent
22a5cd4f84
commit
9630b3e5e2
1 changed files with 11 additions and 1 deletions
|
|
@ -51,11 +51,21 @@ class EncryptedFieldMixin(object):
|
|||
return self.to_python(value)
|
||||
|
||||
def to_python(self, value):
|
||||
if value is None or not isinstance(value, str):
|
||||
if value is None or not isinstance(value, str) or hasattr(self, '_already_decrypted'):
|
||||
return value
|
||||
value = self.f.decrypt(bytes(value, 'utf-8')).decode('utf-8')
|
||||
return super(EncryptedFieldMixin, self).to_python(value)
|
||||
|
||||
def clean(self, value, model_instance):
|
||||
"""
|
||||
Create and assign a semaphore so that to_python method will not try to decrypt an already decrypted value
|
||||
during cleaning of a form
|
||||
"""
|
||||
self._already_decrypted = True
|
||||
ret = super().clean(value, model_instance)
|
||||
del self._already_decrypted
|
||||
return ret
|
||||
|
||||
|
||||
class EncryptedCharField(EncryptedFieldMixin, models.CharField):
|
||||
pass
|
||||
|
|
|
|||
Loading…
Reference in a new issue