Construct HTML title in Python
Site is cached, and makes it easier to reference elsewhere
This commit is contained in:
parent
1023c821ab
commit
fe3c741347
7 changed files with 21 additions and 10 deletions
|
@ -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
|
||||
|
|
|
@ -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 %}
|
||||
<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">
|
||||
|
|
|
@ -6,10 +6,6 @@
|
|||
<meta charset="utf-8" />
|
||||
<title>
|
||||
{% 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>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
{% if not SEO_INDEX %}<meta name="robots" content="noindex" />{% endif %}
|
||||
|
|
|
@ -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 %}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue