1 from django.conf import settings
2 from django.contrib.sites.models import Site
3 from django.http import Http404
5 from philo.models import Node, View
6 from philo.utils.lazycompat import SimpleLazyObject
10 """Returns a :class:`Node` instance at ``path`` (relative to the current site) or ``None``."""
12 current_site = Site.objects.get_current()
13 except Site.DoesNotExist:
16 trailing_slash = False
21 node, subpath = Node.objects.get_with_path(path, root=getattr(current_site, 'root_node', None), absolute_result=False)
22 except Node.DoesNotExist:
27 subpath = "/" + subpath
29 if trailing_slash and subpath[-1] != "/":
33 node._subpath = subpath
38 class RequestNodeMiddleware(object):
40 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`.
42 :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.
45 def process_view(self, request, view_func, view_args, view_kwargs):
47 path = view_kwargs['path']
51 request.node = SimpleLazyObject(lambda: get_node(path))
53 def process_exception(self, request, exception):
54 if settings.DEBUG or not hasattr(request, 'node') or not request.node:
57 if isinstance(exception, Http404):
58 error_view = request.node.attributes.get('Http404', None)
61 error_view = request.node.attributes.get('Http500', None)
64 if error_view is None or not isinstance(error_view, View):
65 # Should this be duck-typing? Perhaps even no testing?
68 extra_context = {'exception': exception}
69 response = error_view.render_to_response(request, extra_context)
70 response.status_code = status_code