Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions bakerydemo/blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from django.test import TestCase
from wagtail.models import Site
from wagtail.test.utils import WagtailPageTests
from bakerydemo.blog.models import BlogIndexPage, BlogPage

class BlogPageTests(WagtailPageTests):
def setUp(self):
# Get the default site
self.site = Site.objects.get(is_default_site=True)
self.root = self.site.root_page

# Create a BlogIndexPage
self.blog_index = BlogIndexPage(
title="Blog Index",
slug="blog",
introduction="Welcome to the blog"
)
self.root.add_child(instance=self.blog_index)
self.blog_index.save_revision().publish()

# Create some BlogPages
self.blog_post = BlogPage(
title="First Post",
slug="first-post",
introduction="Intro",
body=[('paragraph', 'Content')],
date_published="2023-01-01"
)
self.blog_index.add_child(instance=self.blog_post)
self.blog_post.save_revision().publish()

# Add a tag to the post
self.blog_post.tags.add("bread")
self.blog_post.save_revision().publish()

def test_blog_index_page_renders(self):
"""Test that the blog index page renders correctly"""
response = self.client.get(self.blog_index.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'blog/blog_index_page.html')
self.assertContains(response, "First Post")

def test_tag_filtering(self):
"""Test that filtering by tag works"""
# Create a post without the tag
other_post = BlogPage(
title="Other Post",
slug="other-post",
introduction="Intro",
body=[('paragraph', 'Content')],
date_published="2023-01-02"
)
self.blog_index.add_child(instance=other_post)
other_post.save_revision().publish()

# url for tag filter
url = self.blog_index.url + self.blog_index.reverse_subpage('tag_archive', args=('bread',))

response = self.client.get(url)
self.assertEqual(response.status_code, 200)

# Should contain the tagged post
self.assertContains(response, "First Post")
# Should NOT contain the untagged post
self.assertNotContains(response, "Other Post")

def test_get_posts_method(self):
from taggit.models import Tag
# Directly test the get_posts method logic
self.assertIn(self.blog_post, self.blog_index.get_posts())

# Test filtering
tag = Tag.objects.get(slug="bread")
tagged_posts = self.blog_index.get_posts(tag=tag)
self.assertIn(self.blog_post, tagged_posts)
79 changes: 40 additions & 39 deletions bakerydemo/templates/blog/blog_index_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,52 @@
{% load wagtailcore_tags navigation_tags wagtailimages_tags wagtail_cache %}

{% if tag %}
{% block title %}
{% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}: {{ tag }}
{% endblock %}
{% block title %}
{% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}: {{ tag }}
{% endblock %}

{% block search_description %}Viewing all blog posts sorted by the tag {{ tag }}{% endblock %}
{% block search_description %}Viewing all blog posts sorted by the tag {{ tag }}{% endblock %}
{% endif %}

{% block content %}
{% if not tag %}
{% include "base/include/header-index.html" %}
{% endif %}
{% if not tag %}
{% include "base/include/header-index.html" %}
{% endif %}

<div class="container">
{% if tag %}
<div class="row">
<div class="col-md-12">
<h1 class="index-header__title">Blog</h1>
</div>
<div class="col-md-12">
<p class="index-header__introduction">Viewing all blog posts sorted by the tag <span class="blog-tags__tag">{{ tag }}</span>.</p>
</div>
</div>
{% endif %}
<div class="container">
{% if tag %}
<div class="row">
<div class="col-md-12">
<h1 class="index-header__title">Blog</h1>
</div>
<div class="col-md-12">
<p class="index-header__introduction">Viewing all blog posts sorted by the tag <span
class="blog-tags__tag">{{ tag }}</span>.</p>
</div>
</div>
{% endif %}

{% if page.get_child_tags %}
<ul class="blog-tags">
<li><span class="blog-tags__pill blog-tags__pill--selected">All</span></li>
{% for tag in page.get_child_tags %}
<li><a class="blog-tags__pill" aria-label="Filter by tag name {{ tag }}" href="{{ tag.url }}">{{ tag }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% if page.get_child_tags %}
<ul class="blog-tags">
<li><span class="blog-tags__pill blog-tags__pill--selected">All</span></li>
{% for tag in page.get_child_tags %}
<li><a class="blog-tags__pill" aria-label="Filter by tag name {{ tag }}" href="{{ tag.url }}">{{ tag }}</a></li>
{% endfor %}
</ul>
{% endif %}

<div class="blog-list">
{% if posts %}
{% wagtailcache 500 posts %}
{% for blog in posts %}
{% include "includes/card/blog-listing-card.html" %}
{% endfor %}
{% endwagtailcache %}
{% else %}
<div class="col-md-12">
<p>Oh, snap. Looks like we were too busy baking to write any blog posts. Sorry.</p>
</div>
{% endif %}
<div class="blog-list">
{% if posts %}
{% wagtailcache 500 posts tag %}
{% for blog in posts %}
{% include "includes/card/blog-listing-card.html" %}
{% endfor %}
{% endwagtailcache %}
{% else %}
<div class="col-md-12">
<p>Oh, snap. Looks like we were too busy baking to write any blog posts. Sorry.</p>
</div>
{% endif %}
</div>
{% endblock content %}
</div>
{% endblock content %}