From 9139ad1aa2c62236ac04c8d4d2e3394283b09644 Mon Sep 17 00:00:00 2001 From: Konstantin Kostov Date: Tue, 26 Mar 2024 18:08:42 +0100 Subject: [PATCH] Add RSS and Atom feed for the blog --- blog/feeds.py | 45 +++++++++++++++++++++++++++ iamkonstantin_web/templates/base.html | 2 ++ iamkonstantin_web/urls.py | 2 ++ 3 files changed, 49 insertions(+) create mode 100644 blog/feeds.py diff --git a/blog/feeds.py b/blog/feeds.py new file mode 100644 index 0000000..f170da6 --- /dev/null +++ b/blog/feeds.py @@ -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 \ No newline at end of file diff --git a/iamkonstantin_web/templates/base.html b/iamkonstantin_web/templates/base.html index 02596e8..d4253b8 100644 --- a/iamkonstantin_web/templates/base.html +++ b/iamkonstantin_web/templates/base.html @@ -35,6 +35,8 @@ {# Global stylesheets #} + + {% block extra_css %} {# Override this in templates to add extra stylesheets #} {% endblock %} diff --git a/iamkonstantin_web/urls.py b/iamkonstantin_web/urls.py index 98d4e4a..017d2ef 100644 --- a/iamkonstantin_web/urls.py +++ b/iamkonstantin_web/urls.py @@ -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"), ]