feeds.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from __future__ import absolute_import, unicode_literals
  2. from django.contrib.syndication import views
  3. from django.core.exceptions import ObjectDoesNotExist
  4. from django.utils import feedgenerator, tzinfo
  5. from .models import Article, Entry
  6. class ComplexFeed(views.Feed):
  7. def get_object(self, request, foo=None):
  8. if foo is not None:
  9. raise ObjectDoesNotExist
  10. return None
  11. class TestRss2Feed(views.Feed):
  12. title = 'My blog'
  13. description = 'A more thorough description of my blog.'
  14. link = '/blog/'
  15. feed_guid = '/foo/bar/1234'
  16. author_name = 'Sally Smith'
  17. author_email = 'test@example.com'
  18. author_link = 'http://www.example.com/'
  19. categories = ('python', 'django')
  20. feed_copyright = 'Copyright (c) 2007, Sally Smith'
  21. ttl = 600
  22. def items(self):
  23. return Entry.objects.all()
  24. def item_description(self, item):
  25. return "Overridden description: %s" % item
  26. def item_pubdate(self, item):
  27. return item.date
  28. item_author_name = 'Sally Smith'
  29. item_author_email = 'test@example.com'
  30. item_author_link = 'http://www.example.com/'
  31. item_categories = ('python', 'testing')
  32. item_copyright = 'Copyright (c) 2007, Sally Smith'
  33. class TestRss2FeedWithGuidIsPermaLinkTrue(TestRss2Feed):
  34. def item_guid_is_permalink(self, item):
  35. return True
  36. class TestRss2FeedWithGuidIsPermaLinkFalse(TestRss2Feed):
  37. def item_guid(self, item):
  38. return str(item.pk)
  39. def item_guid_is_permalink(self, item):
  40. return False
  41. class TestRss091Feed(TestRss2Feed):
  42. feed_type = feedgenerator.RssUserland091Feed
  43. class TestNoPubdateFeed(views.Feed):
  44. title = 'Test feed'
  45. link = '/feed/'
  46. def items(self):
  47. return Entry.objects.all()
  48. class TestAtomFeed(TestRss2Feed):
  49. feed_type = feedgenerator.Atom1Feed
  50. subtitle = TestRss2Feed.description
  51. class 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. class TestEnclosureFeed(TestRss2Feed):
  59. pass
  60. class TemplateFeed(TestRss2Feed):
  61. """
  62. A feed to test defining item titles and descriptions with templates.
  63. """
  64. title_template = 'syndication/title.html'
  65. description_template = 'syndication/description.html'
  66. # Defining a template overrides any item_title definition
  67. def item_title(self):
  68. return "Not in a template"
  69. class NaiveDatesFeed(TestAtomFeed):
  70. """
  71. A feed with naive (non-timezone-aware) dates.
  72. """
  73. def item_pubdate(self, item):
  74. return item.date
  75. class TZAwareDatesFeed(TestAtomFeed):
  76. """
  77. A feed with timezone-aware dates.
  78. """
  79. def item_pubdate(self, item):
  80. # Provide a weird offset so that the test can know it's getting this
  81. # specific offset and not accidentally getting on from
  82. # settings.TIME_ZONE.
  83. return item.date.replace(tzinfo=tzinfo.FixedOffset(42))
  84. class TestFeedUrlFeed(TestAtomFeed):
  85. feed_url = 'http://example.com/customfeedurl/'
  86. class MyCustomAtom1Feed(feedgenerator.Atom1Feed):
  87. """
  88. Test of a custom feed generator class.
  89. """
  90. def root_attributes(self):
  91. attrs = super(MyCustomAtom1Feed, self).root_attributes()
  92. attrs['django'] = 'rocks'
  93. return attrs
  94. def add_root_elements(self, handler):
  95. super(MyCustomAtom1Feed, self).add_root_elements(handler)
  96. handler.addQuickElement('spam', 'eggs')
  97. def item_attributes(self, item):
  98. attrs = super(MyCustomAtom1Feed, self).item_attributes(item)
  99. attrs['bacon'] = 'yum'
  100. return attrs
  101. def add_item_elements(self, handler, item):
  102. super(MyCustomAtom1Feed, self).add_item_elements(handler, item)
  103. handler.addQuickElement('ministry', 'silly walks')
  104. class TestCustomFeed(TestAtomFeed):
  105. feed_type = MyCustomAtom1Feed