def test_get_factory(self): from pyramid.registry import Registry from pyramid.response import Response, _get_response_factory registry = Registry() response = _get_response_factory(registry)(None) self.assertTrue(isinstance(response, Response))
def _make_response(self, result, request): # broken out of render_to_response as a separate method for testing # purposes response = getattr(request, 'response', None) if response is None: # request is None or request is not a pyramid.response.Response registry = self.registry response_factory = _get_response_factory(registry) response = response_factory(request) if result is not None: if isinstance(result, text_type): if response.charset is None: warnings.warn( "Renderer returned a result of type {0}, " "however the response Content-Type <{1}> does not " "have a charset. Implicitly encoding the result as " "UTF-8.".format(type(result), response.content_type), RuntimeWarning ) response.body = result.encode('UTF-8') else: response.text = result elif isinstance(result, bytes): response.body = result elif hasattr(result, '__iter__'): response.app_iter = result else: response.body = result return response
def response(self): """This attribute is actually a "reified" property which returns an instance of the :class:`pyramid.response.Response`. class. The response object returned does not exist until this attribute is accessed. Subsequent accesses will return the same Response object. The ``request.response`` API is used by renderers. A render obtains the response object it will return from a view that uses that renderer by accessing ``request.response``. Therefore, it's possible to use the ``request.response`` API to set up a response object with "the right" attributes (e.g. by calling ``request.response.set_cookie()``) within a view that uses a renderer. Mutations to this response object will be preserved in the response sent to the client.""" response_factory = _get_response_factory(self.registry) return response_factory(self)
def _make_response(self, result, request): # broken out of render_to_response as a separate method for testing # purposes response = getattr(request, 'response', None) if response is None: # request is None or request is not a pyramid.response.Response registry = self.registry response_factory = _get_response_factory(registry) response = response_factory(request) if result is not None: if isinstance(result, text_type): response.text = result elif isinstance(result, bytes): response.body = result elif hasattr(result, '__iter__'): response.app_iter = result else: response.body = result return response
def response(self): f = _get_response_factory(self.registry) return f(self)