Josh VanderLinden avatar Josh VanderLinden committed 887fbec

Fixing RSS feeds and making them more accessible

Comments (0)

Files changed (8)

articles/__init__.py

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

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
 

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']
 

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',)
 

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 %}
 

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>

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>
 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'),
+
 )
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.