From fe3c741347f07cb0b70f21e44d63d9ebadbf71e8 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 30 Oct 2022 12:53:15 +0000 Subject: [PATCH] Construct HTML title in Python Site is cached, and makes it easier to reference elsewhere --- website/common/models.py | 5 +++++ website/common/templates/404.html | 6 +++++- website/common/templates/base.html | 4 ---- website/common/templates/wagtail_base.html | 2 +- website/common/tests/test_pages.py | 4 ++-- website/common/utils.py | 8 +++++++- website/home/models.py | 2 +- 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/website/common/models.py b/website/common/models.py index 842495e..fb3fad8 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -32,6 +32,7 @@ from .utils import ( TocEntry, count_words, extract_text, + get_site_title, get_table_of_contents, truncate_string, ) @@ -57,6 +58,10 @@ class BasePage(Page): def html_title(self) -> str: return self.seo_title or self.title + @cached_property + def html_title_tag(self) -> str: + return f"{self.html_title} :: {get_site_title()}" + @cached_property def hero_title(self) -> str: return self.html_title diff --git a/website/common/templates/404.html b/website/common/templates/404.html index 092db91..fcfc5d8 100644 --- a/website/common/templates/404.html +++ b/website/common/templates/404.html @@ -4,7 +4,11 @@ {% block body_class %}page-404{% endblock %} -{% block title %}Page not found{% endblock %} +{% block title %} + Page not found + {% wagtail_site as current_site %} + {% if current_site and current_site.site_name %}:: {{ current_site.site_name }}{% endif %} +{% endblock %} {% block main_content %} diff --git a/website/common/templates/base.html b/website/common/templates/base.html index beb500f..48bb042 100644 --- a/website/common/templates/base.html +++ b/website/common/templates/base.html @@ -6,10 +6,6 @@ {% block title %}{% endblock %} - {% block title_suffix %} - {% wagtail_site as current_site %} - {% if current_site and current_site.site_name %}:: {{ current_site.site_name }}{% endif %} - {% endblock %} {% if not SEO_INDEX %}{% endif %} diff --git a/website/common/templates/wagtail_base.html b/website/common/templates/wagtail_base.html index 95e356c..5c52b0f 100644 --- a/website/common/templates/wagtail_base.html +++ b/website/common/templates/wagtail_base.html @@ -4,7 +4,7 @@ {% block body_class %}{{ page.body_class }}{% endblock %} -{% block title %}{{ page.html_title }}{% endblock %} +{% block title %}{{ page.html_title_tag }}{% endblock %} {% block extra_head %} {% meta_tags %} diff --git a/website/common/tests/test_pages.py b/website/common/tests/test_pages.py index 8500c09..c5e1d84 100644 --- a/website/common/tests/test_pages.py +++ b/website/common/tests/test_pages.py @@ -35,7 +35,7 @@ class ContentPageTestCase(TestCase): self.assertEqual(response.status_code, 200) def test_queries(self) -> None: - with self.assertNumQueries(33): + with self.assertNumQueries(34): self.client.get(self.page.url) @@ -52,7 +52,7 @@ class ListingPageTestCase(TestCase): ContentPageFactory(parent=cls.page) def test_accessible(self) -> None: - with self.assertNumQueries(37): + with self.assertNumQueries(38): response = self.client.get(self.page.url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context["listing_pages"]), 2) diff --git a/website/common/utils.py b/website/common/utils.py index 4ea5a3c..b702bdf 100644 --- a/website/common/utils.py +++ b/website/common/utils.py @@ -6,8 +6,9 @@ from bs4 import BeautifulSoup, SoupStrainer from django.conf import settings from django.http.request import HttpRequest from django.utils.text import slugify, smart_split +from django_cache_decorator import django_cache_decorator from more_itertools import ilen -from wagtail.models import Page +from wagtail.models import Page, Site from wagtail.models import get_page_models as get_wagtail_page_models HEADER_TAGS = ["h2", "h3", "h4", "h5", "h6"] @@ -96,3 +97,8 @@ def heading_id(heading: str) -> str: if slug[0].isdigit(): return "ref-" + slug return slug + + +@django_cache_decorator(time=300) +def get_site_title() -> str: + return Site.objects.values_list("site_name", flat=True).first() diff --git a/website/home/models.py b/website/home/models.py index d1600e7..a69d781 100644 --- a/website/home/models.py +++ b/website/home/models.py @@ -52,7 +52,7 @@ class HomePage(BasePage, WagtailImageMetadataMixin): return self.search_description def get_object_title(self) -> str: - return "" + return self.html_title def get_context(self, request: HttpRequest) -> dict: from website.search.models import SearchPage