Add dedicated favicon view

This commit is contained in:
Jake Howard 2023-07-23 14:44:35 +01:00
parent 7105386e61
commit 96a6ded02e
Signed by: jake
GPG Key ID: 57AFB45680EDD477
3 changed files with 24 additions and 3 deletions

View File

@ -16,7 +16,7 @@ class Error404PageTestCase(TestCase):
)
def test_actual_404_no_url_match(self) -> None:
response = self.client.get("/favicon.ico")
response = self.client.get("/missing.file")
self.assertContains(
response, "<h1>There's nothing here!</h1>", html=True, status_code=404
)

View File

@ -9,9 +9,11 @@ from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_control, cache_page
from django.views.defaults import ERROR_404_TEMPLATE_NAME
from django.views.generic import TemplateView
from django.views.generic import RedirectView, TemplateView
from wagtail.models import Page
from wagtail.query import PageQuerySet
from wagtail_favicon.models import FaviconSettings
from wagtail_favicon.utils import get_rendition_url
from website.common.utils import get_site_title
from website.contrib.singleton_page.utils import SingletonPageCache
@ -139,3 +141,15 @@ class ContentPageFeed(AllPagesFeed):
def items(self) -> PageQuerySet:
return self.posts
@method_decorator(cache_control(max_age=60 * 60), name="dispatch")
class FaviconView(RedirectView):
def get_redirect_url(self) -> str:
favicon_settings = FaviconSettings.for_request(self.request)
size = FaviconSettings.icon_sizes[0]
# Force image to PNG
return get_rendition_url(
favicon_settings.base_favicon_image, f"fill-{size}|format-png"
)

View File

@ -14,7 +14,13 @@ from wagtail.images.views.serve import ServeView
from wagtail_favicon.urls import urls as favicon_urls
from wagtailautocomplete.urls.admin import urlpatterns as autocomplete_admin_urls
from website.common.views import AllPagesFeed, KeybaseView, RobotsView, page_not_found
from website.common.views import (
AllPagesFeed,
FaviconView,
KeybaseView,
RobotsView,
page_not_found,
)
urlpatterns = [
path("admin/autocomplete/", include(autocomplete_admin_urls)),
@ -49,6 +55,7 @@ urlpatterns = [
"@jake",
RedirectView.as_view(url=f"https://{settings.ACTIVITYPUB_HOST}/@jake"),
),
path("favicon.ico", FaviconView.as_view()),
path("", include(favicon_urls)),
]