PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/syndication/feeds.py

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