Example #1
0
    def format_date(self, data):
        """
        A hook to control how dates are formatted.

        Can be overridden at the ``Serializer`` level (``datetime_formatting``)
        or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).

        Default is ``iso-8601``, which looks like "2010-12-16".
        """
        if self.datetime_formatting == 'rfc-2822':
            return format_date(data)

        return data.isoformat()
    def format_date(self, data):
        """
        A hook to control how dates are formatted.

        Can be overridden at the ``Serializer`` level (``datetime_formatting``)
        or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).

        Default is ``iso-8601``, which looks like "2010-12-16".
        """
        if self.datetime_formatting == 'rfc-2822':
            return format_date(data)

        return data.isoformat()
Example #3
0
 def to_simple(self, data, options):
     """
     For a piece of data, attempts to recognize it and provide a simplified
     form of something complex.
     
     This brings complex Python data structures down to native types of the
     serialization format(s).
     """
     if isinstance(data, (list, tuple)):
         return [self.to_simple(item, options) for item in data]
     if isinstance(data, dict):
         return dict((key, self.to_simple(val, options)) for (key, val) in data.iteritems())
     elif isinstance(data, Bundle):
         return dict((key, self.to_simple(val, options)) for (key, val) in data.data.iteritems())
     elif isinstance(data, ApiField):
         if isinstance(data, ToOneField):
             if data.full:
                 return self.to_simple(data.fk_resource, options)
             else:
                 return self.to_simple(data.value, options)
         elif isinstance(data, ToManyField):
             if data.full:
                 return [self.to_simple(bundle, options) for bundle in data.m2m_bundles]
             else:
                 return [self.to_simple(val, options) for val in data.value]
         else:
             return self.to_simple(data.value, options)
     elif isinstance(data, datetime.datetime):
         return format_datetime(data)
     elif isinstance(data, datetime.date):
         return format_date(data)
     elif isinstance(data, datetime.time):
         return format_time(data)
     elif isinstance(data, bool):
         return data
     elif type(data) in (long, int, float):
         return data
     elif data is None:
         return None
     else:
         return force_unicode(data)
Example #4
0
 def to_simple(self, data, options):
     if type(data) in (list, tuple) or isinstance(data, RepresentationSet):
         return [self.to_simple(item, options) for item in data]
     elif isinstance(data, dict):
         return dict((key, self.to_simple(val, options)) for (key, val) in data.iteritems())
     elif isinstance(data, Representation):
         object = {}
         for field_name, field_object in data.fields.items():
             object[field_name] = self.to_simple(field_object, options)
         return object
     elif isinstance(data, ApiField):
         if isinstance(data, ToOneField):
             if data.full_repr:
                 return self.to_simple(data.fk_repr, options)
             else:
                 return self.to_simple(data.value, options)
         elif isinstance(data, ToManyField):
             if data.full_repr:
                 return [self.to_simple(repr, options) for repr in data.m2m_reprs]
             else:
                 return [self.to_simple(val, options) for val in data.value]
         else:
             return self.to_simple(data.value, options)
     elif isinstance(data, datetime.datetime):
         return format_datetime(data)
     elif isinstance(data, datetime.date):
         return format_date(data)
     elif isinstance(data, datetime.time):
         return format_time(data)
     elif isinstance(data, bool):
         return data
     elif type(data) in (long, int, float):
         return data
     elif data is None:
         return None
     else:
         return force_unicode(data)