Skip to content

Commit

Permalink
Merge pull request #37 from stuartmaxwell/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell authored May 12, 2024
2 parents b0f55c6 + 1cd1173 commit 37b25fa
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
18 changes: 18 additions & 0 deletions djpress/templatetags/djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django import template
from django.conf import settings
from django.db import models
from django.urls import reverse
from django.utils.safestring import mark_safe

from djpress.models import Category, Post

Expand Down Expand Up @@ -31,3 +33,19 @@ def get_single_published_post(slug: str) -> Post | None:
def get_blog_title() -> str:
"""Return the blog title."""
return settings.BLOG_TITLE


@register.simple_tag
def post_author_link(post: Post) -> str:
"""Return the author link for a post."""
if not settings.AUTHOR_PATH_ENABLED:
return post.author_display_name

author_url = reverse("djpress:author_posts", args=[post.author])

output = (
f'<a href="{author_url}" title="View all posts by '
f'{ post.author_display_name }">{ post.author_display_name }</a>'
)

return mark_safe(output)
55 changes: 55 additions & 0 deletions djpress/tests/test_djpress_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
from django.urls import reverse
from django.contrib.auth.models import User
from djpress.models import Post
from django.conf import settings

from djpress.templatetags import djpress_tags


@pytest.fixture
def user():
user = User.objects.create_user(
username="testuser",
password="testpass",
first_name="Test",
last_name="User",
)
return user


@pytest.fixture
def create_test_post(user):
post = Post.post_objects.create(
title="Test Post",
slug="test-post",
content="This is a test post.",
author=user,
status="published",
post_type="post",
)
return post


def test_get_blog_title():
assert djpress_tags.get_blog_title() == settings.BLOG_TITLE


@pytest.mark.django_db
def test_post_author_link_without_author_path(create_test_post):
settings.AUTHOR_PATH_ENABLED = False
assert (
djpress_tags.post_author_link(create_test_post)
== create_test_post.author_display_name
)


@pytest.mark.django_db
def test_post_author_link_with_author_path(create_test_post):
settings.AUTHOR_PATH_ENABLED = True
author_url = reverse("djpress:author_posts", args=[create_test_post.author])
expected_output = (
f'<a href="{author_url}" title="View all posts by '
f'{ create_test_post.author_display_name }">{ create_test_post.author_display_name }</a>'
)
assert djpress_tags.post_author_link(create_test_post) == expected_output
6 changes: 3 additions & 3 deletions templates/djpress/snippets/post_detail.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% load djpress_tags %}

<div class="card my-4 shadow">
<div class="card-body">
<h1>{{ post.title }}</h1>
Expand All @@ -12,9 +14,7 @@ <h1>{{ post.title }}</h1>
<a href="{% url "djpress:archives_posts" post.date.year|stringformat:"04d" post.date.month|stringformat:"02d" post.date.day|stringformat:"02d" %}" title="View all posts on this day">{{ post.date|date:"j" }}</a>,
<a href="{% url "djpress:archives_posts" post.date.year|stringformat:"04d" %}" title="View all posts in this year">{{ post.date|date:"Y" }}</a>,
{{ post.date|date:"g:i a" }}
by <a href="{% url "djpress:author_posts" post.author %}" title="View all posts by {{ post.author_display_name }}">
{{ post.author_display_name }}
</a>
by {% post_author_link post %}
</p>
<p>
Categories:
Expand Down

0 comments on commit 37b25fa

Please sign in to comment.