Fix issues with public tags in templates

This commit is contained in:
Jake Howard 2024-02-06 18:35:40 +00:00
parent 4c600651b6
commit 0424c2dba2
Signed by: jake
GPG Key ID: 57AFB45680EDD477
3 changed files with 19 additions and 4 deletions

View File

@ -7,6 +7,7 @@ from django.utils import timezone
from django.utils.functional import cached_property from django.utils.functional import cached_property
from modelcluster.fields import ParentalManyToManyField from modelcluster.fields import ParentalManyToManyField
from wagtail.admin.panels import FieldPanel from wagtail.admin.panels import FieldPanel
from wagtail.models import PageQuerySet
from wagtail.search import index from wagtail.search import index
from wagtailautocomplete.edit_handlers import AutocompletePanel from wagtailautocomplete.edit_handlers import AutocompletePanel
@ -61,6 +62,19 @@ class BlogPostPage(BaseContentPage):
def tag_list_page_url(self) -> Optional[str]: def tag_list_page_url(self) -> Optional[str]:
return SingletonPageCache.get_url(BlogPostTagListPage) 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 @cached_property
def blog_post_list_page_url(self) -> Optional[str]: def blog_post_list_page_url(self) -> Optional[str]:
return SingletonPageCache.get_url(BlogPostListPage) return SingletonPageCache.get_url(BlogPostListPage)

View File

@ -20,14 +20,14 @@
</div> </div>
{% endif %} {% endif %}
{% if page.tags.public.live.all %} {% if page.tags_list %}
<div class="icon-text is-family-code"> <div class="icon-text is-family-code">
<span class="icon"> <span class="icon">
<a href="{{ page.tag_list_page_url }}" title="View all tags"> <a href="{{ page.tag_list_page_url }}" title="View all tags">
<i class="fas fa-lg fa-tags"></i> <i class="fas fa-lg fa-tags"></i>
</a> </a>
</span> </span>
{% for tag in page.tags.public.live.all|dictsort:"slug" %} {% for tag in page.tags_list %}
<span><a title="{{ tag.name }}" href="{% pageurl tag %}">#{{ tag.slug }}</a></span> <span><a title="{{ tag.name }}" href="{% pageurl tag %}">#{{ tag.slug }}</a></span>
{% endfor %} {% endfor %}
</div> </div>

View File

@ -15,6 +15,7 @@ from wagtail.query import PageQuerySet
from wagtail_favicon.models import FaviconSettings from wagtail_favicon.models import FaviconSettings
from wagtail_favicon.utils import get_rendition_url from wagtail_favicon.utils import get_rendition_url
from website.blog.models import BlogPostPage
from website.common.utils import get_site_title from website.common.utils import get_site_title
from website.contrib.singleton_page.utils import SingletonPageCache from website.contrib.singleton_page.utils import SingletonPageCache
from website.home.models import HomePage from website.home.models import HomePage
@ -126,8 +127,8 @@ class AllPagesFeed(Feed):
return getattr(item, "summary", None) or item.title return getattr(item, "summary", None) or item.title
def item_categories(self, item: BasePage) -> Optional[list[str]]: def item_categories(self, item: BasePage) -> Optional[list[str]]:
if tags := getattr(item, "tags", None): if isinstance(item, BlogPostPage):
return tags.public().live().order_by("slug").values_list("slug", flat=True) return item.tags_list.values_list("slug", flat=True)
return None return None
def item_enclosure_url(self, item: BasePage) -> Optional[str]: def item_enclosure_url(self, item: BasePage) -> Optional[str]: