Add category taxonomy and navigation integration
Implements Issue #35 with category snippets, article category routing, category-aware templates, and category RSS feeds with tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -4,7 +4,7 @@ from django import template
|
||||
from django.utils.safestring import mark_safe
|
||||
from wagtail.models import Site
|
||||
|
||||
from apps.blog.models import TagMetadata
|
||||
from apps.blog.models import ArticleIndexPage, TagMetadata
|
||||
from apps.core.models import SiteSettings
|
||||
from apps.legal.models import LegalPage
|
||||
|
||||
@@ -46,6 +46,30 @@ def get_social_links(context):
|
||||
return list(settings.social_links.all())
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def get_categories_nav(context):
|
||||
request = context.get("request")
|
||||
if not request:
|
||||
return []
|
||||
site = Site.find_for_request(request) if request else None
|
||||
index_qs = ArticleIndexPage.objects.live().public()
|
||||
if site:
|
||||
index_qs = index_qs.in_site(site)
|
||||
index_page = index_qs.first()
|
||||
if not index_page:
|
||||
return []
|
||||
categories = index_page.get_listing_context(request, active_category=None)["available_categories"].filter(show_in_nav=True)
|
||||
return [
|
||||
{
|
||||
"name": category.name,
|
||||
"slug": category.slug,
|
||||
"url": index_page.get_category_url(category),
|
||||
"article_count": index_page.get_articles().filter(category=category).count(),
|
||||
}
|
||||
for category in categories
|
||||
]
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
@register.filter
|
||||
def get_tag_css(tag):
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import pytest
|
||||
|
||||
from apps.blog.models import ArticleIndexPage, ArticlePage, Category
|
||||
from apps.blog.tests.factories import AuthorFactory
|
||||
from apps.legal.models import LegalIndexPage, LegalPage
|
||||
|
||||
|
||||
@@ -13,3 +15,25 @@ def test_get_legal_pages_tag(client, home_page):
|
||||
|
||||
resp = client.get("/")
|
||||
assert resp.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_categories_nav_tag_renders_category_link(client, home_page):
|
||||
index = ArticleIndexPage(title="Articles", slug="articles")
|
||||
home_page.add_child(instance=index)
|
||||
category = Category.objects.create(name="Reviews", slug="reviews", show_in_nav=True)
|
||||
author = AuthorFactory()
|
||||
article = ArticlePage(
|
||||
title="R1",
|
||||
slug="r1",
|
||||
author=author,
|
||||
summary="summary",
|
||||
body=[("rich_text", "<p>body</p>")],
|
||||
category=category,
|
||||
)
|
||||
index.add_child(instance=article)
|
||||
article.save_revision().publish()
|
||||
|
||||
resp = client.get("/")
|
||||
assert resp.status_code == 200
|
||||
assert "/articles/category/reviews/" in resp.content.decode()
|
||||
|
||||
Reference in New Issue
Block a user