Josh VanderLinden avatar Josh VanderLinden committed d51533b

Achieved 100% coverage for the feeds. Added Atom feeds.

Comments (0)

Files changed (3)

articles/feeds.py

 from django.contrib.sites.models import Site
 from django.core.cache import cache
 from django.core.urlresolvers import reverse
+from django.utils.feedgenerator import Atom1Feed
+
 from articles.models import Article, Tag
 
 # default to 24 hours for feed caching
     @property
     def site(self):
         if not hasattr(self, '_site'):
-            try:
-                self._site = Site.objects.get_current()
-            except AttributeError:
-                self._site = Site(domain='example.com', name='Demo Site')
+            self._site = Site.objects.get_current()
 
         return self._site
 
 class LatestEntries(Feed, SiteMixin):
+
     def title(self):
         return "%s Articles" % (self.site.name,)
 
     def item_author_name(self, item):
         return item.author.username
 
-    def item_tags(self, item):
-        return [c.name for c in item.tags.all()] + [keyword.strip() for keyword in item.keywords.split(',')]
-
     def item_pubdate(self, item):
         return item.publish_date
 
 class TagFeed(Feed, SiteMixin):
+
     def get_object(self, bits):
         if len(bits) != 1:
             raise FeedDoesNotExist
         return "%s: Newest Articles Tagged '%s'" % (self.site.name, obj.name)
 
     def link(self, obj):
-        if not obj:
-            raise FeedDoesNotExist
         return obj.get_absolute_url()
 
     def description(self, obj):
     def item_pubdate(self, item):
         return item.publish_date
 
+class LatestEntriesAtom(LatestEntries):
+    feed_type = Atom1Feed
+
+class TagFeedAtom(TagFeed):
+    feed_type = Atom1Feed

articles/tests.py

 from django.contrib.auth.models import User, Permission
 from django.core.urlresolvers import reverse
 from django.test import TestCase
-from django.test.client import Client, RequestFactory
+from django.test.client import Client
 
 from models import Article, ArticleStatus, Tag, MARKUP_HTML
 
 
     def setUp(self):
         self.client = Client()
-        self.factory = RequestFactory()
 
         User.objects.create_superuser('admin', 'admin@admin.com', 'admin')
         self.client.login(username='admin', password='admin')
         self.client.login(username='admin', password='admin')
         res = self.client.get(reverse('admin:articles_article_changelist'))
         self.assertEqual(res.content.count('_selected_action'), 10)
+
+class FeedTest(TestCase, ArticleUtilMixin):
+    fixtures = ['tags', 'users']
+
+    def setUp(self):
+        self.client = Client()
+
+        status = ArticleStatus.objects.filter(is_live=True)[0]
+        self.new_article('This is a test!', 'Testing testing 1 2 3',
+                         tags=Tag.objects.all(), status=status)
+
+    def test_latest_entries(self):
+        """Makes sure the latest entries feed works"""
+
+        self.client.get(reverse('articles_feed', args=['latest']))
+        self.client.get(reverse('articles_feed_atom', args=['latest']))
+
+    def test_tags(self):
+        """Makes sure that the tags feed works"""
+
+        self.client.get(reverse('articles_feed', args=['tags/demo']))
+        self.client.get(reverse('articles_feed', args=['tags/demo/']))
+
+        self.client.get(reverse('articles_feed_atom', args=['tags/demo']))
+        self.client.get(reverse('articles_feed_atom', args=['tags/demo/']))
 from django.conf.urls.defaults import *
 from django.contrib.syndication.views import feed
-from django.views.generic import date_based, list_detail
+
 from articles import views
-from articles.feeds import TagFeed, LatestEntries
+from articles.feeds import TagFeed, LatestEntries, TagFeedAtom, LatestEntriesAtom
 
 feeds = {
   'latest': LatestEntries,
 }
 feed_dict = {'feed_dict': feeds}
 
+atom_feeds = {
+    'latest': LatestEntriesAtom,
+    'tags': TagFeedAtom,
+}
+atom_dict = {'feed_dict': atom_feeds}
+
 urlpatterns = patterns('',
     (r'^(?P<year>\d{4})/(?P<month>.{3})/(?P<day>\d{1,2})/(?P<slug>.*)/$', views.redirect_to_article),
     url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/page/(?P<page>\d+)/$', views.display_blog_page, name='articles_in_month_page'),
     url(r'^ajax/tag/autocomplete/$', views.ajax_tag_autocomplete, name='articles_tag_autocomplete'),
 
     # RSS
+    url(r'^feeds/atom/(?P<url>.*)\.xml$', feed, atom_dict, name='articles_feed_atom'),
     (r'^feeds/(?P<url>.*)/$', feed, feed_dict),
     url(r'^feeds/(?P<url>.*)\.rss$', feed, feed_dict, name='articles_feed'),
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.