Ticket #4720: syndication-500-2.diff

File syndication-500-2.diff, 3.4 KB (added by Andy Gayton <andy-django@…>, 17 years ago)
  • django/contrib/syndication/feeds.py

     
    5555                return attr()
    5656        return attr
    5757
    58     def get_feed(self, url=None):
     58    def get_object(self, bits):
     59        return None
     60
     61    def get_feed(self, url=""):
    5962        """
    6063        Returns a feedgenerator.DefaultFeed object, fully populated, for
    6164        this feed. Raises FeedDoesNotExist for invalid parameters.
    6265        """
    63         if url:
    64             try:
    65                 obj = self.get_object(url.split('/'))
    66             except (AttributeError, ObjectDoesNotExist):
    67                 raise FeedDoesNotExist
    68         else:
    69             obj = None
     66        try:
     67            obj = self.get_object([bit for bit in url.split('/') if bit])
     68        except (AttributeError, ObjectDoesNotExist):
     69            raise FeedDoesNotExist
    7070
    7171        if Site._meta.installed:
    7272            current_site = Site.objects.get_current()
  • tests/regressiontests/syndication/tests.py

     
     1# -*- coding: utf-8 -*-
     2
     3from django.test import TestCase
     4from django.contrib.syndication import feeds
     5from django.core.exceptions import ObjectDoesNotExist
     6from django.http import HttpRequest
     7
     8class SyndicationFeedTest(TestCase):
     9    def test_send_empty_urls_to_get_object(self):
     10        """
     11        tests that get_object gets a chance to handle empty urls
     12        """
     13        _test_log = []
     14        class FeedStub(feeds.Feed):
     15            def get_object(self, bits):
     16                _test_log.append(bits)
     17                raise ObjectDoesNotExist
     18               
     19        f = FeedStub('foo', HttpRequest())
     20        self.assertRaises(
     21            feeds.FeedDoesNotExist,
     22            f.get_feed,
     23            '')
     24        self.assertEquals(_test_log, [[]])
  • docs/syndication_feeds.txt

     
    243243      raises ``Beat.DoesNotExist`` on failure, and ``Beat.DoesNotExist`` is a
    244244      subclass of ``ObjectDoesNotExist``. Raising ``ObjectDoesNotExist`` in
    245245      ``get_object()`` tells Django to produce a 404 error for that request.
     246
     247      **New in Django development version:** Note that get_object also gets a
     248      chance to handle the ``/rss/beats/`` url.  In this case bits will be
     249      an empty list.  In our example, since ``len(bits) != 1`` an
     250      ``ObjectDoesNotExist`` exception will be raised, so ``/rss/beats/`` will
     251      generate a 404 page.  But you can handle this case however you like.  For
     252      example you could generate a combined feed for all beats.
     253
    246254    * To generate the feed's ``<title>``, ``<link>`` and ``<description>``,
    247255      Django uses the ``title()``, ``link()`` and ``description()`` methods. In
    248256      the previous example, they were simple string class attributes, but this
Back to Top