Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose response class #1499

Merged
merged 11 commits into from
Jan 2, 2015
7 changes: 3 additions & 4 deletions pyramid/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pyramid.interfaces import (
IJSONAdapter,
IRendererFactory,
IResponseFactory,
IRendererInfo,
)

Expand All @@ -19,6 +18,8 @@
text_type,
)

from pyramid.util import _get_response_factory

from pyramid.decorator import reify

from pyramid.events import BeforeRender
Expand Down Expand Up @@ -448,9 +449,7 @@ def _make_response(self, result, request):
if response is None:
# request is None or request is not a pyramid.response.Response
registry = self.registry
response_factory = registry.queryUtility(IResponseFactory,
default=Response)

response_factory = _get_response_factory(registry, request)
response = response_factory()

if result is not None:
Expand Down
10 changes: 5 additions & 5 deletions pyramid/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
IRequest,
IResponse,
ISessionFactory,
IResponseFactory,
)

from pyramid.compat import (
Expand All @@ -27,7 +26,10 @@
AuthorizationAPIMixin,
)
from pyramid.url import URLMethodsMixin
from pyramid.util import InstancePropertyMixin
from pyramid.util import (
InstancePropertyMixin,
_get_response_factory,
)

class TemplateContext(object):
pass
Expand Down Expand Up @@ -214,9 +216,7 @@ def response(self):
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."""
registry = self.registry
response_factory = registry.queryUtility(IResponseFactory,
default=Response)
response_factory = _get_response_factory(self.registry, self)
return response_factory()

def is_response(self, ob):
Expand Down
30 changes: 30 additions & 0 deletions pyramid/tests/test_config/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,36 @@ def test_setup_registry_request_factory(self):
utility = reg.getUtility(IRequestFactory)
self.assertEqual(utility, factory)

def test_setup_registry_request_factory_custom_response_class(self):
from pyramid.registry import Registry
from pyramid.interfaces import IRequestFactory
from pyramid.request import Request

class MyResponse(object):
pass

class MyRequest(Request):
ResponseClass = MyResponse

reg = Registry()
config = self._makeOne(reg)
factory = MyRequest({
'PATH_INFO': '/',
'wsgi.url_scheme': 'http',
'HTTP_HOST': 'localhost',
'SERVER_PROTOCOL': '1.0',
})
factory.registry = reg

config.setup_registry(request_factory=factory)
self.assertEqual(reg.queryUtility(IRequestFactory), None)
config.commit()
utility = reg.getUtility(IRequestFactory)
self.assertEqual(utility, factory)

new_response = factory.response
self.assertTrue(isinstance(new_response, MyResponse))

def test_setup_registry_request_factory_dottedname(self):
from pyramid.registry import Registry
from pyramid.interfaces import IRequestFactory
Expand Down
27 changes: 27 additions & 0 deletions pyramid/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,33 @@ def test___str__(self):
"Line 0 of file filename:\n linerepr ")


class TestGetResponseFactory(unittest.TestCase):
def test_no_request(self):
from pyramid.util import _get_response_factory
from pyramid.registry import Registry
from pyramid.response import Response

registry = Registry()
factory = _get_response_factory(registry)
self.assertEqual(factory, Response)

def test_with_request(self):
from pyramid.util import _get_response_factory
from pyramid.registry import Registry
from pyramid.request import Request

class MyResponse(object):
pass

class MyRequest(Request):
ResponseClass = MyResponse
registry = Registry()

request = MyRequest({})
factory = _get_response_factory(registry, request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registry undefined, breaks Travis.

self.assertEqual(factory, MyResponse)


def dummyfunc(): pass

class Dummy(object):
Expand Down
21 changes: 21 additions & 0 deletions pyramid/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
CyclicDependencyError,
)

from pyramid.interfaces import (
IResponseFactory,
)

from pyramid.compat import (
iteritems_,
is_nonstr_iter,
Expand All @@ -25,6 +29,7 @@
)

from pyramid.interfaces import IActionInfo
from pyramid.response import Response
from pyramid.path import DottedNameResolver as _DottedNameResolver

class DottedNameResolver(_DottedNameResolver):
Expand Down Expand Up @@ -551,3 +556,19 @@ def wrapper(self, *arg, **kw):
wrapper.__docobj__ = wrapped
return wrapper

def _get_response_factory(registry, request=None):
""" Obtain a :class: `pyramid.response.Response` using the
``request.ResponseClass`` property if available.
"""
# Request is `None` or does not have a `ResponseClass`
if hasattr(request, 'ResponseClass'):
response_class = request.ResponseClass
else:
response_class = Response

response_factory = registry.queryUtility(
IResponseFactory,
default=response_class
)

return response_factory