Handle date, time, and datetime column types.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import base64
|
import base64
|
||||||
import six
|
import six
|
||||||
|
import datetime
|
||||||
from sqlalchemy.types import TypeDecorator, String, Binary
|
from sqlalchemy.types import TypeDecorator, String, Binary
|
||||||
from sqlalchemy_utils.exceptions import ImproperlyConfigured
|
from sqlalchemy_utils.exceptions import ImproperlyConfigured
|
||||||
from .scalar_coercible import ScalarCoercible
|
from .scalar_coercible import ScalarCoercible
|
||||||
@@ -224,10 +225,14 @@ class EncryptedType(TypeDecorator, ScalarCoercible):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
# Doesn't have 'process_bind_param'
|
# Doesn't have 'process_bind_param'
|
||||||
|
|
||||||
# Handle 'boolean'
|
# Handle 'boolean' and 'dates'
|
||||||
if issubclass(self.underlying_type.python_type, bool):
|
type_ = self.underlying_type.python_type
|
||||||
|
if issubclass(type_, bool):
|
||||||
value = "true" if value else "false"
|
value = "true" if value else "false"
|
||||||
|
|
||||||
|
elif issubclass(type_, (datetime.date, datetime.time)):
|
||||||
|
value = value.isoformat()
|
||||||
|
|
||||||
return self.engine.encrypt(value)
|
return self.engine.encrypt(value)
|
||||||
|
|
||||||
def process_result_value(self, value, dialect):
|
def process_result_value(self, value, dialect):
|
||||||
@@ -243,10 +248,23 @@ class EncryptedType(TypeDecorator, ScalarCoercible):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
# Doesn't have 'process_result_value'
|
# Doesn't have 'process_result_value'
|
||||||
|
|
||||||
# Handle 'boolean'
|
# Handle 'boolean' and 'dates'
|
||||||
if issubclass(self.underlying_type.python_type, bool):
|
type_ = self.underlying_type.python_type
|
||||||
|
if issubclass(type_, bool):
|
||||||
return decrypted_value == "true"
|
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
|
# Handle all others
|
||||||
return self.underlying_type.python_type(decrypted_value)
|
return self.underlying_type.python_type(decrypted_value)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user