Commits

Josh VanderLinden  committed 887fbec

Fixing RSS feeds and making them more accessible

  • Participants
  • Parent commits 6f3a99d

Comments (0)

Files changed (8)

File articles/__init__.py

-__version__ = '1.6a'
+__version__ = '1.7a'
 
 """
 The Pygments reStructuredText directive

File articles/feeds.py

+from django.conf import settings
 from django.contrib.syndication.feeds import Feed
+from django.contrib.sites.models import Site
 from django.core.cache import cache
 from django.core.urlresolvers import reverse
-from django.contrib.sites.models import Site
 from articles.models import Article, Tag
 
 SITE = Site.objects.get_current()
 
+# default to 24 hours for feed caching
+FEED_TIMEOUT = getattr(settings, 'ARTICLE_FEED_TIMEOUT', 86400)
+
 class LatestEntries(Feed):
     def title(self):
         return "%s Articles" % SITE.name
         articles = cache.get(key)
 
         if articles is None:
-            articles = Article.objects.active().order_by('-publish_date')[:15]
-            cache.set(key, articles)
+            articles = list(Article.objects.active().order_by('-publish_date')[:15])
+            cache.set(key, articles, FEED_TIMEOUT)
 
         return articles
 
         if len(bits) != 1:
             raise FeedDoesNotExist
 
-        return Tag.objects.get(name__exact=bits[0])
+        return Tag.objects.get(name__iexact=bits[0])
 
     def title(self, obj):
         return "%s: Newest Articles Tagged '%s'" % (SITE.name, obj.name)
 
     def item_set(self, obj):
         key = 'articles_for_%s' % obj.name
-        #articles = cache.get(key)
+        articles = cache.get(key)
 
-        #if articles is None:
-        articles = obj.article_set.active().order_by('-publish_date')
-        #cache.set(key, articles)
+        if articles is None:
+            articles = list(obj.article_set.active().order_by('-publish_date'))
+            cache.set(key, articles, FEED_TIMEOUT)
 
         return articles
 

File articles/forms.py

     def clean_tags(self):
         """Turns the string of tags into a list"""
 
-        tags = [Tag.objects.get_or_create(name=t.strip())[0] for t in self.cleaned_data['tags'].split()]
+        tags = [Tag.objects.get_or_create(name__iexact=t.strip())[0] for t in self.cleaned_data['tags'].split()]
         self.cleaned_data['tags'] = tags
         return self.cleaned_data['tags']
 

File articles/models.py

     def get_absolute_url(self):
         return ('articles_display_tag', (self.name,))
 
+    @property
+    def rss_name(self):
+        return u'tags/%s' % self.name
+
     class Meta:
         ordering = ('name',)
 

File articles/templates/articles/article_detail.html

 {% block title %}{% trans article.title %}{% endblock %}
 {% block meta-keywords %}{{ article.keywords|escape }}{% endblock %}
 {% block meta-description %}{{ article.description|escape }}{% endblock %}
+{% block extra-head %}
+{{ block.super }}
+{% for tag in article.tags.all %}
+<link rel="alternate" type="application/rss+xml" title="Blog Articles Tagged '{{ tag.name }}' RSS Feed" href="{% url articles_tag_feed tag.name %}" />{% endfor %}
+{% endblock %}
 
 {% block content %}
 

File articles/templates/articles/base.html

 {% extends 'base.html' %}
 {% load article_tags i18n %}
 
+{% block extra-head %}
+<link rel="alternate" type="application/rss+xml" title="Latest Blog Articles RSS Feed" href="{% url articles_feed 'latest' %}" />
+{% endblock %}
+
 {% block content %}
 <div id="article-archives">
     <h2 class="title">{% trans 'Article Archives' %}</h2>

File articles/templates/articles/display_tag.html

 {% load i18n %}
 
 {% block title %}{% trans 'Articles Tagged' %}: {{ tag.name }}{% endblock %}
+{% block extra-head %}
+{{ block.super }}
+<link rel="alternate" type="application/rss+xml" title="Blog Articles Tagged '{{ tag.name }}' RSS Feed" href="{% url articles_feed tag.rss_name %}" />
+{% endblock %}
 
 {% block articles-content %}
 <h2>{% trans 'Articles Tagged' %} <em>{{ tag.name }}</em>{% ifnotequal paginator.num_pages 1 %}, {% trans 'page' %} {{ page_obj.number }}{% endifnotequal %}</h2>

File articles/urls.py

 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
+
+feeds = {
+  'latest': LatestEntries,
+  'tags': TagFeed
+}
+feed_dict = {'feed_dict': feeds}
 
 urlpatterns = patterns('',
     (r'^(?P<year>\d{4})/(?P<month>.{3})/(?P<day>\d{1,2})/(?P<slug>.*)/$', views.redirect_to_article),
 
     # AJAX
     url(r'^ajax/tag/autocomplete/$', views.ajax_tag_autocomplete, name='articles_tag_autocomplete'),
+
+    # RSS
+    (r'^feeds/(?P<url>.*)/$', feed, feed_dict),
+    url(r'^feeds/(?P<url>.*)\.rss$', feed, feed_dict, name='articles_feed'),
+
 )