Shunted responsibility for 404 and 500 error catching from node_view to RequestNodeMi...
authorStephen Burrows <stephen.r.burrows@gmail.com>
Tue, 2 Nov 2010 20:37:35 +0000 (16:37 -0400)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Tue, 2 Nov 2010 20:51:19 +0000 (16:51 -0400)
middleware.py
models/base.py
models/fields.py
views.py

index 8c208e4..ad660ec 100644 (file)
@@ -1,5 +1,7 @@
+from django.conf import settings
 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):
@@ -32,4 +34,20 @@ class RequestNodeMiddleware(object):
                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
index 1b6e60f..fdfe75c 100644 (file)
@@ -128,10 +128,15 @@ class ManyToManyValue(AttributeValue):
                # Value is probably a queryset - but allow any iterable.
                
                # These lines shouldn't be necessary; however, if value is an EmptyQuerySet,
-               # the code won't work without them. Unclear why...
+               # the code (specifically the object_id__in query) won't work without them. Unclear why...
                if not value:
                        value = []
                
+               # Before we can fiddle with the many-to-many to foreignkeyvalues, we need
+               # a pk.
+               if self.pk is None:
+                       self.save()
+               
                if isinstance(value, models.query.QuerySet):
                        value = value.values_list('id', flat=True)
                
index 85c5583..3e43c0f 100644 (file)
@@ -92,7 +92,7 @@ class ForeignKeyAttributeDescriptor(AttributeFieldDescriptor):
 
 class ManyToManyAttributeDescriptor(AttributeFieldDescriptor):
        def __set__(self, instance, value):
-               if isinstance(value, models.QuerySet):
+               if isinstance(value, models.query.QuerySet):
                        if self.field in instance._removed_attribute_registry:
                                instance._removed_attribute_registry.remove(self.field)
                        instance._added_attribute_registry[self.field] = value
index 7aac917..41718e5 100644 (file)
--- a/views.py
+++ b/views.py
@@ -1,10 +1,6 @@
-from django.contrib.sites.models import Site
-from django.conf import settings
-from django.http import Http404, HttpResponse
-from django.template import RequestContext
+from django.http import Http404
 from django.views.decorators.vary import vary_on_headers
 from philo.exceptions import MIDDLEWARE_NOT_CONFIGURED
-from philo.models import Node
 
 
 @vary_on_headers('Accept')
@@ -18,37 +14,6 @@ def node_view(request, path=None, **kwargs):
        node = request.node
        subpath = request.node.subpath
        
-       try:
-               if subpath and not node.accepts_subpath:
-                       raise Http404
-               return node.render_to_response(request, kwargs)
-       except Http404, e:
-               if settings.DEBUG:
-                       raise
-               
-               try:
-                       Http404View = node.attributes['Http404']
-               except KeyError:
-                       Http404View = None
-               
-               if not Http404View:
-                       raise e
-               
-               extra_context = {'exception': e}
-               
-               return Http404View.render_to_response(request, extra_context)
-       except Exception, e:
-               if settings.DEBUG:
-                       raise
-               
-               try:
-                       Http500View = node.attributes['Http500']
-                       
-                       if not Http500View:
-                               raise e
-                       
-                       extra_context = {'exception': e}
-                       
-                       return Http500View.render_to_response(request, extra_context)
-               except:
-                       raise e
\ No newline at end of file
+       if subpath and not node.accepts_subpath:
+               raise Http404
+       return node.render_to_response(request, kwargs)
\ No newline at end of file