Exemple #1
0
        def convert_values(self, value, field):
            from django.db.models.fields import DateField, DateTimeField, \
                 TimeField, BooleanField, NullBooleanField, DecimalField, FloatField, Field

            # Oracle stores empty strings as null. We need to undo this in
            # order to adhere to the Django convention of using the empty
            # string instead of null, but only if the field accepts the
            # empty string.
            if value is None:
                pass
            elif value is None and isinstance(field, Field) and field.empty_strings_allowed:
                value = u''
            # Convert 1 or 0 to True or False
            elif isinstance(value, float):
                value = float(value)
            # Added 04-26-2009 to repair "Invalid literal for int() base 10" error
            elif isinstance(value,int):
                value = int(value)
            elif field is not None and field.get_internal_type() == 'AutoField':
                value = int(float(value))
            elif value in (1, 0) and field is not None and field.get_internal_type() in ('BooleanField', 'NullBooleanField'):
                value = bool(value)
            # Force floats to the correct type
            elif field is not None and field.get_internal_type() == 'FloatField':
                value = float(value)
            # Convert floats to decimals
            elif field is not None and field.get_internal_type() == 'DecimalField':
                value = util.typecast_decimal(field.format_number(value))
            elif field is not None and field.get_internal_type() == 'SmallIntegerField':
                value = util.typecast_decimal(field.format_number(value))
            return value
        def convert_values(self, value, field):
            from django.db.models.fields import (
                DateField,
                DateTimeField,
                TimeField,
                BooleanField,
                NullBooleanField,
                DecimalField,
                Field,
            )

            if isinstance(value, Database.LOB):
                value = value.read()
                if field and field.get_internal_type() == "TextField":
                    value = force_unicode(value)

            # Oracle stores empty strings as null. We need to undo this in
            # order to adhere to the Django convention of using the empty
            # string instead of null, but only if the field accepts the
            # empty string.
            if value is None and isinstance(field, Field) and field.empty_strings_allowed:
                value = u""
            # Convert 1 or 0 to True or False
            elif value in (1, 0) and isinstance(field, (BooleanField, NullBooleanField)):
                value = bool(value)
            # Convert floats to decimals
            elif value is not None and isinstance(field, DecimalField):
                value = util.typecast_decimal(field.format_number(value))
            # cx_Oracle always returns datetime.datetime objects for
            # DATE and TIMESTAMP columns, but Django wants to see a
            # python datetime.date, .time, or .datetime.  We use the type
            # of the Field to determine which to cast to, but it's not
            # always available.
            # As a workaround, we cast to date if all the time-related
            # values are 0, or to time if the date is 1/1/1900.
            # This could be cleaned a bit by adding a method to the Field
            # classes to normalize values from the database (the to_python
            # method is used for validation and isn't what we want here).
            elif isinstance(value, Database.Timestamp):
                # In Python 2.3, the cx_Oracle driver returns its own
                # Timestamp object that we must convert to a datetime class.
                if not isinstance(value, datetime.datetime):
                    value = datetime.datetime(
                        value.year, value.month, value.day, value.hour, value.minute, value.second, value.fsecond
                    )
                if isinstance(field, DateTimeField):
                    # DateTimeField subclasses DateField so must be checked
                    # first.
                    pass
                elif isinstance(field, DateField):
                    value = value.date()
                elif isinstance(field, TimeField) or (value.year == 1900 and value.month == value.day == 1):
                    value = value.time()
                elif value.hour == value.minute == value.second == value.microsecond == 0:
                    value = value.date()
            return value
 def convert_values(self, value, field):
     if value is not None and field and field.get_internal_type() == 'DecimalField':
         value = util.typecast_decimal(field.format_number(value))
     elif value in (1, 0) and field and field.get_internal_type() in ('BooleanField', 'NullBooleanField'):
         value = bool(value)
     # Force floats to the correct type
     elif value is not None and field and field.get_internal_type() == 'FloatField':
         value = float(value)
     elif value is not None and field and (field.get_internal_type().endswith('IntegerField')
                                           or field.get_internal_type() == 'AutoField'):
         return int(value)
     return value
