From 6600b9ccb70b46adbc489fbe7dff1462955df472 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 19 Jun 2022 20:13:19 +0100 Subject: [PATCH] Build ToC from python --- website/common/models.py | 8 ++++- website/common/templates/common/hero.html | 35 +++++++++++-------- website/common/templates/common/toc-item.html | 10 ++++++ website/common/utils.py | 24 ++++++++++++- 4 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 website/common/templates/common/toc-item.html diff --git a/website/common/models.py b/website/common/models.py index 1d9ef92..d8068d2 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -2,11 +2,13 @@ from typing import Any from django.db import models from django.http.request import HttpRequest -from django.utils.functional import classproperty +from django.utils.functional import cached_property, classproperty from wagtail.admin.panels import FieldPanel from wagtail.images import get_image_model_string from wagtail.models import Page +from .utils import TocEntry, get_table_of_contents + class BasePage(Page): show_in_menus_default = True @@ -26,6 +28,10 @@ class BasePage(Page): """ return self.get_ancestors().reverse().exclude(depth__lte=2) + @cached_property + def table_of_contents(self) -> list[TocEntry]: + return get_table_of_contents() + class BaseContentMixin(models.Model): subtitle = models.CharField(max_length=255, blank=True) diff --git a/website/common/templates/common/hero.html b/website/common/templates/common/hero.html index 06a380a..b7c9b6e 100644 --- a/website/common/templates/common/hero.html +++ b/website/common/templates/common/hero.html @@ -16,23 +16,28 @@ {% endif %}