Ticket #4720: syndication-500-2-alt.diff

File syndication-500-2-alt.diff, 4.2 KB (added by Andy Gayton <andy-django@…>, 8 years ago)

When in rome ...

  • 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.test.client import Client
     5
     6class SyndicationFeedTest(TestCase):
     7    def test_complex_base_url(self):
     8        """
     9        tests that that the base url for a complex feed doesn't raise a 500
     10        exception
     11        """
     12        c = Client()
     13        response = c.get('/syndication/feeds/complex/')
     14        self.assertEquals(response.status_code, 404)
  • tests/regressiontests/syndication/urls.py

     
     1from django.conf.urls.defaults import patterns
     2from django.core.exceptions import ObjectDoesNotExist
     3from django.contrib.syndication import feeds
     4
     5
     6class ComplexFeed(feeds.Feed):
     7    def get_object(self, bits):
     8        if len(bits) != 1:
     9            raise ObjectDoesNotExist
     10        return None
     11
     12
     13urlpatterns = patterns('',
     14    (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {
     15        'feed_dict': dict(
     16            complex = ComplexFeed,
     17        )}),
     18)
  • 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
  • tests/urls.py

     
    1414   
    1515    # django built-in views
    1616    (r'^views/', include('regressiontests.views.urls')),
     17
     18    # test urlconf for syndication tests
     19    (r'^syndication/', include('regressiontests.syndication.urls')),
    1720)
Back to Top