Skip to content

Commit

Permalink
Merge pull request #39 from stuartmaxwell/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell authored May 12, 2024
2 parents 0c8b54d + 8e88e58 commit e6029d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 5 additions & 2 deletions djpress/templatetags/djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ def get_blog_title() -> str:


@register.simple_tag
def post_author_link(post: Post) -> str:
def post_author_link(post: Post, link_class: str = "") -> 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])

link_class_html = f' class="{link_class}"' if link_class else ""

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

return mark_safe(output)
Expand Down
29 changes: 28 additions & 1 deletion djpress/tests/test_djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def category():


@pytest.fixture
def create_test_post(user):
def create_test_post(user, category):
post = Post.post_objects.create(
title="Test Post",
slug="test-post",
Expand Down Expand Up @@ -65,6 +65,33 @@ def test_post_author_link_with_author_path(create_test_post):
assert djpress_tags.post_author_link(create_test_post) == expected_output


@pytest.mark.django_db
def test_post_author_link_with_author_path_with_one_link_class(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 }" class="class1">'
f"{ create_test_post.author_display_name }</a>"
)
assert djpress_tags.post_author_link(create_test_post, "class1") == expected_output


@pytest.mark.django_db
def test_post_author_link_with_author_path_with_two_link_class(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 }" class="class1 class2">'
f"{ create_test_post.author_display_name }</a>"
)
assert (
djpress_tags.post_author_link(create_test_post, "class1 class2")
== expected_output
)


@pytest.mark.django_db
def test_post_category_link_without_category_path(category):
settings.CATEGORY_PATH_ENABLED = False
Expand Down

0 comments on commit e6029d3

Please sign in to comment.