From 9e738a63041c17c95812da3c366ea183cf1d83b7 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Mon, 13 Jun 2011 18:40:22 -0400 Subject: [PATCH] Added docs for the Http404/Http500 handling that philo does. Resolves issue #156. Added status codes to the responses for that handling. --- philo/middleware.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/philo/middleware.py b/philo/middleware.py index 037fdc8..f4f7e9d 100644 --- a/philo/middleware.py +++ b/philo/middleware.py @@ -36,7 +36,12 @@ def get_node(path): class RequestNodeMiddleware(object): - """Adds a ``node`` attribute, representing the currently-viewed node, to every incoming :class:`HttpRequest` object. This is required by :func:`philo.views.node_view`.""" + """ + Adds a ``node`` attribute, representing the currently-viewed :class:`.Node`, to every incoming :class:`HttpRequest` object. This is required by :func:`philo.views.node_view`. + + :class:`RequestNodeMiddleware` also catches all exceptions raised while handling requests that have attached :class:`.Node`\ s if :setting:`settings.DEBUG` is ``True``. If a :exc:`django.http.Http404` error was caught, :class:`RequestNodeMiddleware` will look for an "Http404" :class:`.Attribute` on the request's :class:`.Node`; otherwise it will look for an "Http500" :class:`.Attribute`. If an appropriate :class:`.Attribute` is found, and the value of the attribute is a :class:`.View` instance, then the :class:`.View` will be rendered with the exception in the ``extra_context``, bypassing any later handling of exceptions. + + """ def process_view(self, request, view_func, view_args, view_kwargs): try: path = view_kwargs['path'] @@ -51,12 +56,16 @@ class RequestNodeMiddleware(object): if isinstance(exception, Http404): error_view = request.node.attributes.get('Http404', None) + status_code = 404 else: error_view = request.node.attributes.get('Http500', None) + status_code = 500 if error_view is None or not isinstance(error_view, View): # Should this be duck-typing? Perhaps even no testing? return extra_context = {'exception': exception} - return error_view.render_to_response(request, extra_context) \ No newline at end of file + response = error_view.render_to_response(request, extra_context) + response.status_code = status_code + return response \ No newline at end of file -- 2.20.1