From 730a2bcb9df7cbf8ab26bbb0968f97e09ebb3d82 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Wed, 1 Nov 2017 09:29:59 +1100 Subject: [PATCH] Added support for RSS and Atom feeds, because why not --- entries/feeds.py | 79 +++++++++++++++++++++ entries/kinds.py | 8 +++ entries/templates/entries/index.html | 2 + entries/urls.py | 9 ++- entries/views.py | 7 +- home/views.py | 2 + lemoncurry/templates/lemoncurry/layout.html | 2 + 7 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 entries/feeds.py diff --git a/entries/feeds.py b/entries/feeds.py new file mode 100644 index 0000000..00b9cb4 --- /dev/null +++ b/entries/feeds.py @@ -0,0 +1,79 @@ +from django.contrib.sites.models import Site +from django.contrib.syndication.views import Feed +from django.urls import reverse +from django.utils.feedgenerator import Atom1Feed +from urllib.parse import urljoin +from .models import Entry + + +class EntriesFeed(Feed): + def item_title(self, entry): + return entry.title + + def item_description(self, entry): + return entry.content + + def item_author_name(self, entry): + return entry.author.name + + def item_author_email(self, entry): + return entry.author.email + + def item_author_link(self, entry): + base = 'https://' + Site.objects.get_current().domain + return urljoin(base, entry.author.url) + + def item_pubdate(self, entry): + return entry.published + + def item_updatedate(self, entry): + return entry.updated + + +class RssByKind(EntriesFeed): + def __init__(self, kind): + self.kind = kind + + def title(self): + return "{0} ~ {1}".format( + self.kind.plural, + Site.objects.get_current().name, + ) + + def link(self): + return reverse('entries:' + self.kind.index) + + def description(self): + return "all {0} at {1}".format( + self.kind.plural, + Site.objects.get_current().name, + ) + + def items(self): + return Entry.objects.filter(kind=self.kind.id) + + +class AtomByKind(RssByKind): + feed_type = Atom1Feed + subtitle = RssByKind.description + + +class RssAllEntries(EntriesFeed): + def title(self): + return Site.objects.get_current().name + + def link(self): + return reverse('home:index') + + def description(self): + return "content from {0}".format( + Site.objects.get_current().name, + ) + + def items(self): + return Entry.objects.all() + + +class AtomAllEntries(RssAllEntries): + feed_type = Atom1Feed + subtitle = RssAllEntries.description diff --git a/entries/kinds.py b/entries/kinds.py index 5703f54..09b0ce1 100644 --- a/entries/kinds.py +++ b/entries/kinds.py @@ -13,6 +13,14 @@ class Entry: def entry(self): return self.plural + '_entry' + @property + def atom(self): + return self.plural + '_atom' + + @property + def rss(self): + return self.plural + '_rss' + Note = Entry( id='note', diff --git a/entries/templates/entries/index.html b/entries/templates/entries/index.html index 1255b2f..4537284 100644 --- a/entries/templates/entries/index.html +++ b/entries/templates/entries/index.html @@ -1,9 +1,11 @@ {% extends 'lemoncurry/layout.html' %} {% load static %} {% block html_class %}h-feed{% endblock %} + {% block styles %} {% endblock %} + {% block main %}
    {% for entry in entries %} diff --git a/entries/urls.py b/entries/urls.py index 84d38e4..d9bb97c 100644 --- a/entries/urls.py +++ b/entries/urls.py @@ -1,5 +1,5 @@ from django.conf.urls import url -from . import kinds, views +from . import feeds, kinds, views from lemoncurry import breadcrumbs as crumbs @@ -12,13 +12,18 @@ def prefix(route): app_name = 'entries' -urlpatterns = [] +urlpatterns = [ + url('^atom$', feeds.AtomAllEntries(), name='atom'), + url('^rss$', feeds.RssAllEntries(), name='rss'), +] for k in kinds.all: kind = k.plural id = r'/(?P\d+)' slug = r'(?:/(?P.+))?' urlpatterns += ( url(to_pat(kind), views.index, name=k.index, kwargs={'kind': k}), + url(to_pat(kind, '/atom'), feeds.AtomByKind(k), name=k.atom), + url(to_pat(kind, '/rss'), feeds.RssByKind(k), name=k.rss), url(to_pat(kind, id, slug), views.entry, name=k.entry), ) diff --git a/entries/views.py b/entries/views.py index 6770053..eadba83 100644 --- a/entries/views.py +++ b/entries/views.py @@ -6,7 +6,12 @@ from .models import Entry @render_to('entries/index.html') def index(request, kind): entries = Entry.objects.filter(kind=kind.id) - return {'entries': entries, 'title': kind.plural} + return { + 'entries': entries, + 'atom': 'entries:' + kind.atom, + 'rss': 'entries:' + kind.rss, + 'title': kind.plural, + } @render_to('entries/entry.html') diff --git a/home/views.py b/home/views.py index 70e1a5e..17ff38a 100644 --- a/home/views.py +++ b/home/views.py @@ -17,6 +17,8 @@ def index(request): return { 'user': user, 'entries': user.entries.all(), + 'atom': 'entries:atom', + 'rss': 'entries:rss', 'meta': user.as_meta(request), } diff --git a/lemoncurry/templates/lemoncurry/layout.html b/lemoncurry/templates/lemoncurry/layout.html index 9fb76a3..1a98b10 100644 --- a/lemoncurry/templates/lemoncurry/layout.html +++ b/lemoncurry/templates/lemoncurry/layout.html @@ -6,6 +6,8 @@ + {% if atom %}{% endif %} + {% if rss %} {% endif %} {% block head %}{% endblock %}