diff --git a/djpress/templates/djpress/base.html b/djpress/templates/djpress/base.html deleted file mode 100644 index b3a81da..0000000 --- a/djpress/templates/djpress/base.html +++ /dev/null @@ -1,32 +0,0 @@ -{% load static %} -{% load djpress_tags %} - - - - - - - {% blog_page_title post_text="| " %}{% blog_title %} - - - -
-

{% blog_title_link %}

- {% blog_pages %} -
- -
- - {% block main %}{% endblock main %} - -
- - - - diff --git a/djpress/templates/djpress/index.html b/djpress/templates/djpress/index.html index f8924ad..faecdef 100644 --- a/djpress/templates/djpress/index.html +++ b/djpress/templates/djpress/index.html @@ -1,49 +1,72 @@ -{% extends "djpress/base.html" %} +{% load static %} {% load djpress_tags %} -{% block main %} + + + + + + {% blog_page_title post_text="| " %}{% blog_title %} + + + +
+

{% blog_title_link %}

+ {% blog_pages %} +
- {% category_name "h1" pre_text="View Posts in the " post_text=" Category" %} - {% author_name "h1" pre_text="View Posts by " %} +
- {% if post %} + {% category_name "h1" pre_text="View Posts in the " post_text=" Category" %} + {% author_name "h1" pre_text="View Posts by " %} -
-
-

{% post_title_link %}

-

By {% post_author_link %}. {% post_date_link %}

-
-
- {% post_content %} -
-
-

Categories: {% post_categories_link "span" "badge" %}

-
-
+ {% if post %} - {% else %} +
+
+

{% post_title_link %}

+

By {% post_author_link %}. {% post_date_link %}

+
+
+ {% post_content %} +
+
+

Categories: {% post_categories_link "span" "badge" %}

+
+
- {% for post in posts %} + {% else %} -
-
-

{% post_title_link %}

-

By {% post_author_link %}. {% post_date_link %}

-
-
- {% post_content %} -
-
+ {% for post in posts %} - {% empty %} +
+
+

{% post_title_link %}

+

By {% post_author_link %}. {% post_date_link %}

+
+
+ {% post_content %} +
+
-

No posts available.

+ {% empty %} - {% endfor %} +

No posts available.

- {% endif %} + {% endfor %} + {% endif %} - {% posts_nav_links %} + {% pagination_links %} -{% endblock main %} +
+ + + + diff --git a/djpress/templates/djpress/single.html b/djpress/templates/djpress/single.html deleted file mode 100644 index 66572d8..0000000 --- a/djpress/templates/djpress/single.html +++ /dev/null @@ -1,21 +0,0 @@ -{% extends "djpress/base.html" %} -{% load djpress_tags %} - -{% block main %} - -
-
-

{% post_title_link %}

-

By {% post_author_link %}. {% post_date_link %}

