Пример #1
0
    def dispatch_request(self, request, environ):
        """Dispatch the request.

        This method tries to find the proper controller and view for the
        request and does the request/response middleware wrapping.
        """
        try:
            # Test if we are on the correct base domain and do redirect
            # if we're using something like `localhost` instead.
            url_adapter = self.get_url_adapter(request.environ)
        except ValueError:
            # we cannot use make_full_domain() here because the url adapter
            # is used there too.  So we raise a new `ValueError` here too.
            # Thats why we do it the manual way
            return redirect('http://%s/' % self.ctx.cfg['base_domain_name'])

        for middleware in IMiddleware.iter_middlewares():
            response = middleware.process_request(request)

            if response is not None:
                return response

        # dispatch the request if not already done by some middleware
        request = _lookup_object('request')
        try:
            if request.routing_exception is not None:
                raise request.routing_exception

            try:
                view = self.get_view(request.endpoint)
                response = view(request, **request.view_args)
                if response is None:
                    raise ValueError('View function did not return a response')
            except db.NoResultFound:
                raise NotFound()

        except NotFound as err:
            if self.ctx.cfg['debug']:
                return notfound(request, url_adapter)
            response = err.get_response(request.environ)

        except HTTPException as err:
            response = err.get_response(request.environ)

        response = self.make_response(request, response)

        # Let middlewares process the response.  Note that we *only*
        # process the response object, if it's returned from some view.
        # If an request modifing middleware is in-place we never reach
        # this code block.
        for middleware in reversed(IMiddleware.iter_middlewares()):
            response = middleware.process_response(request, response)

        return response
Пример #2
0
 def test_lookup_object(self):
     # we're outside of a request context
     self.assertRaises(RuntimeError, _lookup_object, 'request')
     # get into request context
     with self.get_new_request():
         self.assertTrue(_lookup_object('request') is not None)
Пример #3
0
 def test_lookup_object(self):
     # we're outside of a request context
     self.assertRaises(RuntimeError, _lookup_object, 'request')
     # get into request context
     with self.get_new_request():
         self.assertTrue(_lookup_object('request') is not None)