From 922e259aaf25f9365123af6df56755dd599bda18 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 22 Aug 2022 21:11:46 +0100 Subject: [PATCH] Add legacy redirect app --- website/blog/factories.py | 8 ++++++++ website/legacy/__init__.py | 0 website/legacy/tests.py | 17 +++++++++++++++++ website/legacy/urls.py | 7 +++++++ website/legacy/views.py | 13 +++++++++++++ website/settings.py | 1 + website/urls.py | 1 + 7 files changed, 47 insertions(+) create mode 100644 website/blog/factories.py create mode 100644 website/legacy/__init__.py create mode 100644 website/legacy/tests.py create mode 100644 website/legacy/urls.py create mode 100644 website/legacy/views.py diff --git a/website/blog/factories.py b/website/blog/factories.py new file mode 100644 index 0000000..a06348b --- /dev/null +++ b/website/blog/factories.py @@ -0,0 +1,8 @@ +from website.common.factories import BaseContentFactory + +from . import models + + +class BlogPostListPageFactory(BaseContentFactory): + class Meta: + model = models.BlogPostListPage diff --git a/website/legacy/__init__.py b/website/legacy/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/website/legacy/tests.py b/website/legacy/tests.py new file mode 100644 index 0000000..28a4132 --- /dev/null +++ b/website/legacy/tests.py @@ -0,0 +1,17 @@ +from django.test import TestCase + +from website.blog.factories import BlogPostListPageFactory +from website.home.models import HomePage + + +class PostsFeedViewTestCase(TestCase): + @classmethod + def setUpTestData(cls) -> None: + cls.home_page = HomePage.objects.get() + cls.page = BlogPostListPageFactory(parent=cls.home_page) + + def test_redirects(self) -> None: + response = self.client.get("/posts/index.xml") + self.assertRedirects( + response, self.page.url + self.page.reverse_subpage("feed") + ) diff --git a/website/legacy/urls.py b/website/legacy/urls.py new file mode 100644 index 0000000..5efb185 --- /dev/null +++ b/website/legacy/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from . import views + +app_name = "legacy" + +urlpatterns = [path("posts/index.xml", views.PostsFeedView.as_view())] diff --git a/website/legacy/views.py b/website/legacy/views.py new file mode 100644 index 0000000..aac3c5d --- /dev/null +++ b/website/legacy/views.py @@ -0,0 +1,13 @@ +from django.shortcuts import get_object_or_404 +from django.utils.decorators import method_decorator +from django.views.decorators.cache import cache_page +from django.views.generic import RedirectView + +from website.blog.models import BlogPostListPage + + +@method_decorator(cache_page(60 * 60), name="dispatch") +class PostsFeedView(RedirectView): + def get_redirect_url(self) -> str: + post_list = get_object_or_404(BlogPostListPage.objects.live()) + return post_list.url + post_list.reverse_subpage("feed") diff --git a/website/settings.py b/website/settings.py index d269338..79924ab 100644 --- a/website/settings.py +++ b/website/settings.py @@ -35,6 +35,7 @@ INSTALLED_APPS = [ "website.spotify", "website.utils", "website.well_known", + "website.legacy", "website.contrib.code_block", "website.contrib.mermaid_block", "website.contrib.unsplash", diff --git a/website/urls.py b/website/urls.py index 5bea475..adc218b 100644 --- a/website/urls.py +++ b/website/urls.py @@ -25,6 +25,7 @@ urlpatterns = [ path("sitemap.xml", cache_page(60 * 60)(sitemap), name="sitemap"), path("robots.txt", cache_page(60 * 60)(RobotsView.as_view()), name="robotstxt"), path("404/", page_not_found, name="404"), + path("", include("website.legacy.urls")), ]