From 78660500a53accb7c06b429282ddb1f228253a1f Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 16 Jul 2022 00:21:10 +0100 Subject: [PATCH] Add tag filtered view to blog list --- static/src/scss/_content.scss | 11 ++++++++ website/blog/models.py | 13 ++++++++++ .../blog/templates/blog/blog_list_page.html | 26 +++++++++++++++---- .../templates/common/content-details.html | 4 ++- website/common/templates/common/hero.html | 2 +- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/static/src/scss/_content.scss b/static/src/scss/_content.scss index d2038f4..6e42195 100644 --- a/static/src/scss/_content.scss +++ b/static/src/scss/_content.scss @@ -41,3 +41,14 @@ section.content { max-width: calc(99% - ($column-gap * 2)); } } + +.blog-filter { + &, + .tag { + font-size: 1.25rem; + } + + .tag { + font-family: $family-code; + } +} diff --git a/website/blog/models.py b/website/blog/models.py index 32c8c19..c5b9b5c 100644 --- a/website/blog/models.py +++ b/website/blog/models.py @@ -57,6 +57,12 @@ class BlogListPage(BaseContentMixin, BasePage): # type: ignore[misc] .prefetch_related("tags") .order_by("-date") ) + if tag := request.GET.get("tag"): + tag = BlogPostTag.objects.filter(slug=tag).first() + if tag: + context["filtering_by_tag"] = tag + context["no_table_of_contents"] = True + context["child_pages"] = context["child_pages"].filter(tags=tag) return context @@ -69,6 +75,13 @@ class BlogPostTag(TagBase): verbose_name = "blog tag" verbose_name_plural = "blog tags" + def get_absolute_url(self) -> str: + return ( + BlogListPage.objects.live().defer_streamfields().first().get_url() # type: ignore[attr-defined] + + "?tag=" + + self.slug + ) + class TaggedBlog(ItemBase): tag = models.ForeignKey( diff --git a/website/blog/templates/blog/blog_list_page.html b/website/blog/templates/blog/blog_list_page.html index 379dcd6..a56f7a8 100644 --- a/website/blog/templates/blog/blog_list_page.html +++ b/website/blog/templates/blog/blog_list_page.html @@ -1,16 +1,32 @@ {% extends "wagtail_base.html" %} +{% load wagtailcore_tags %} + {% block content %} {% include "common/hero.html" %} +{% if filtering_by_tag %} +
+

+ Showing only: + + #{{ filtering_by_tag.name }} + + +

+
+{% endif %} +
{% for page in child_pages %} - {% ifchanged %} - - {% endifchanged %} + {% if not filtering_by_tag %} + {% ifchanged %} + + {% endifchanged %} + {% endif %} {% include "common/listing-item.html" %} {% endfor %} diff --git a/website/common/templates/common/content-details.html b/website/common/templates/common/content-details.html index 0e056c4..40d0ecc 100644 --- a/website/common/templates/common/content-details.html +++ b/website/common/templates/common/content-details.html @@ -1,3 +1,5 @@ + +
{% if page.date %} @@ -23,7 +25,7 @@
{% for tag in page.tags.all %} - #{{ tag.slug }} + #{{ tag.slug }} {% endfor %}
diff --git a/website/common/templates/common/hero.html b/website/common/templates/common/hero.html index 1891bbd..6b897b0 100644 --- a/website/common/templates/common/hero.html +++ b/website/common/templates/common/hero.html @@ -16,7 +16,7 @@ {% include "common/content-details.html" %}