-
-
- {% post_content %} -
- -
- - {% posts_nav_links %} - -{% endblock main %} diff --git a/djpress/templatetags/djpress_tags.py b/djpress/templatetags/djpress_tags.py index 7fd67ff..c0d44a8 100644 --- a/djpress/templatetags/djpress_tags.py +++ b/djpress/templatetags/djpress_tags.py @@ -577,7 +577,58 @@ def post_categories_link( @register.simple_tag(takes_context=True) -def posts_nav_links( +def is_paginated(context: Context) -> bool: + """Return whether the posts are paginated. + + Args: + context: The context. + + Returns: + bool: Whether the posts are paginated. + """ + posts: Page | None = context.get("posts") + if not posts or not isinstance(posts, Page): + return False + + return True + + +@register.simple_tag(takes_context=True) +def get_pagination_range(context: Context) -> range: + """Return the range of pagination pages. + + Args: + context: The context. + + Returns: + range: The pagination pages. + """ + page: Page | None = context.get("posts") + if not page or not isinstance(page, Page): + return range(0) + + return page.paginator.page_range + + +@register.simple_tag(takes_context=True) +def get_pagination_current_page(context: Context) -> int: + """Return the current page number. + + Args: + context: The context. + + Returns: + int: The current page number. + """ + page: Page | None = context.get("posts") + if not page or not isinstance(page, Page): + return 0 + + return page.number + + +@register.simple_tag(takes_context=True) +def pagination_links( context: Context, ) -> str: """Return the previous and next post links. diff --git a/pyproject.toml b/pyproject.toml index bc9f48b..bcf2f80 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "djpress" -version = "0.6.0" +version = "0.7.0" authors = [{ name = "Stuart Maxwell", email = "stuart@amanzi.nz" }] description = "A blog application for Django sites, inspired by classic WordPress." readme = "README.md" diff --git a/tests/test_templatetags_djpress_tags.py b/tests/test_templatetags_djpress_tags.py index 0663db2..b99793a 100644 --- a/tests/test_templatetags_djpress_tags.py +++ b/tests/test_templatetags_djpress_tags.py @@ -878,15 +878,111 @@ def test_blog_page_title(test_post1, test_page1): assert "" == djpress_tags.blog_page_title(context) -def test_posts_nav_links_no_posts(): +@pytest.mark.django_db +def test_is_paginated(test_post1, test_post2, test_long_post1): + # Confirm settings are set according to settings_testing.py + assert settings.RECENT_PUBLISHED_POSTS_COUNT == 3 + + # Test case 1 - no paginator in context context = Context() + assert djpress_tags.is_paginated(context) == False + + # Test case 2 - paginator in context + posts = Paginator( + Post.post_objects.get_published_posts(), + settings.RECENT_PUBLISHED_POSTS_COUNT, + ) + page = posts.get_page(number=None) + context = Context({"posts": page}) + assert djpress_tags.is_paginated(context) == True + + +def test_pagination_links_no_posts(): + context = Context() + + assert djpress_tags.pagination_links(context) == "" + assert type(djpress_tags.pagination_links(context)) == str + + +@pytest.mark.django_db +def test_get_pagination_range(test_post1, test_post2, test_long_post1): + # Test case 1 - 1 page, i.e. 3 posts with 3 posts per page + # Confirm settings are set according to settings_testing.py + assert settings.RECENT_PUBLISHED_POSTS_COUNT == 3 + posts = Paginator( + Post.post_objects.get_published_posts(), + settings.RECENT_PUBLISHED_POSTS_COUNT, + ) + page = posts.get_page(number=None) + context = Context({"posts": page}) + assert djpress_tags.get_pagination_range(context) == range(1, 2) - assert djpress_tags.posts_nav_links(context) == "" - assert type(djpress_tags.posts_nav_links(context)) == str + # Test case 2 - 2 pages, i.e. 3 posts with 2 posts per page + settings.set("RECENT_PUBLISHED_POSTS_COUNT", 2) + posts = Paginator( + Post.post_objects.get_published_posts(), + settings.RECENT_PUBLISHED_POSTS_COUNT, + ) + page = posts.get_page(number=None) + context = Context({"posts": page}) + assert settings.RECENT_PUBLISHED_POSTS_COUNT == 2 + assert djpress_tags.get_pagination_range(context) == range(1, 3) + + # Test case 3 - 3 pages, i.e. 3 posts with 1 posts per page + settings.set("RECENT_PUBLISHED_POSTS_COUNT", 1) + assert settings.RECENT_PUBLISHED_POSTS_COUNT == 1 + posts = Paginator( + Post.post_objects.get_published_posts(), + settings.RECENT_PUBLISHED_POSTS_COUNT, + ) + page = posts.get_page(number=None) + context = Context({"posts": page}) + assert djpress_tags.get_pagination_range(context) == range(1, 4) + + # Set back to defaults + settings.set("RECENT_PUBLISHED_POSTS_COUNT", 3) + assert settings.RECENT_PUBLISHED_POSTS_COUNT == 3 + + +@pytest.mark.django_db +def test_get_pagination_current_page(test_post1, test_post2, test_long_post1): + # Confirm settings are set according to settings_testing.py + assert settings.RECENT_PUBLISHED_POSTS_COUNT == 3 + + settings.set("RECENT_PUBLISHED_POSTS_COUNT", 1) + assert settings.RECENT_PUBLISHED_POSTS_COUNT == 1 + + # Test case 1 - no page number + posts = Paginator( + Post.post_objects.get_published_posts(), + settings.RECENT_PUBLISHED_POSTS_COUNT, + ) + page = posts.get_page(number=None) + context = Context({"posts": page}) + assert djpress_tags.get_pagination_current_page(context) == 1 + + # Test case 2 - page number 1 + page = posts.get_page(number=1) + context = Context({"posts": page}) + assert djpress_tags.get_pagination_current_page(context) == 1 + + # Test case 3 - page number 2 + page = posts.get_page(number=2) + context = Context({"posts": page}) + assert djpress_tags.get_pagination_current_page(context) == 2 + + # Test case 4 - page number 3 + page = posts.get_page(number=3) + context = Context({"posts": page}) + assert djpress_tags.get_pagination_current_page(context) == 3 + + # Set back to defaults + settings.set("RECENT_PUBLISHED_POSTS_COUNT", 3) + assert settings.RECENT_PUBLISHED_POSTS_COUNT == 3 @pytest.mark.django_db -def test_posts_nav_links_one_page(test_post1, test_post2, test_long_post1): +def test_pagination_links_one_page(test_post1, test_post2, test_long_post1): # Confirm settings are set according to settings_testing.py assert settings.RECENT_PUBLISHED_POSTS_COUNT == 3 @@ -908,11 +1004,11 @@ def test_posts_nav_links_one_page(test_post1, test_post2, test_long_post1): expected_output = f'' - assert djpress_tags.posts_nav_links(context) == expected_output + assert djpress_tags.pagination_links(context) == expected_output @pytest.mark.django_db -def test_posts_nav_links_two_pages(test_post1, test_post2, test_long_post1): +def test_pagination_links_two_pages(test_post1, test_post2, test_long_post1): # Confirm settings are set according to settings_testing.py assert settings.RECENT_PUBLISHED_POSTS_COUNT == 3 @@ -945,7 +1041,7 @@ def test_posts_nav_links_two_pages(test_post1, test_post2, test_long_post1): expected_output = f'' - assert djpress_tags.posts_nav_links(context) == expected_output + assert djpress_tags.pagination_links(context) == expected_output # Test case 2 - first page with page number 1 page = posts.get_page(number=1) @@ -967,7 +1063,7 @@ def test_posts_nav_links_two_pages(test_post1, test_post2, test_long_post1): expected_output = f'' - assert djpress_tags.posts_nav_links(context) == expected_output + assert djpress_tags.pagination_links(context) == expected_output # Test case 3 - second page with page number 2 page = posts.get_page(number=2) @@ -989,7 +1085,7 @@ def test_posts_nav_links_two_pages(test_post1, test_post2, test_long_post1): expected_output = f'' - assert djpress_tags.posts_nav_links(context) == expected_output + assert djpress_tags.pagination_links(context) == expected_output # Set back to defaults settings.set("RECENT_PUBLISHED_POSTS_COUNT", 3) @@ -997,7 +1093,7 @@ def test_posts_nav_links_two_pages(test_post1, test_post2, test_long_post1): @pytest.mark.django_db -def test_posts_nav_links_three_pages(test_post1, test_post2, test_long_post1): +def test_pagination_links_three_pages(test_post1, test_post2, test_long_post1): # Confirm settings are set according to settings_testing.py assert settings.RECENT_PUBLISHED_POSTS_COUNT == 3 @@ -1030,7 +1126,7 @@ def test_posts_nav_links_three_pages(test_post1, test_post2, test_long_post1): expected_output = f'' - assert djpress_tags.posts_nav_links(context) == expected_output + assert djpress_tags.pagination_links(context) == expected_output # Test case 2 - first page with page number 1 page = posts.get_page(number=1) @@ -1052,7 +1148,7 @@ def test_posts_nav_links_three_pages(test_post1, test_post2, test_long_post1): expected_output = f'' - assert djpress_tags.posts_nav_links(context) == expected_output + assert djpress_tags.pagination_links(context) == expected_output # Test case 3 - first page with page number 2 page = posts.get_page(number=2) @@ -1079,7 +1175,7 @@ def test_posts_nav_links_three_pages(test_post1, test_post2, test_long_post1): expected_output = f'' - assert djpress_tags.posts_nav_links(context) == expected_output + assert djpress_tags.pagination_links(context) == expected_output # Test case 4 - first page with page number 3 page = posts.get_page(number=3) @@ -1101,7 +1197,7 @@ def test_posts_nav_links_three_pages(test_post1, test_post2, test_long_post1): expected_output = f'' - assert djpress_tags.posts_nav_links(context) == expected_output + assert djpress_tags.pagination_links(context) == expected_output # Set back to defaults settings.set("RECENT_PUBLISHED_POSTS_COUNT", 3)