Minor correction to LazyNode's use of subpath to avoid NameErrors.
[philo.git] / middleware.py
index 8c208e4..fa30ef6 100644 (file)
@@ -1,5 +1,7 @@
+from django.conf import settings
 from django.contrib.sites.models import Site
 from django.contrib.sites.models import Site
-from philo.models import Node
+from django.http import Http404
+from philo.models import Node, View
 
 
 class LazyNode(object):
 
 
 class LazyNode(object):
@@ -19,6 +21,9 @@ class LazyNode(object):
                                node = None
                        
                        if node:
                                node = None
                        
                        if node:
+                               if subpath is None:
+                                       subpath = ""
+                               subpath = "/" + subpath
                                node.subpath = subpath
                        
                        request._found_node = node
                                node.subpath = subpath
                        
                        request._found_node = node
@@ -32,4 +37,20 @@ class RequestNodeMiddleware(object):
                request.__class__.node = LazyNode()
        
        def process_view(self, request, view_func, view_args, view_kwargs):
                request.__class__.node = LazyNode()
        
        def process_view(self, request, view_func, view_args, view_kwargs):
-               request._cached_node_path = view_kwargs.get('path', '/')
\ No newline at end of file
+               request._cached_node_path = view_kwargs.get('path', '/')
+       
+       def process_exception(self, request, exception):
+               if settings.DEBUG or not hasattr(request, 'node') or not request.node:
+                       return
+               
+               if isinstance(exception, Http404):
+                       error_view = request.node.attributes.get('Http404', None)
+               else:
+                       error_view = request.node.attributes.get('Http500', None)
+               
+               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