def _encode_data(self, data, format=None, content_type=None): """ Encode the data returning a two tuple of (bytes, content_type) """ if data is None: return ('', content_type) assert format is None or content_type is None, ( 'You may not set both `format` and `content_type`.' ) if content_type: # Content type specified explicitly, treat data as a raw bytestring ret = force_bytes_or_smart_bytes(data, settings.DEFAULT_CHARSET) else: format = format or self.default_format assert format in self.renderer_classes, ( "Invalid format '{0}'. Available formats are {1}. " "Set TEST_REQUEST_RENDERER_CLASSES to enable " "extra request formats.".format( format, ', '.join( ["'" + fmt + "'" for fmt in self.renderer_classes.keys()]) ) ) # Use format and render the data into a bytestring renderer = self.renderer_classes[format]() ret = renderer.render(data) # Determine the content-type header from the renderer content_type = "{0}; charset={1}".format( renderer.media_type, renderer.charset ) # Coerce text to bytes if required. if isinstance(ret, six.text_type): ret = bytes(ret.encode(renderer.charset)) return ret, content_type
def _encode_data(self, data, format=None, content_type=None): """ Encode the data returning a two tuple of (bytes, content_type) """ if data is None: return ('', content_type) assert format is None or content_type is None, ( 'You may not set both `format` and `content_type`.' ) if content_type: # Content type specified explicitly, treat data as a raw bytestring ret = force_bytes_or_smart_bytes(data, settings.DEFAULT_CHARSET) else: format = format or self.default_format assert format in self.renderer_classes, ( "Invalid format '{0}'. Available formats are {1}. " "Set TEST_REQUEST_RENDERER_CLASSES to enable " "extra request formats.".format( format, ', '.join(["'" + fmt + "'" for fmt in self.renderer_classes.keys()]) ) ) # Use format and render the data into a bytestring renderer = self.renderer_classes[format]() ret = renderer.render(data) # Determine the content-type header from the renderer content_type = "{0}; charset={1}".format( renderer.media_type, renderer.charset ) # Coerce text to bytes if required. if isinstance(ret, six.text_type): ret = bytes(ret.encode(renderer.charset)) return ret, content_type
def get_view_description(view_cls, html=False): """ Alternative view description function to be used as the DRF ``VIEW_DESCRIPTION_FUNCTION`` so that RestructuredText can be used instead of the DRF-default MarkDown. Except for the RST parts, derived by [email protected] from the DRF default get_view_description function. """ description = view_cls.__doc__ or '' description = formatting.dedent(force_bytes_or_smart_bytes(description)) if html: # from https://wiki.python.org/moin/ReStructuredText -- we use the # third recipe to get just the HTML parts corresponding to the ReST # docstring: parts = core.publish_parts(source=description, writer_name='html') html = parts['body_pre_docinfo']+parts['fragment'] # have to use mark_safe so our HTML will get explicitly marked as # safe and will be correctly rendered return mark_safe(html) return description
def get_view_description(view_cls, html=False): """ Alternative view description function to be used as the DRF ``VIEW_DESCRIPTION_FUNCTION`` so that RestructuredText can be used instead of the DRF-default MarkDown. Except for the RST parts, derived by [email protected] from the DRF default get_view_description function. """ description = view_cls.__doc__ or '' description = formatting.dedent(force_bytes_or_smart_bytes(description)) if html: # from https://wiki.python.org/moin/ReStructuredText -- we use the # third recipe to get just the HTML parts corresponding to the ReST # docstring: parts = core.publish_parts(source=description, writer_name='html') html = parts['body_pre_docinfo'] + parts['fragment'] # have to use mark_safe so our HTML will get explicitly marked as # safe and will be correctly rendered return mark_safe(html) return description