Add RSS and Atom feed for the blog

This commit is contained in:
Konstantin 2024-03-26 18:08:42 +01:00
parent 2b18d44281
commit 9139ad1aa2
3 changed files with 49 additions and 0 deletions

45
blog/feeds.py Normal file
View file

@ -0,0 +1,45 @@
from datetime import datetime, time
from django.contrib.syndication.views import Feed
from django.http import Http404
from .models import BlogPage, BlogIndexPage
from django.utils.feedgenerator import Atom1Feed
class RssBlogFeed(Feed):
title = "Konstantin's Blog Feed"
description = "A blog on independent software development, Swift, Elixir and more..."
def link(self):
root = BlogIndexPage.objects.live().public().first()
if not root:
raise Http404
return root.full_url
def items(self):
return BlogPage.objects.live().public().order_by("-first_published_at")
def item_title(self, item):
return item.title
def item_description(self, item):
return item.intro
def item_link(self, item):
return item.full_url
def item_pubdate(self, item):
return datetime.combine(item.date, time())
def item_author_name(self, item):
author = item.authors.first()
if not author:
return None
return author.name
class AtomBlogFeed(RssBlogFeed):
feed_type = Atom1Feed
subtitle = RssBlogFeed.description

View file

@ -35,6 +35,8 @@
{# Global stylesheets #}
<link rel="stylesheet" type="text/css" href="{% static 'css/iamkonstantin_web.css' %}">
<link title="Konstantin's Blog" type="application/atom+xml" rel="alternate" href="{% url 'blog_feed' %}">
{% block extra_css %}
{# Override this in templates to add extra stylesheets #}
{% endblock %}

View file

@ -6,6 +6,7 @@ from wagtail.admin import urls as wagtailadmin_urls
from wagtail import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from blog.feeds import RssBlogFeed
from search import views as search_views
urlpatterns = [
@ -13,6 +14,7 @@ urlpatterns = [
path("admin/", include(wagtailadmin_urls)),
path("documents/", include(wagtaildocs_urls)),
path("search/", search_views.search, name="search"),
path("blog/feed/", RssBlogFeed(), name="blog_feed"),
]