Example #1
0
 def convert_decimalfield_value(self, value, expression, connection,
                                context):
     field = expression.field
     val = utils.format_number(value, field.max_digits,
                               field.decimal_places)
     value = utils.typecast_decimal(val)
     return value
Example #2
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:
            if field.get_internal_type() == 'BinaryField':
                value = b''
            else:
                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 = backend_utils.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
Example #3
0
 def convert_value(self, value, expression, connection, context):
     """
     Expressions provide their own converters because users have the option
     of manually specifying the output_field which may be a different type
     from the one the database returns.
     """
     field = self.output_field
     internal_type = field.get_internal_type()
     if value is None:
         return value
     elif internal_type == 'FloatField':
         return float(value)
     elif internal_type.endswith('IntegerField'):
         return int(value)
     elif internal_type == 'DecimalField':
         return backend_utils.typecast_decimal(value)
     return value
Example #4
0
 def convert_value(self, value, expression, connection, context):
     """
     Expressions provide their own converters because users have the option
     of manually specifying the output_field which may be a different type
     from the one the database returns.
     """
     field = self.output_field
     internal_type = field.get_internal_type()
     if value is None:
         return value
     elif internal_type == 'FloatField':
         return float(value)
     elif internal_type.endswith('IntegerField'):
         return int(value)
     elif internal_type == 'DecimalField':
         return backend_utils.typecast_decimal(value)
     return value
Example #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_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:
            if field.get_internal_type() == 'BinaryField':
                value = b''
            else:
                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 = backend_utils.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
Example #6
0
    def convert_values(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.
        """
        if value is None:
            return None

        internal_type = field.get_internal_type()
        if internal_type == "DecimalField":
            return backend_utils.typecast_decimal(field.format_number(value))
        elif internal_type and internal_type.endswith("IntegerField") or internal_type == "AutoField":
            return int(value)
        elif internal_type == "DateField":
            return parse_date(value)
        elif internal_type == "DateTimeField":
            return parse_datetime_with_timezone_support(value)
        elif internal_type == "TimeField":
            return parse_time(value)

        # No field, or the field isn't known to be a decimal or integer
        return value
Example #7
0
    def convert_values(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.
        """
        if value is None:
            return None

        internal_type = field.get_internal_type()
        if internal_type == 'DecimalField':
            return backend_utils.typecast_decimal(field.format_number(value))
        elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
            return int(value)
        elif internal_type == 'DateField':
            return parse_date(value)
        elif internal_type == 'DateTimeField':
            return parse_datetime_with_timezone_support(value)
        elif internal_type == 'TimeField':
            return parse_time(value)

        # No field, or the field isn't known to be a decimal or integer
        return value
Example #8
0
 def convert_decimalfield_value(self, value, expression, connection, context):
     return backend_utils.typecast_decimal(expression.output_field.format_number(value))
Example #9
0
 def convert_decimalfield_value(self, value, expression, connection, context):
     if value is not None:
         value = expression.output_field.format_number(value)
         value = backend_utils.typecast_decimal(value)
     return value
Example #10
0
 def convert_decimalfield_value(self, value, expression, connection,
                                context):
     if value is not None:
         value = expression.output_field.format_number(value)
         value = backend_utils.typecast_decimal(value)
     return value
Example #11
0
def typecast_decimal(v):
    if isinstance(v, bytes):
        v = v.decode('ascii')
    return backend_utils.typecast_decimal(v)
Example #12
0
 def convert_decimalfield_value(self, value, field):
     return backend_utils.typecast_decimal(field.format_number(value))
Example #13
0
 def convert_decimalfield_value(self, value, expression, context):
     return backend_utils.typecast_decimal(expression.output_field.format_number(value))
Example #14
0
 def convert_decimalfield_value(self, value, field):
     if value is not None:
         value = backend_utils.typecast_decimal(field.format_number(value))
     return value
Example #15
0
 def convert_decimalfield_value(self, value, field):
     return backend_utils.typecast_decimal(field.format_number(value))
from __future__ import unicode_literals
Example #17
0
 def convert_decimalfield_value(self, value, field):
     if value is not None:
         value = backend_utils.typecast_decimal(field.format_number(value))
     return value
Example #18
0
def typecast_decimal(v):
    if isinstance(v, bytes):
        v = v.decode('ascii')
    return backend_utils.typecast_decimal(v)