Exemplo n.º 1
0
    def adapt_timefield_value(self, value):
        if value is None:
            return None

        # MySQL doesn't support tz-aware times
        if timezone.is_aware(value):
            raise ValueError(
                "MySQL backend does not support timezone-aware times.")

        return six.text_type(value)
Exemplo n.º 2
0
 def default(self, obj):
     if isinstance(obj, decimal.Decimal):
         return str(obj)
     if isinstance(obj, datetime.datetime):
         if timezone.is_aware(obj):
             obj = timezone.localtime(obj)
         return obj.strftime('%Y-%m-%d %H:%M:%S')
     if isinstance(obj, datetime.date):
         return obj.strftime('%Y-%m-%d')
     return json.JSONEncoder.default(self, obj)
Exemplo n.º 3
0
    def adapt_timefield_value(self, value):
        if value is None:
            return None

        # MySQL doesn't support tz-aware times
        if timezone.is_aware(value):
            raise ValueError(
                "MySQL backend does not support timezone-aware times.")

        return six.text_type(value)
Exemplo n.º 4
0
    def adapt_datetimefield_value(self, value):
        if value is None:
            return None

        # MySQL doesn't support tz-aware datetimes
        if timezone.is_aware(value):
            if settings.USE_TZ:
                value = timezone.make_naive(value, self.connection.timezone)
            else:
                raise ValueError(
                    "MySQL backend does not support timezone-aware datetimes when USE_TZ is False.")

        if not self.connection.features.supports_microsecond_precision:
            value = value.replace(microsecond=0)

        return six.text_type(value)
Exemplo n.º 5
0
    def adapt_datetimefield_value(self, value):
        if value is None:
            return None

        # MySQL doesn't support tz-aware datetimes
        if timezone.is_aware(value):
            if settings.USE_TZ:
                value = timezone.make_naive(value, self.connection.timezone)
            else:
                raise ValueError(
                    "MySQL backend does not support timezone-aware datetimes when USE_TZ is False."
                )

        if not self.connection.features.supports_microsecond_precision:
            value = value.replace(microsecond=0)

        return six.text_type(value)
Exemplo n.º 6
0
 def default(self, obj):
     # For Date Time string spec, see ECMA 262
     # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
     if isinstance(obj, datetime.datetime):
         representation = obj.isoformat()
         # NOTE: we store microseconds with full precision, technically not a valid isostring...
         # if obj.microsecond:
         #     representation = representation[:23] + representation[26:]
         if representation.endswith('+00:00'):
             representation = representation[:-6] + 'Z'
         return representation
     elif isinstance(obj, datetime.date):
         return obj.isoformat()
     elif isinstance(obj, datetime.time):
         if timezone and timezone.is_aware(obj):
             raise ValueError("JSON can't represent timezone-aware times.")
         representation = obj.isoformat()
         # NOTE: we store microseconds with full precision, technically not a valid isostring...
         # if obj.microsecond:
         #     representation = representation[:12]
         return representation
     elif isinstance(obj, decimal.Decimal):
         # Serializers will coerce decimals to strings by default.
         return float(obj)
     elif isinstance(obj, uuid.UUID):
         return str(obj)
     elif hasattr(obj, 'tolist'):
         # Numpy arrays and array scalars.
         return obj.tolist()
     elif hasattr(obj, '__getitem__'):
         try:
             return dict(obj)
         except:
             pass
     elif hasattr(obj, '__iter__'):
         return tuple(item for item in obj)
     return super(JSONEncoder, self).default(obj)