Construct HTML title in Python

Site is cached, and makes it easier to reference elsewhere
This commit is contained in:
Jake Howard 2022-10-30 12:53:15 +00:00
parent 1023c821ab
commit fe3c741347
Signed by: jake
GPG key ID: 57AFB45680EDD477
7 changed files with 21 additions and 10 deletions

View file

@ -32,6 +32,7 @@ from .utils import (
TocEntry, TocEntry,
count_words, count_words,
extract_text, extract_text,
get_site_title,
get_table_of_contents, get_table_of_contents,
truncate_string, truncate_string,
) )
@ -57,6 +58,10 @@ class BasePage(Page):
def html_title(self) -> str: def html_title(self) -> str:
return self.seo_title or self.title 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 @cached_property
def hero_title(self) -> str: def hero_title(self) -> str:
return self.html_title return self.html_title

View file

@ -4,7 +4,11 @@
{% block body_class %}page-404{% endblock %} {% 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 %} {% block main_content %}
<marquee {% if homepage.image %}style="background-image: url({% image_url homepage.image 'width-1200' %})"{% endif %} class="outer" direction="down" behavior="alternate" scrolldelay="15" truespeed scrollamount="2"> <marquee {% if homepage.image %}style="background-image: url({% image_url homepage.image 'width-1200' %})"{% endif %} class="outer" direction="down" behavior="alternate" scrolldelay="15" truespeed scrollamount="2">

View file

@ -6,10 +6,6 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<title> <title>
{% block title %}{% endblock %} {% 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 %}
</title> </title>
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
{% if not SEO_INDEX %}<meta name="robots" content="noindex" />{% endif %} {% if not SEO_INDEX %}<meta name="robots" content="noindex" />{% endif %}

View file

@ -4,7 +4,7 @@
{% block body_class %}{{ page.body_class }}{% endblock %} {% block body_class %}{{ page.body_class }}{% endblock %}
{% block title %}{{ page.html_title }}{% endblock %} {% block title %}{{ page.html_title_tag }}{% endblock %}
{% block extra_head %} {% block extra_head %}
{% meta_tags %} {% meta_tags %}

View file

@ -35,7 +35,7 @@ class ContentPageTestCase(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_queries(self) -> None: def test_queries(self) -> None:
with self.assertNumQueries(33): with self.assertNumQueries(34):
self.client.get(self.page.url) self.client.get(self.page.url)
@ -52,7 +52,7 @@ class ListingPageTestCase(TestCase):
ContentPageFactory(parent=cls.page) ContentPageFactory(parent=cls.page)
def test_accessible(self) -> None: def test_accessible(self) -> None:
with self.assertNumQueries(37): with self.assertNumQueries(38):
response = self.client.get(self.page.url) response = self.client.get(self.page.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context["listing_pages"]), 2) self.assertEqual(len(response.context["listing_pages"]), 2)

View file

@ -6,8 +6,9 @@ from bs4 import BeautifulSoup, SoupStrainer
from django.conf import settings from django.conf import settings
from django.http.request import HttpRequest from django.http.request import HttpRequest
from django.utils.text import slugify, smart_split from django.utils.text import slugify, smart_split
from django_cache_decorator import django_cache_decorator
from more_itertools import ilen 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 from wagtail.models import get_page_models as get_wagtail_page_models
HEADER_TAGS = ["h2", "h3", "h4", "h5", "h6"] HEADER_TAGS = ["h2", "h3", "h4", "h5", "h6"]
@ -96,3 +97,8 @@ def heading_id(heading: str) -> str:
if slug[0].isdigit(): if slug[0].isdigit():
return "ref-" + slug return "ref-" + slug
return slug return slug
@django_cache_decorator(time=300)
def get_site_title() -> str:
return Site.objects.values_list("site_name", flat=True).first()

View file

@ -52,7 +52,7 @@ class HomePage(BasePage, WagtailImageMetadataMixin):
return self.search_description return self.search_description
def get_object_title(self) -> str: def get_object_title(self) -> str:
return "" return self.html_title
def get_context(self, request: HttpRequest) -> dict: def get_context(self, request: HttpRequest) -> dict:
from website.search.models import SearchPage from website.search.models import SearchPage