Corrected FeedView handling of incorrect Accept headers. Will now actually return...
authorStephen Burrows <stephen.r.burrows@gmail.com>
Wed, 23 Feb 2011 16:50:50 +0000 (11:50 -0500)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Wed, 23 Feb 2011 16:50:50 +0000 (11:50 -0500)
contrib/penfield/exceptions.py [new file with mode: 0644]
contrib/penfield/middleware.py [new file with mode: 0644]
contrib/penfield/models.py

diff --git a/contrib/penfield/exceptions.py b/contrib/penfield/exceptions.py
new file mode 100644 (file)
index 0000000..96b96ed
--- /dev/null
@@ -0,0 +1,3 @@
+class HttpNotAcceptable(Exception):
+       """This will be raised if an Http-Accept header will not accept the feed content types that are available."""
+       pass
\ No newline at end of file
diff --git a/contrib/penfield/middleware.py b/contrib/penfield/middleware.py
new file mode 100644 (file)
index 0000000..b25a28b
--- /dev/null
@@ -0,0 +1,14 @@
+from django.http import HttpResponse
+from django.utils.decorators import decorator_from_middleware
+from philo.contrib.penfield.exceptions import HttpNotAcceptable
+
+
+class HttpNotAcceptableMiddleware(object):
+       """Middleware to catch HttpNotAcceptable errors and return an Http406 response.
+       See RFC 2616."""
+       def process_exception(self, request, exception):
+               if isinstance(exception, HttpNotAcceptable):
+                       return HttpResponse(status=406)
+
+
+http_not_acceptable = decorator_from_middleware(HttpNotAcceptableMiddleware)
\ No newline at end of file
index 98dcdd5..b970643 100644 (file)
@@ -10,6 +10,8 @@ from django.utils.datastructures import SortedDict
 from django.utils.encoding import smart_unicode, force_unicode
 from django.utils.html import escape
 from datetime import date, datetime
+from philo.contrib.penfield.exceptions import HttpNotAcceptable
+from philo.contrib.penfield.middleware import http_not_acceptable
 from philo.contrib.penfield.validators import validate_pagination_count
 from philo.exceptions import ViewCanNotProvideSubpath
 from philo.models import Tag, Titled, Entity, MultiView, Page, register_value_model, TemplateField, Template
@@ -62,7 +64,7 @@ class FeedView(MultiView):
                urlpatterns = patterns('')
                if self.feeds_enabled:
                        feed_reverse_name = "%s_feed" % reverse_name
-                       feed_view = self.feed_view(get_items_attr, feed_reverse_name)
+                       feed_view = http_not_acceptable(self.feed_view(get_items_attr, feed_reverse_name))
                        feed_pattern = r'%s%s%s$' % (base, (base and base[-1] != "^") and "/" or "", self.feed_suffix)
                        urlpatterns += patterns('',
                                url(feed_pattern, feed_view, name=feed_reverse_name),
@@ -139,8 +141,7 @@ class FeedView(MultiView):
                                else:
                                        feed_type = None
                        if not feed_type:
-                               # See RFC 2616
-                               return HttpResponse(status=406)
+                               raise HttpNotAcceptable
                return FEEDS[feed_type]
        
        def get_feed(self, obj, request, reverse_name):