PageRenderTime 543ms CodeModel.GetById 87ms app.highlight 201ms RepoModel.GetById 111ms app.codeStats 27ms

/tests/regressiontests/syndication/feeds.py

https://code.google.com/p/mango-py/
Python | 142 lines | 83 code | 39 blank | 20 comment | 3 complexity | 7a48d79dd34029680ad79c2f2216fd29 MD5 | raw file
  1from django.contrib.syndication import feeds, views
  2from django.core.exceptions import ObjectDoesNotExist
  3from django.utils import feedgenerator, tzinfo
  4from models import Article, Entry
  5
  6
  7class ComplexFeed(views.Feed):
  8    def get_object(self, request, foo=None):
  9        if foo is not None:
 10            raise ObjectDoesNotExist
 11        return None
 12
 13
 14class TestRss2Feed(views.Feed):
 15    title = 'My blog'
 16    description = 'A more thorough description of my blog.'
 17    link = '/blog/'
 18    feed_guid = '/foo/bar/1234'
 19    author_name = 'Sally Smith'
 20    author_email = 'test@example.com'
 21    author_link = 'http://www.example.com/'
 22    categories = ('python', 'django')
 23    feed_copyright = 'Copyright (c) 2007, Sally Smith'
 24    ttl = 600
 25
 26    def items(self):
 27        return Entry.objects.all()
 28
 29    def item_description(self, item):
 30        return "Overridden description: %s" % item
 31
 32    def item_pubdate(self, item):
 33        return item.date
 34
 35    item_author_name = 'Sally Smith'
 36    item_author_email = 'test@example.com'
 37    item_author_link = 'http://www.example.com/'
 38    item_categories = ('python', 'testing')
 39    item_copyright = 'Copyright (c) 2007, Sally Smith'
 40
 41
 42class TestRss091Feed(TestRss2Feed):
 43    feed_type = feedgenerator.RssUserland091Feed
 44
 45
 46class TestAtomFeed(TestRss2Feed):
 47    feed_type = feedgenerator.Atom1Feed
 48    subtitle = TestRss2Feed.description
 49
 50
 51class ArticlesFeed(TestRss2Feed):
 52    """
 53    A feed to test no link being defined. Articles have no get_absolute_url()
 54    method, and item_link() is not defined.
 55    """
 56    def items(self):
 57        return Article.objects.all()
 58
 59
 60class TestEnclosureFeed(TestRss2Feed):
 61    pass
 62
 63
 64class TemplateFeed(TestRss2Feed):
 65    """
 66    A feed to test defining item titles and descriptions with templates.
 67    """
 68    title_template = 'syndication/title.html'
 69    description_template = 'syndication/description.html'
 70
 71    # Defining a template overrides any item_title definition
 72    def item_title(self):
 73        return "Not in a template"
 74
 75
 76class NaiveDatesFeed(TestAtomFeed):
 77    """
 78    A feed with naive (non-timezone-aware) dates.
 79    """
 80    def item_pubdate(self, item):
 81        return item.date
 82
 83
 84class TZAwareDatesFeed(TestAtomFeed):
 85    """
 86    A feed with timezone-aware dates.
 87    """
 88    def item_pubdate(self, item):
 89        # Provide a weird offset so that the test can know it's getting this
 90        # specific offset and not accidentally getting on from
 91        # settings.TIME_ZONE.
 92        return item.date.replace(tzinfo=tzinfo.FixedOffset(42))
 93
 94
 95class TestFeedUrlFeed(TestAtomFeed):
 96    feed_url = 'http://example.com/customfeedurl/'
 97
 98
 99class MyCustomAtom1Feed(feedgenerator.Atom1Feed):
100    """
101    Test of a custom feed generator class.
102    """
103    def root_attributes(self):
104        attrs = super(MyCustomAtom1Feed, self).root_attributes()
105        attrs[u'django'] = u'rocks'
106        return attrs
107
108    def add_root_elements(self, handler):
109        super(MyCustomAtom1Feed, self).add_root_elements(handler)
110        handler.addQuickElement(u'spam', u'eggs')
111
112    def item_attributes(self, item):
113        attrs = super(MyCustomAtom1Feed, self).item_attributes(item)
114        attrs[u'bacon'] = u'yum'
115        return attrs
116
117    def add_item_elements(self, handler, item):
118        super(MyCustomAtom1Feed, self).add_item_elements(handler, item)
119        handler.addQuickElement(u'ministry', u'silly walks')
120
121
122class TestCustomFeed(TestAtomFeed):
123    feed_type = MyCustomAtom1Feed
124
125
126class DeprecatedComplexFeed(feeds.Feed):
127    def get_object(self, bits):
128        if len(bits) != 1:
129            raise ObjectDoesNotExist
130        return None
131
132
133class DeprecatedRssFeed(feeds.Feed):
134    link = "/blog/"
135    title = 'My blog'
136
137    def items(self):
138        return Entry.objects.all()
139
140    def item_link(self, item):
141        return "/blog/%s/" % item.pk
142