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):
39 """Adds a ``node`` attribute, representing the currently-viewed node, to every incoming :class:`HttpRequest` object. This is required by :func:`philo.views.node_view`."""
40 def process_view(self, request, view_func, view_args, view_kwargs):
42 path = view_kwargs['path']
46 request.node = SimpleLazyObject(lambda: get_node(path))
48 def process_exception(self, request, exception):
49 if settings.DEBUG or not hasattr(request, 'node') or not request.node:
52 if isinstance(exception, Http404):
53 error_view = request.node.attributes.get('Http404', None)
55 error_view = request.node.attributes.get('Http500', None)
57 if error_view is None or not isinstance(error_view, View):
58 # Should this be duck-typing? Perhaps even no testing?
61 extra_context = {'exception': exception}
62 return error_view.render_to_response(request, extra_context)