Refactored RequestNodeMiddleware to use SimpleLazyObject - comparable to django r1629...
[philo.git] / philo / middleware.py
1 from django.conf import settings
2 from django.contrib.sites.models import Site
3 from django.http import Http404
4
5 from philo.models import Node, View
6 from philo.utils.lazycompat import SimpleLazyObject
7
8
9 def get_node(path):
10         """Returns a :class:`Node` instance at ``path`` (relative to the current site) or ``None``."""
11         try:
12                 current_site = Site.objects.get_current()
13         except Site.DoesNotExist:
14                 current_site = None
15         
16         trailing_slash = False
17         if path[-1] == '/':
18                 trailing_slash = True
19         
20         try:
21                 node, subpath = Node.objects.get_with_path(path, root=getattr(current_site, 'root_node', None), absolute_result=False)
22         except Node.DoesNotExist:
23                 return None
24         
25         if subpath is None:
26                 subpath = ""
27         subpath = "/" + subpath
28         
29         if trailing_slash and subpath[-1] != "/":
30                 subpath += "/"
31         
32         node._path = path
33         node._subpath = subpath
34         
35         return node
36
37
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):
41                 try:
42                         path = view_kwargs['path']
43                 except KeyError:
44                         request.node = None
45                 else:
46                         request.node = SimpleLazyObject(lambda: get_node(path))
47         
48         def process_exception(self, request, exception):
49                 if settings.DEBUG or not hasattr(request, 'node') or not request.node:
50                         return
51                 
52                 if isinstance(exception, Http404):
53                         error_view = request.node.attributes.get('Http404', None)
54                 else:
55                         error_view = request.node.attributes.get('Http500', None)
56                 
57                 if error_view is None or not isinstance(error_view, View):
58                         # Should this be duck-typing? Perhaps even no testing?
59                         return
60                 
61                 extra_context = {'exception': exception}
62                 return error_view.render_to_response(request, extra_context)