Code

Ticket #4720: syndication-500-2.diff

File syndication-500-2.diff, 3.4 KB (added by Andy Gayton <andy-django@…>, 7 years ago)
Line 
1Index: django/contrib/syndication/feeds.py
2===================================================================
3--- django/contrib/syndication/feeds.py (revision 6837)
4+++ django/contrib/syndication/feeds.py (working copy)
5@@ -55,18 +55,18 @@
6                 return attr()
7         return attr
8 
9-    def get_feed(self, url=None):
10+    def get_object(self, bits):
11+        return None
12+
13+    def get_feed(self, url=""):
14         """
15         Returns a feedgenerator.DefaultFeed object, fully populated, for
16         this feed. Raises FeedDoesNotExist for invalid parameters.
17         """
18-        if url:
19-            try:
20-                obj = self.get_object(url.split('/'))
21-            except (AttributeError, ObjectDoesNotExist):
22-                raise FeedDoesNotExist
23-        else:
24-            obj = None
25+        try:
26+            obj = self.get_object([bit for bit in url.split('/') if bit])
27+        except (AttributeError, ObjectDoesNotExist):
28+            raise FeedDoesNotExist
29 
30         if Site._meta.installed:
31             current_site = Site.objects.get_current()
32Index: tests/regressiontests/syndication/__init__.py
33===================================================================
34Index: tests/regressiontests/syndication/tests.py
35===================================================================
36--- tests/regressiontests/syndication/tests.py  (revision 0)
37+++ tests/regressiontests/syndication/tests.py  (revision 0)
38@@ -0,0 +1,24 @@
39+# -*- coding: utf-8 -*-
40+
41+from django.test import TestCase
42+from django.contrib.syndication import feeds
43+from django.core.exceptions import ObjectDoesNotExist
44+from django.http import HttpRequest
45+
46+class SyndicationFeedTest(TestCase):
47+    def test_send_empty_urls_to_get_object(self):
48+        """
49+        tests that get_object gets a chance to handle empty urls
50+        """
51+        _test_log = []
52+        class FeedStub(feeds.Feed):
53+            def get_object(self, bits):
54+                _test_log.append(bits)
55+                raise ObjectDoesNotExist
56+               
57+        f = FeedStub('foo', HttpRequest())
58+        self.assertRaises(
59+            feeds.FeedDoesNotExist,
60+            f.get_feed,
61+            '')
62+        self.assertEquals(_test_log, [[]])
63Index: tests/regressiontests/syndication/models.py
64===================================================================
65Index: docs/syndication_feeds.txt
66===================================================================
67--- docs/syndication_feeds.txt  (revision 6837)
68+++ docs/syndication_feeds.txt  (working copy)
69@@ -243,6 +243,14 @@
70       raises ``Beat.DoesNotExist`` on failure, and ``Beat.DoesNotExist`` is a
71       subclass of ``ObjectDoesNotExist``. Raising ``ObjectDoesNotExist`` in
72       ``get_object()`` tells Django to produce a 404 error for that request.
73+
74+      **New in Django development version:** Note that get_object also gets a
75+      chance to handle the ``/rss/beats/`` url.  In this case bits will be
76+      an empty list.  In our example, since ``len(bits) != 1`` an
77+      ``ObjectDoesNotExist`` exception will be raised, so ``/rss/beats/`` will
78+      generate a 404 page.  But you can handle this case however you like.  For
79+      example you could generate a combined feed for all beats.
80+
81     * To generate the feed's ``<title>``, ``<link>`` and ``<description>``,
82       Django uses the ``title()``, ``link()`` and ``description()`` methods. In
83       the previous example, they were simple string class attributes, but this