2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2011-04-02 16:44:47 +08:00
|
|
|
from django.contrib.syndication import views
|
2008-08-12 04:49:19 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2010-01-28 21:46:18 +08:00
|
|
|
from django.utils import feedgenerator, tzinfo
|
2011-10-14 05:34:56 +08:00
|
|
|
|
|
|
|
from .models import Article, Entry
|
2008-08-12 04:49:19 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
class ComplexFeed(views.Feed):
|
|
|
|
def get_object(self, request, foo=None):
|
|
|
|
if foo is not None:
|
2008-08-12 04:49:19 +08:00
|
|
|
raise ObjectDoesNotExist
|
|
|
|
return None
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
class TestRss2Feed(views.Feed):
|
2008-08-12 04:49:19 +08:00
|
|
|
title = 'My blog'
|
2010-01-28 21:46:18 +08:00
|
|
|
description = 'A more thorough description of my blog.'
|
|
|
|
link = '/blog/'
|
|
|
|
feed_guid = '/foo/bar/1234'
|
|
|
|
author_name = 'Sally Smith'
|
|
|
|
author_email = 'test@example.com'
|
|
|
|
author_link = 'http://www.example.com/'
|
|
|
|
categories = ('python', 'django')
|
|
|
|
feed_copyright = 'Copyright (c) 2007, Sally Smith'
|
|
|
|
ttl = 600
|
|
|
|
|
2008-08-12 04:49:19 +08:00
|
|
|
def items(self):
|
|
|
|
return Entry.objects.all()
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def item_description(self, item):
|
|
|
|
return "Overridden description: %s" % item
|
|
|
|
|
|
|
|
def item_pubdate(self, item):
|
|
|
|
return item.date
|
|
|
|
|
|
|
|
item_author_name = 'Sally Smith'
|
|
|
|
item_author_email = 'test@example.com'
|
|
|
|
item_author_link = 'http://www.example.com/'
|
|
|
|
item_categories = ('python', 'testing')
|
|
|
|
item_copyright = 'Copyright (c) 2007, Sally Smith'
|
|
|
|
|
|
|
|
|
2013-02-06 18:25:35 +08:00
|
|
|
class TestRss2FeedWithGuidIsPermaLinkTrue(TestRss2Feed):
|
|
|
|
def item_guid_is_permalink(self, item):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class TestRss2FeedWithGuidIsPermaLinkFalse(TestRss2Feed):
|
|
|
|
def item_guid(self, item):
|
|
|
|
return str(item.pk)
|
|
|
|
|
|
|
|
def item_guid_is_permalink(self, item):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class TestRss091Feed(TestRss2Feed):
|
|
|
|
feed_type = feedgenerator.RssUserland091Feed
|
|
|
|
|
|
|
|
|
2012-10-01 02:44:27 +08:00
|
|
|
class TestNoPubdateFeed(views.Feed):
|
|
|
|
title = 'Test feed'
|
|
|
|
link = '/feed/'
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return Entry.objects.all()
|
|
|
|
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class TestAtomFeed(TestRss2Feed):
|
|
|
|
feed_type = feedgenerator.Atom1Feed
|
|
|
|
subtitle = TestRss2Feed.description
|
|
|
|
|
|
|
|
|
|
|
|
class ArticlesFeed(TestRss2Feed):
|
|
|
|
"""
|
|
|
|
A feed to test no link being defined. Articles have no get_absolute_url()
|
|
|
|
method, and item_link() is not defined.
|
|
|
|
"""
|
|
|
|
def items(self):
|
|
|
|
return Article.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class TestEnclosureFeed(TestRss2Feed):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateFeed(TestRss2Feed):
|
|
|
|
"""
|
|
|
|
A feed to test defining item titles and descriptions with templates.
|
|
|
|
"""
|
|
|
|
title_template = 'syndication/title.html'
|
|
|
|
description_template = 'syndication/description.html'
|
|
|
|
|
|
|
|
# Defining a template overrides any item_title definition
|
|
|
|
def item_title(self):
|
|
|
|
return "Not in a template"
|
|
|
|
|
|
|
|
|
|
|
|
class NaiveDatesFeed(TestAtomFeed):
|
|
|
|
"""
|
|
|
|
A feed with naive (non-timezone-aware) dates.
|
|
|
|
"""
|
|
|
|
def item_pubdate(self, item):
|
|
|
|
return item.date
|
|
|
|
|
|
|
|
|
|
|
|
class TZAwareDatesFeed(TestAtomFeed):
|
|
|
|
"""
|
|
|
|
A feed with timezone-aware dates.
|
|
|
|
"""
|
|
|
|
def item_pubdate(self, item):
|
|
|
|
# Provide a weird offset so that the test can know it's getting this
|
|
|
|
# specific offset and not accidentally getting on from
|
|
|
|
# settings.TIME_ZONE.
|
|
|
|
return item.date.replace(tzinfo=tzinfo.FixedOffset(42))
|
|
|
|
|
|
|
|
|
|
|
|
class TestFeedUrlFeed(TestAtomFeed):
|
|
|
|
feed_url = 'http://example.com/customfeedurl/'
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class MyCustomAtom1Feed(feedgenerator.Atom1Feed):
|
2008-08-12 06:22:26 +08:00
|
|
|
"""
|
|
|
|
Test of a custom feed generator class.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
2008-08-12 06:22:26 +08:00
|
|
|
def root_attributes(self):
|
|
|
|
attrs = super(MyCustomAtom1Feed, self).root_attributes()
|
2012-06-08 00:08:47 +08:00
|
|
|
attrs['django'] = 'rocks'
|
2008-08-12 06:22:26 +08:00
|
|
|
return attrs
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_root_elements(self, handler):
|
|
|
|
super(MyCustomAtom1Feed, self).add_root_elements(handler)
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement('spam', 'eggs')
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def item_attributes(self, item):
|
|
|
|
attrs = super(MyCustomAtom1Feed, self).item_attributes(item)
|
2012-06-08 00:08:47 +08:00
|
|
|
attrs['bacon'] = 'yum'
|
2008-08-12 06:22:26 +08:00
|
|
|
return attrs
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_item_elements(self, handler, item):
|
|
|
|
super(MyCustomAtom1Feed, self).add_item_elements(handler, item)
|
2012-06-08 00:08:47 +08:00
|
|
|
handler.addQuickElement('ministry', 'silly walks')
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
class TestCustomFeed(TestAtomFeed):
|
|
|
|
feed_type = MyCustomAtom1Feed
|