Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions django_cubrid/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import sys
import django
import uuid
import warnings

try:
Expand All @@ -25,6 +26,7 @@
from django_cubrid.introspection import DatabaseIntrospection
from django_cubrid.validation import DatabaseValidation
from django.utils import timezone
from django.utils.encoding import froce_text
from django.conf import settings
if django.VERSION >= (1, 7) and django.VERSION < (1, 8):
from django_cubrid.schema import DatabaseSchemaEditor
Expand Down Expand Up @@ -340,15 +342,53 @@ def bulk_insert_sql(self, fields, placeholder_rows):
def get_db_converters(self, expression):
converters = super().get_db_converters(expression)
internal_type = expression.output_field.get_internal_type()
if internal_type in ["BooleanField", "NullBooleanField"]:
if internal_type == 'TextField':
converters.append(self.convert_textfield_value)
elif internal_type in ['BooleanField', 'NullBooleanField']:
converters.append(self.convert_booleanfield_value)
elif internal_type == 'DateTimeField':
if settings.USE_TZ:
converters.append(self.convert_datetimefield_value)
elif internal_type == 'UUIDField':
converters.append(self.convert_uuidfield_value)
elif internal_type in ['IPAddressField', 'GenericIPAddressField']:
converters.append(self.convert_ipaddress_value)
return converters

def convert_binaryfield_value(self, value, expression, connection):
if not value.startswith('0B'):
raise ValueError('Unexpected value: %s' % value)
value = value[2:]
def gen_bytes():
for i in range(0, len(value), 8):
yield int(value[i:i + 8], 2)
value = bytes(gen_bytes())
return value

def convert_textfield_value(self, value, expression, connection):
if value is not None:
value = force_text(value)
return value

def convert_booleanfield_value(self, value, expression, connection):
if value in (0, 1):
value = bool(value)
return value

def convert_datetimefield_value(self, value, expression, connection):
if value is not None:
value = timezone.make_aware(value, self.connection.timezone)
return value

def convert_uuidfield_value(self, value, expression, connection):
if value is not None:
value = uuid.UUID(value)
return value

def convert_ipaddress_value(self, value, expression, connection):
if value is not None:
value = value.strip()
return value

class DatabaseWrapper(BaseDatabaseWrapper):
vendor = 'cubrid'
Expand Down Expand Up @@ -532,18 +572,3 @@ def _savepoint_commit(self, sid):
if django.VERSION >= (1, 7) and django.VERSION < (1, 8):
def schema_editor(self, *args, **kwargs):
return DatabaseSchemaEditor(self, *args, **kwargs)


def django_fetch_value_converter(row, descriptor):
# We need to convert naive datetime values to aware, if USE_TZ is true.
if not settings.USE_TZ:
return row

index = 0
for des in descriptor:
if des[1] == FIELD_TYPE.DATETIME and timezone.is_naive(row[index]):
row[index] = row[index].replace(tzinfo=timezone.utc)

index = index + 1

return row