From ce4a9bba1855eb8c49054e95c6ed698c63f96d91 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Thu, 30 Oct 2014 13:38:10 -0700 Subject: [PATCH] Add support for 'coercion_listener' to the encrypted type. --- sqlalchemy_utils/types/encrypted.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sqlalchemy_utils/types/encrypted.py b/sqlalchemy_utils/types/encrypted.py index aa1506f..e0b0957 100644 --- a/sqlalchemy_utils/types/encrypted.py +++ b/sqlalchemy_utils/types/encrypted.py @@ -3,6 +3,7 @@ import base64 import six from sqlalchemy.types import TypeDecorator, String, Binary from sqlalchemy_utils.exceptions import ImproperlyConfigured +from .scalar_coercible import ScalarCoercible cryptography = None try: @@ -111,7 +112,7 @@ class FernetEngine(EncryptionDecryptionBaseEngine): return decrypted -class EncryptedType(TypeDecorator): +class EncryptedType(TypeDecorator, ScalarCoercible): """ EncryptedType provides a way to encrypt and decrypt values, to and from databases, that their type is a basic SQLAlchemy type. @@ -223,3 +224,9 @@ class EncryptedType(TypeDecorator): self._update_key() decrypted_value = self.engine.decrypt(value) return self.underlying_type.python_type(decrypted_value) + + def _coerce(self, value): + if isinstance(self.underlying_type, ScalarCoercible): + return self.underlying_type._coerce(value) + + return value