Handle date, time, and datetime column types.

This commit is contained in:
Ryan Leckey
2014-10-30 14:16:57 -07:00
parent 04d612a185
commit cae6d5431d

View File

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import base64
import six
import datetime
from sqlalchemy.types import TypeDecorator, String, Binary
from sqlalchemy_utils.exceptions import ImproperlyConfigured
from .scalar_coercible import ScalarCoercible
@@ -224,10 +225,14 @@ class EncryptedType(TypeDecorator, ScalarCoercible):
except AttributeError:
# Doesn't have 'process_bind_param'
# Handle 'boolean'
if issubclass(self.underlying_type.python_type, bool):
# Handle 'boolean' and 'dates'
type_ = self.underlying_type.python_type
if issubclass(type_, bool):
value = "true" if value else "false"
elif issubclass(type_, (datetime.date, datetime.time)):
value = value.isoformat()
return self.engine.encrypt(value)
def process_result_value(self, value, dialect):
@@ -243,10 +248,23 @@ class EncryptedType(TypeDecorator, ScalarCoercible):
except AttributeError:
# Doesn't have 'process_result_value'
# Handle 'boolean'
if issubclass(self.underlying_type.python_type, bool):
# Handle 'boolean' and 'dates'
type_ = self.underlying_type.python_type
if issubclass(type_, bool):
return decrypted_value == "true"
elif issubclass(type_, datetime.time):
return datetime.datetime.strptime(
decrypted_value, "%H:%M:%S").time()
elif issubclass(type_, datetime.date):
return datetime.datetime.strptime(
decrypted_value, "%Y-%m-%d").date()
elif issubclass(type_, datetime.datetime):
return datetime.datetime.strptime(
decrypted_value, "%Y-%m-%dT%H:%M:%S")
# Handle all others
return self.underlying_type.python_type(decrypted_value)