From 0424c2dba2f04da85738a4fbe4f1459796c668d6 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 6 Feb 2024 18:35:40 +0000 Subject: [PATCH] Fix issues with public tags in templates --- website/blog/models.py | 14 ++++++++++++++ .../common/templates/common/content-details.html | 4 ++-- website/common/views.py | 5 +++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/website/blog/models.py b/website/blog/models.py index 5615841..4e41a86 100644 --- a/website/blog/models.py +++ b/website/blog/models.py @@ -7,6 +7,7 @@ from django.utils import timezone from django.utils.functional import cached_property from modelcluster.fields import ParentalManyToManyField from wagtail.admin.panels import FieldPanel +from wagtail.models import PageQuerySet from wagtail.search import index from wagtailautocomplete.edit_handlers import AutocompletePanel @@ -61,6 +62,19 @@ class BlogPostPage(BaseContentPage): def tag_list_page_url(self) -> Optional[str]: return SingletonPageCache.get_url(BlogPostTagListPage) + @cached_property + def tags_list(self) -> models.QuerySet: + """ + Use this to get a page's tags. + """ + tags = self.tags.order_by("slug") + + # In drafts, `django-modelcluster` doesn't support these filters + if isinstance(tags, PageQuerySet): + return tags.public().live() + + return tags + @cached_property def blog_post_list_page_url(self) -> Optional[str]: return SingletonPageCache.get_url(BlogPostListPage) diff --git a/website/common/templates/common/content-details.html b/website/common/templates/common/content-details.html index 4f84a6f..f57a57b 100644 --- a/website/common/templates/common/content-details.html +++ b/website/common/templates/common/content-details.html @@ -20,14 +20,14 @@ {% endif %} - {% if page.tags.public.live.all %} + {% if page.tags_list %}
- {% for tag in page.tags.public.live.all|dictsort:"slug" %} + {% for tag in page.tags_list %} #{{ tag.slug }} {% endfor %}
diff --git a/website/common/views.py b/website/common/views.py index 36ffa91..a706c69 100644 --- a/website/common/views.py +++ b/website/common/views.py @@ -15,6 +15,7 @@ from wagtail.query import PageQuerySet from wagtail_favicon.models import FaviconSettings from wagtail_favicon.utils import get_rendition_url +from website.blog.models import BlogPostPage from website.common.utils import get_site_title from website.contrib.singleton_page.utils import SingletonPageCache from website.home.models import HomePage @@ -126,8 +127,8 @@ class AllPagesFeed(Feed): return getattr(item, "summary", None) or item.title def item_categories(self, item: BasePage) -> Optional[list[str]]: - if tags := getattr(item, "tags", None): - return tags.public().live().order_by("slug").values_list("slug", flat=True) + if isinstance(item, BlogPostPage): + return item.tags_list.values_list("slug", flat=True) return None def item_enclosure_url(self, item: BasePage) -> Optional[str]: