Cache latest blog post

This commit is contained in:
Jake Howard 2022-10-21 13:20:22 +01:00
parent 35579a8803
commit 214f10002c
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 33 additions and 10 deletions

14
poetry.lock generated
View file

@ -396,6 +396,14 @@ python-versions = ">=3.6"
[package.dependencies] [package.dependencies]
Django = ">=2.2" Django = ">=2.2"
[[package]]
name = "django3-cache-decorator"
version = "0.5.2"
description = "Easily add caching to functions within a django project."
category = "main"
optional = false
python-versions = "*"
[[package]] [[package]]
name = "djangorestframework" name = "djangorestframework"
version = "3.13.1" version = "3.13.1"
@ -1461,7 +1469,7 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>=
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "ebe98ffb0ca3eecc013a5d4ff58665bdbba2806da83d08ccb3acdbf03bda39b0" content-hash = "78d386497d4f46ead5da6c395a16ce50d9f9846cb20bf39bf2932bf5db5d4316"
[metadata.files] [metadata.files]
anyascii = [ anyascii = [
@ -1721,6 +1729,10 @@ django-treebeard = [
{file = "django-treebeard-4.5.1.tar.gz", hash = "sha256:80150017725239702054e5fa64dc66e383dc13ac262c8d47ee5a82cb005969da"}, {file = "django-treebeard-4.5.1.tar.gz", hash = "sha256:80150017725239702054e5fa64dc66e383dc13ac262c8d47ee5a82cb005969da"},
{file = "django_treebeard-4.5.1-py3-none-any.whl", hash = "sha256:7c2b1cdb1e9b46d595825186064a1228bc4d00dbbc186db5b0b9412357fba91c"}, {file = "django_treebeard-4.5.1-py3-none-any.whl", hash = "sha256:7c2b1cdb1e9b46d595825186064a1228bc4d00dbbc186db5b0b9412357fba91c"},
] ]
django3-cache-decorator = [
{file = "django3-cache-decorator-0.5.2.tar.gz", hash = "sha256:d41409aa18c5bd1a99d48632373336d9787cb94025807ff3161804044a9f8abe"},
{file = "django3_cache_decorator-0.5.2-py3-none-any.whl", hash = "sha256:c8dc082bad6fa0598b5c26b30b15062aff5072ce649202fb82db2b49bfa454b3"},
]
djangorestframework = [ djangorestframework = [
{file = "djangorestframework-3.13.1-py3-none-any.whl", hash = "sha256:24c4bf58ed7e85d1fe4ba250ab2da926d263cd57d64b03e8dcef0ac683f8b1aa"}, {file = "djangorestframework-3.13.1-py3-none-any.whl", hash = "sha256:24c4bf58ed7e85d1fe4ba250ab2da926d263cd57d64b03e8dcef0ac683f8b1aa"},
{file = "djangorestframework-3.13.1.tar.gz", hash = "sha256:0c33407ce23acc68eca2a6e46424b008c9c02eceb8cf18581921d0092bc1f2ee"}, {file = "djangorestframework-3.13.1.tar.gz", hash = "sha256:0c33407ce23acc68eca2a6e46424b008c9c02eceb8cf18581921d0092bc1f2ee"},

View file

@ -34,6 +34,7 @@ wagtail-2fa = "1.6.0"
django-health-check = "3.17.0" django-health-check = "3.17.0"
wagtail-autocomplete = {git = "https://github.com/wagtail/wagtail-autocomplete", rev = "03f46a0c256989690d0a43fc21fe0f37f9ede765"} wagtail-autocomplete = {git = "https://github.com/wagtail/wagtail-autocomplete", rev = "03f46a0c256989690d0a43fc21fe0f37f9ede765"}
Wand = "^0.6.10" Wand = "^0.6.10"
django3-cache-decorator = "^0.5.2"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]

View file

@ -1,5 +1,8 @@
from typing import Optional, Tuple
from django.db import models from django.db import models
from django.http.request import HttpRequest from django.http.request import HttpRequest
from django_cache_decorator import django_cache_decorator
from wagtail.admin.panels import FieldPanel from wagtail.admin.panels import FieldPanel
from wagtail.images import get_image_model_string from wagtail.images import get_image_model_string
from wagtail.images.models import Image from wagtail.images.models import Image
@ -9,6 +12,20 @@ from website.common.models import BasePage
from website.contrib.singleton_page.utils import SingletonPageCache from website.contrib.singleton_page.utils import SingletonPageCache
@django_cache_decorator(time=21600)
def get_latest_blog_post() -> Optional[Tuple[str, str]]:
from website.blog.models import BlogPostPage
try:
latest_blog_post = (
BlogPostPage.objects.live().public().defer_streamfields().latest("date")
)
except BlogPostPage.DoesNotExist:
return None
return latest_blog_post.title, latest_blog_post.get_url()
class HomePage(BasePage, WagtailImageMetadataMixin): class HomePage(BasePage, WagtailImageMetadataMixin):
max_count = 1 max_count = 1
@ -38,16 +55,9 @@ class HomePage(BasePage, WagtailImageMetadataMixin):
return "" return ""
def get_context(self, request: HttpRequest) -> dict: def get_context(self, request: HttpRequest) -> dict:
from website.blog.models import BlogPostPage
from website.search.models import SearchPage from website.search.models import SearchPage
context = super().get_context(request) context = super().get_context(request)
context["latest_blog_post"] = ( context["latest_blog_post"] = get_latest_blog_post()
BlogPostPage.objects.live()
.public()
.defer_streamfields()
.order_by("-date")
.first()
)
context["search_page_url"] = SingletonPageCache.get_url(SearchPage, request) context["search_page_url"] = SingletonPageCache.get_url(SearchPage, request)
return context return context

View file

@ -16,7 +16,7 @@
{% if latest_blog_post %} {% if latest_blog_post %}
<div class="box latest"> <div class="box latest">
<strong>Latest Post</strong>: <strong>Latest Post</strong>:
<a href="{% pageurl latest_blog_post %}">{{ latest_blog_post.title }}</a> <a href="{{ latest_blog_post.1 }}">{{ latest_blog_post.0 }}</a>
&rarr; &rarr;
</div> </div>
{% endif %} {% endif %}