Exemple #4
0
 def resolve_columns(self, row, fields=()):
     from django.db.models.fields import DateField, DateTimeField, \
         TimeField, BooleanField, NullBooleanField, DecimalField, Field
     values = []
     for value, field in map(None, row, fields):
         if isinstance(value, Database.LOB):
             value = value.read()
         # Oracle stores empty strings as null. We need to undo this in
         # order to adhere to the Django convention of using the empty
         # string instead of null, but only if the field accepts the
         # empty string.
         if value is None and isinstance(
                 field, Field) and field.empty_strings_allowed:
             value = u''
         # Convert 1 or 0 to True or False
         elif value in (1, 0) and isinstance(
                 field, (BooleanField, NullBooleanField)):
             value = bool(value)
         # Convert floats to decimals
         elif value is not None and isinstance(field, DecimalField):
             value = util.typecast_decimal(
                 field.format_number(value))
         # cx_Oracle always returns datetime.datetime objects for
         # DATE and TIMESTAMP columns, but Django wants to see a
         # python datetime.date, .time, or .datetime.  We use the type
         # of the Field to determine which to cast to, but it's not
         # always available.
         # As a workaround, we cast to date if all the time-related
         # values are 0, or to time if the date is 1/1/1900.
         # This could be cleaned a bit by adding a method to the Field
         # classes to normalize values from the database (the to_python
         # method is used for validation and isn't what we want here).
         elif isinstance(value, Database.Timestamp):
             # In Python 2.3, the cx_Oracle driver returns its own
             # Timestamp object that we must convert to a datetime class.
             if not isinstance(value, datetime.datetime):
                 value = datetime.datetime(value.year, value.month,
                                           value.day, value.hour,
                                           value.minute,
                                           value.second,
                                           value.fsecond)
             if isinstance(field, DateTimeField):
                 pass  # DateTimeField subclasses DateField so must be checked first.
             elif isinstance(field, DateField):
                 value = value.date()
             elif isinstance(field, TimeField) or (
                     value.year == 1900
                     and value.month == value.day == 1):
                 value = value.time()
             elif value.hour == value.minute == value.second == value.microsecond == 0:
                 value = value.date()
         values.append(value)
     return values
Exemple #5
0
        def convert_values(self, value, field):
            if isinstance(value, Database.LOB):
                value = value.read()
                if field and field.get_internal_type() == 'TextField':
                    value = force_unicode(value)

            # Oracle stores empty strings as null. We need to undo this in
            # order to adhere to the Django convention of using the empty
            # string instead of null, but only if the field accepts the
            # empty string.
            if value is None and field and field.empty_strings_allowed:
                value = u''
            # Convert 1 or 0 to True or False
            elif value in (1, 0) and field and field.get_internal_type() in (
                    'BooleanField', 'NullBooleanField'):
                value = bool(value)
            # Force floats to the correct type
            elif value is not None and field and field.get_internal_type(
            ) == 'FloatField':
                value = float(value)
            # Convert floats to decimals
            elif value is not None and field and field.get_internal_type(
            ) == 'DecimalField':
                value = util.typecast_decimal(field.format_number(value))
            # cx_Oracle always returns datetime.datetime objects for
            # DATE and TIMESTAMP columns, but Django wants to see a
            # python datetime.date, .time, or .datetime.  We use the type
            # of the Field to determine which to cast to, but it's not
            # always available.
            # As a workaround, we cast to date if all the time-related
            # values are 0, or to time if the date is 1/1/1900.
            # This could be cleaned a bit by adding a method to the Field
            # classes to normalize values from the database (the to_python
            # method is used for validation and isn't what we want here).
            elif isinstance(value, Database.Timestamp):
                # In Python 2.3, the cx_Oracle driver returns its own
                # Timestamp object that we must convert to a datetime class.
                if not isinstance(value, datetime.datetime):
                    value = datetime.datetime(value.year, value.month,
                                              value.day, value.hour,
                                              value.minute, value.second,
                                              value.fsecond)
                if field and field.get_internal_type() == 'DateTimeField':
                    pass
                elif field and field.get_internal_type() == 'DateField':
                    value = value.date()
                elif field and field.get_internal_type() == 'TimeField' or (
                        value.year == 1900 and value.month == value.day == 1):
                    value = value.time()
                elif value.hour == value.minute == value.second == value.microsecond == 0:
                    value = value.date()
            return value
