Skip to content

Commit

Permalink
sketch out a subrequest API
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdonc committed Sep 16, 2012
1 parent fa4672f commit dd9859f
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions pyramid/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self, registry):
tweens = q(ITweens)
if tweens is None:
tweens = excview_tween_factory
self.orig_handle_request = self.handle_request
self.handle_request = tweens(self.handle_request, registry)
self.root_policy = self.root_factory # b/w compat
self.registry = registry
Expand Down Expand Up @@ -161,39 +162,52 @@ def handle_request(self, request):

return response

def __call__(self, environ, start_response):
"""
Accept ``environ`` and ``start_response``; create a
:term:`request` and route the request to a :app:`Pyramid`
view based on introspection of :term:`view configuration`
within the application registry; call ``start_response`` and
return an iterable.
"""
def subrequest(self, request, use_tweens=False):
registry = self.registry
has_listeners = self.registry.has_listeners
notify = self.registry.notify
request = self.request_factory(environ)
threadlocals = {'registry':registry, 'request':request}
manager = self.threadlocal_manager
manager.push(threadlocals)
request.registry = registry
request.subrequest = self.subrequest
if use_tweens:
handle_request = self.handle_request
else:
handle_request = self.orig_handle_request
try:

try:
extensions = self.request_extensions
if extensions is not None:
request._set_extensions(extensions)
response = self.handle_request(request)
response = handle_request(request)
has_listeners and notify(NewResponse(request, response))

if request.response_callbacks:
request._process_response_callbacks(response)

return response(request.environ, start_response)
# XXX before subrequest factoring, the below line
# actually invoked Response.__call__, passing it
# the start_response
return response

finally:
if request.finished_callbacks:
request._process_finished_callbacks()

finally:
manager.pop()

def __call__(self, environ, start_response):
"""
Accept ``environ`` and ``start_response``; create a
:term:`request` and route the request to a :app:`Pyramid`
view based on introspection of :term:`view configuration`
within the application registry; call ``start_response`` and
return an iterable.
"""
request = self.request_factory(environ)
response = self.subrequest(request, use_tweens=True)
return response(request.environ, start_response)

0 comments on commit dd9859f

Please sign in to comment.