Add all pages feed

This one is much simpler than the pages one, as there could be a lot of
pages
This commit is contained in:
Jake Howard 2022-08-22 21:32:38 +01:00
parent 922e259aaf
commit 10005dfe02
Signed by: jake
GPG key ID: 57AFB45680EDD477
6 changed files with 59 additions and 2 deletions

View file

@ -37,3 +37,11 @@ class RobotsViewTestCase(SimpleTestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertContains(response, "Disallow: /") self.assertContains(response, "Disallow: /")
self.assertFalse(response.context["SEO_INDEX"]) self.assertFalse(response.context["SEO_INDEX"])
class AllPagesFeedTestCase(TestCase):
url = reverse("feed")
def test_accessible(self) -> None:
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)

View file

@ -1,12 +1,19 @@
from datetime import datetime
from typing import Any from typing import Any
from django.contrib.syndication.views import Feed
from django.http.request import HttpRequest
from django.http.response import HttpResponse from django.http.response import HttpResponse
from django.urls import reverse from django.urls import reverse
from django.views.defaults import ERROR_404_TEMPLATE_NAME from django.views.defaults import ERROR_404_TEMPLATE_NAME
from django.views.generic import TemplateView from django.views.generic import TemplateView
from wagtail.models import Page
from wagtail.query import PageQuerySet
from website.home.models import HomePage from website.home.models import HomePage
from .models import BasePage
class Error404View(TemplateView): class Error404View(TemplateView):
template_name = ERROR_404_TEMPLATE_NAME template_name = ERROR_404_TEMPLATE_NAME
@ -33,3 +40,29 @@ class RobotsView(TemplateView):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["sitemap"] = self.request.build_absolute_uri(reverse("sitemap")) context["sitemap"] = self.request.build_absolute_uri(reverse("sitemap"))
return context return context
class AllPagesFeed(Feed):
link = "/feed/"
title = "All pages feed"
def __call__(
self, request: HttpRequest, *args: list, **kwargs: dict
) -> HttpResponse:
self.request = request
return super().__call__(request, *args, **kwargs)
def items(self) -> PageQuerySet:
return Page.objects.live().exclude(depth__lte=2)
def item_title(self, item: BasePage) -> str:
return item.title
def item_link(self, item: BasePage) -> str:
return item.get_full_url(request=self.request)
def item_pubdate(self, item: BasePage) -> datetime:
return item.first_published_at
def item_updateddate(self, item: BasePage) -> datetime:
return item.last_published_at

View file

@ -1,4 +1,5 @@
from django.test import TestCase from django.test import TestCase
from django.urls import reverse
from website.blog.factories import BlogPostListPageFactory from website.blog.factories import BlogPostListPageFactory
from website.home.models import HomePage from website.home.models import HomePage
@ -15,3 +16,9 @@ class PostsFeedViewTestCase(TestCase):
self.assertRedirects( self.assertRedirects(
response, self.page.url + self.page.reverse_subpage("feed") response, self.page.url + self.page.reverse_subpage("feed")
) )
class AllPagesFeedViewTestCase(TestCase):
def test_redirects(self) -> None:
response = self.client.get("/index.xml")
self.assertRedirects(response, reverse("feed"))

View file

@ -4,4 +4,7 @@ from . import views
app_name = "legacy" app_name = "legacy"
urlpatterns = [path("posts/index.xml", views.PostsFeedView.as_view())] urlpatterns = [
path("posts/index.xml", views.PostsFeedView.as_view()),
path("index.xml", views.AllPagesFeedView.as_view()),
]

View file

@ -11,3 +11,8 @@ class PostsFeedView(RedirectView):
def get_redirect_url(self) -> str: def get_redirect_url(self) -> str:
post_list = get_object_or_404(BlogPostListPage.objects.live()) post_list = get_object_or_404(BlogPostListPage.objects.live())
return post_list.url + post_list.reverse_subpage("feed") return post_list.url + post_list.reverse_subpage("feed")
@method_decorator(cache_page(60 * 60), name="dispatch")
class AllPagesFeedView(RedirectView):
pattern_name = "feed"

View file

@ -7,7 +7,7 @@ from wagtail.contrib.sitemaps.views import sitemap
from wagtail.documents import urls as wagtaildocs_urls from wagtail.documents import urls as wagtaildocs_urls
from wagtail.images.views.serve import ServeView from wagtail.images.views.serve import ServeView
from website.common.views import RobotsView, page_not_found from website.common.views import AllPagesFeed, RobotsView, page_not_found
urlpatterns = [ urlpatterns = [
path("admin/", include(wagtailadmin_urls)), path("admin/", include(wagtailadmin_urls)),
@ -25,6 +25,7 @@ urlpatterns = [
path("sitemap.xml", cache_page(60 * 60)(sitemap), name="sitemap"), path("sitemap.xml", cache_page(60 * 60)(sitemap), name="sitemap"),
path("robots.txt", cache_page(60 * 60)(RobotsView.as_view()), name="robotstxt"), path("robots.txt", cache_page(60 * 60)(RobotsView.as_view()), name="robotstxt"),
path("404/", page_not_found, name="404"), path("404/", page_not_found, name="404"),
path("feed/", AllPagesFeed(), name="feed"),
path("", include("website.legacy.urls")), path("", include("website.legacy.urls")),
] ]