Exemple #6
0
 def convert_values(self, value, field):
     if value is not None and field and field.get_internal_type(
     ) == 'DecimalField':
         value = util.typecast_decimal(field.format_number(value))
     elif value in (1, 0) and field and field.get_internal_type() in (
             'BooleanField', 'NullBooleanField'):
         value = bool(value)
     # Force floats to the correct type
     elif value is not None and field and field.get_internal_type(
     ) == 'FloatField':
         value = float(value)
     elif value is not None and field and (
             field.get_internal_type().endswith('IntegerField')
             or field.get_internal_type() == 'AutoField'):
         return int(value)
     return value
Exemple #7
0
def convert_values_sqlite(self, value, field):
    """SQLite returns floats when it should be returning decimals,
    and gets dates and datetimes wrong.
    For consistency with other backends, coerce when required.
    """
    internal_type = field.get_internal_type()
    if internal_type == 'DecimalField':
        return util.typecast_decimal(field.format_number(value))
    elif internal_type and internal_type.endswith('IntegerField') or internal_type.endswith('AutoField'): 
        return int(value)
    elif internal_type == 'DateField':
        return util.typecast_date(value)
    elif internal_type == 'DateTimeField':
        return util.typecast_timestamp(value)
    elif internal_type == 'TimeField':
        return util.typecast_time(value)

    # No field, or the field isn't known to be a decimal or integer
    return value
Exemple #8
0
def convert_values_sqlite(self, value, field):
    """SQLite returns floats when it should be returning decimals,
    and gets dates and datetimes wrong.
    For consistency with other backends, coerce when required.
    """
    internal_type = field.get_internal_type()
    if internal_type == 'DecimalField':
        return util.typecast_decimal(field.format_number(value))
    elif internal_type and internal_type.endswith(
            'IntegerField') or internal_type.endswith('AutoField'):
        return int(value)
    elif internal_type == 'DateField':
        return util.typecast_date(value)
    elif internal_type == 'DateTimeField':
        return util.typecast_timestamp(value)
    elif internal_type == 'TimeField':
        return util.typecast_time(value)

    # No field, or the field isn't known to be a decimal or integer
    return value
Exemple #9
0
    def convert_values(self, value, field):
        if isinstance(value, Database.LOB):
            value = value.read()
            if field and field.get_internal_type() == 'TextField':
                value = force_text(value)

        # Oracle stores empty strings as null. We need to undo this in
        # order to adhere to the Django convention of using the empty
        # string instead of null, but only if the field accepts the
        # empty string.
        if value is None and field and field.empty_strings_allowed:
            value = ''
        # Convert 1 or 0 to True or False
        elif value in (1, 0) and field and field.get_internal_type() in ('BooleanField', 'NullBooleanField'):
            value = bool(value)
        # Force floats to the correct type
        elif value is not None and field and field.get_internal_type() == 'FloatField':
            value = float(value)
        # Convert floats to decimals
        elif value is not None and field and field.get_internal_type() == 'DecimalField':
            value = util.typecast_decimal(field.format_number(value))
        # cx_Oracle always returns datetime.datetime objects for
        # DATE and TIMESTAMP columns, but Django wants to see a
        # python datetime.date, .time, or .datetime.  We use the type
        # of the Field to determine which to cast to, but it's not
        # always available.
        # As a workaround, we cast to date if all the time-related
        # values are 0, or to time if the date is 1/1/1900.
        # This could be cleaned a bit by adding a method to the Field
        # classes to normalize values from the database (the to_python
        # method is used for validation and isn't what we want here).
        elif isinstance(value, Database.Timestamp):
            if field and field.get_internal_type() == 'DateTimeField':
                pass
            elif field and field.get_internal_type() == 'DateField':
                value = value.date()
            elif field and field.get_internal_type() == 'TimeField' or (value.year == 1900 and value.month == value.day == 1):
                value = value.time()
            elif value.hour == value.minute == value.second == value.microsecond == 0:
                value = value.date()
        return value
Exemple #10
0
 def convert_values(self, value, field):
     value = super(DatabaseOperations, self).convert_values(value, field)
     if value is not None and field and field.get_internal_type() == "DecimalField":
         value = util.typecast_decimal(field.format_number(value))
     return value