def _encode(self, data, response=None, http_headers=None, resource=None, result=None, **kwargs): from pyston.resource import BaseObjectResource http_headers = {} if http_headers is None else http_headers.copy() converter = self._get_converter(resource) http_headers = self._update_headers(http_headers, resource, converter) obj = ( resource._get_obj_or_none() if isinstance(resource, BaseObjectResource) and resource.has_permission() else None ) kwargs.update({ 'http_headers': http_headers, 'resource': resource, }) data_stream = UniversalBytesIO() converter._encode_to_stream(data_stream, self._convert_url_to_links(serialized_data_to_python(data)), **kwargs) context = kwargs.copy() context.update({ 'permissions': self._get_permissions(resource, obj), 'forms': self._get_forms(resource, obj), 'output': data_stream.get_string_value(), 'name': resource._get_name() if resource and resource.has_permission() else response.status_code }) # All responses has set 200 response code, because response can return status code without content (204) and # browser doesn't render it response.status_code = 200 return get_template(self.template_name).render(context, request=resource.request if resource else None)
def _encode(self, data, response=None, http_headers=None, resource=None, result=None, **kwargs): from pyston.resource import BaseObjectResource http_headers = {} if http_headers is None else http_headers.copy() converter = self._get_converter(resource) http_headers = self._update_headers(http_headers, resource, converter) obj = (resource._get_obj_or_none() if isinstance(resource, BaseObjectResource) and resource.has_permission() else None) kwargs.update({ 'http_headers': http_headers, 'resource': resource, }) data_stream = UniversalBytesIO() converter._encode_to_stream( data_stream, self._convert_url_to_links(serialized_data_to_python(data)), **kwargs) context = kwargs.copy() context.update({ 'permissions': self._get_permissions(resource, obj), 'forms': self._get_forms(resource, obj), 'output': data_stream.get_string_value(), 'name': resource._get_name() if resource and resource.has_permission() else response.status_code }) # All responses has set 200 response code, because response can return status code without content (204) and # browser doesn't render it response.status_code = 200 return get_template(self.template_name).render( context, request=resource.request if resource else None)
def _encode(self, data, response=None, http_headers=None, resource=None, result=None, **kwargs): assert resource is not None, 'HTML converter requires resource and cannot be used as a direct serializer' http_headers = {} if http_headers is None else http_headers.copy() converter = self._get_converter(resource) http_headers = self._update_headers(http_headers, resource, converter) obj = resource._get_obj_or_none() kwargs.update({ 'http_headers': http_headers, 'resource': resource, }) data_stream = UniversalBytesIO() converter._encode_to_stream( data_stream, self._convert_url_to_links(serialized_data_to_python(data)), **kwargs) context = kwargs.copy() context.update({ 'permissions': self._get_permissions(resource, obj), 'forms': self._get_forms(resource, obj), 'output': data_stream.get_string_value(), }) # All responses has set 200 response code, because response can return status code without content (204) and # browser doesn't render it response.status_code = 200 return get_template(self.template_name).render( context, request=resource.request)
def _get_output_stream(self, os): return os if isinstance(os, UniversalBytesIO) else UniversalBytesIO(os)
def _get_output_stream(self, output_stream): return output_stream if isinstance( output_stream, UniversalBytesIO) else UniversalBytesIO(output_stream)