From 23b8454fe4db4932684036147133b77a968a1bdc Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 2 Oct 2022 17:27:55 +0100 Subject: [PATCH] Add template caching for listing components --- pyproject.toml | 2 + .../templates/common/content-details.html | 63 ++++++++++--------- .../common/templates/common/listing-item.html | 6 +- website/common/templatetags/util_tags.py | 7 +++ 4 files changed, 45 insertions(+), 33 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 48f29ee..591105f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,6 +68,8 @@ exclude = ''' | referrallink_snippet_link\.html | onlineaccount_snippet_link\.html | 500\.html + | content-details\.html + | listing-item\.html ) ''' diff --git a/website/common/templates/common/content-details.html b/website/common/templates/common/content-details.html index 1748867..114c25c 100644 --- a/website/common/templates/common/content-details.html +++ b/website/common/templates/common/content-details.html @@ -1,35 +1,36 @@ -{% load wagtailcore_tags humanize_tags %} +{% load wagtailcore_tags humanize_tags cache util_tags %} -
- - {% if page.date %} - - - +{% cache 600|jitter:60 "content-details" page.id request.is_preview %} +
+ {% if page.date %} + + + + + {{ page.date|date:"Y-m-d" }} - {{ page.date|date:"Y-m-d" }} - - {% endif %} + {% endif %} - {% if page.show_reading_time %} -
- - - - {{ page.reading_time|naturaldelta }} -
- {% endif %} + {% if page.show_reading_time %} +
+ + + + {{ page.reading_time|naturaldelta }} +
+ {% endif %} - {% if page.tags.all %} -
- - - - - - {% for tag in page.tags.all %} - #{{ tag.slug }} - {% endfor %} -
- {% endif %} -
+ {% if page.tags.all %} +
+ + + + + + {% for tag in page.tags.all %} + #{{ tag.slug }} + {% endfor %} +
+ {% endif %} +
+{% endcache %} diff --git a/website/common/templates/common/listing-item.html b/website/common/templates/common/listing-item.html index 982b7ee..c986dff 100644 --- a/website/common/templates/common/listing-item.html +++ b/website/common/templates/common/listing-item.html @@ -1,4 +1,4 @@ -{% load wagtailcore_tags %} +{% load wagtailcore_tags cache util_tags %}
@@ -13,7 +13,9 @@

{{ page.title }}

{% include "common/content-details.html" %} -

{{ page.summary }}

+ {% cache 900|jitter:60 "summary" page.id request.is_preview %} +

{{ page.summary }}

+ {% endcache %}
diff --git a/website/common/templatetags/util_tags.py b/website/common/templatetags/util_tags.py index bc7e1cd..06e4b82 100644 --- a/website/common/templatetags/util_tags.py +++ b/website/common/templatetags/util_tags.py @@ -1,3 +1,5 @@ +import random + from django.template import Library from django.utils.encoding import force_str from wagtail.models import Page @@ -18,6 +20,11 @@ def pagefullurl(context: dict, page: Page) -> str: return page.get_full_url(context["request"]) +@register.filter() +def jitter(original: float, jitter: float) -> float: + return random.uniform(original + jitter, original - jitter) + + @register.filter() def extract_text(html: str | RichText) -> str: return utils.extract_text(force_str(html))