From 197ab55a44306d29d34cda5751d700bc0e52d028 Mon Sep 17 00:00:00 2001 From: Stuart Maxwell Date: Mon, 13 May 2024 22:02:00 +1200 Subject: [PATCH 01/10] Remove unused setting --- config/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/settings.py b/config/settings.py index e3ef1ae..8681c30 100644 --- a/config/settings.py +++ b/config/settings.py @@ -247,10 +247,10 @@ ARCHIVES_PATH_ENABLED: bool = True ARCHIVES_PATH: str = "archives" DATE_ARCHIVES_ENABLED: bool = True -DATE_ARCHIVES: bool = True RSS_ENABLED: bool = True RSS_PATH: str = "rss" +# The following are used to generate the post permalink DAY_SLUG: str = "%Y/%m/%d" MONTH_SLUG: str = "%Y/%m" YEAR_SLUG: str = "%Y" From 5a5f33230cc1e5ce57f7d4e9b6ad6e206878a310 Mon Sep 17 00:00:00 2001 From: Stuart Maxwell Date: Mon, 13 May 2024 22:02:27 +1200 Subject: [PATCH 02/10] Create vscode launch.json file --- .vscode/launch.json | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..422162b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Django", + "type": "debugpy", + "request": "launch", + "args": [ + "runserver" + ], + "django": true, + "autoStartBrowser": false, + "program": "${workspaceFolder}/manage.py" + } + ] +} From a240ce4fcba6786b16c532a45ffb0f45fe7742aa Mon Sep 17 00:00:00 2001 From: Stuart Maxwell Date: Mon, 13 May 2024 22:03:19 +1200 Subject: [PATCH 03/10] Create post_date_link template tag --- djpress/templatetags/djpress_tags.py | 52 ++++++++++++++ djpress/tests/test_djpress_tags.py | 78 +++++++++++++++++++++ templates/djpress/snippets/post_detail.html | 6 +- 3 files changed, 131 insertions(+), 5 deletions(-) diff --git a/djpress/templatetags/djpress_tags.py b/djpress/templatetags/djpress_tags.py index c12e63d..8d00aed 100644 --- a/djpress/templatetags/djpress_tags.py +++ b/djpress/templatetags/djpress_tags.py @@ -1,5 +1,7 @@ """Template tags for djpress.""" +from datetime import datetime + from django import template from django.conf import settings from django.db import models @@ -70,3 +72,53 @@ def post_category_link(category: Category, link_class: str = "") -> str: ) return mark_safe(output) + + +@register.simple_tag +def post_date_link(post_date: datetime, link_class: str = "") -> str: + """Return the date link for a post. + + Args: + post_date: The date of the post. + link_class: The CSS class(es) for the link. + """ + if not settings.DATE_ARCHIVES_ENABLED: + return post_date.strftime("%b %-d, %Y") + + post_year = post_date.strftime("%Y") + post_month = post_date.strftime("%m") + post_month_name = post_date.strftime("%b") + post_day = post_date.strftime("%d") + post_day_name = post_date.strftime("%-d") + post_time = post_date.strftime("%-I:%M %p") + + year_url = reverse( + "djpress:archives_posts", + args=[post_year], + ) + month_url = reverse( + "djpress:archives_posts", + args=[post_year, post_month], + ) + day_url = reverse( + "djpress:archives_posts", + args=[ + post_year, + post_month, + post_day, + ], + ) + + link_class_html = f' class="{link_class}"' if link_class else "" + + output = ( + f'{post_month_name} " + f'{post_day_name}, ' + f'' + f"{post_year}, " + f"{post_time}." + ) + + return mark_safe(output) diff --git a/djpress/tests/test_djpress_tags.py b/djpress/tests/test_djpress_tags.py index e4cb4f0..51e8bd4 100644 --- a/djpress/tests/test_djpress_tags.py +++ b/djpress/tests/test_djpress_tags.py @@ -3,6 +3,7 @@ from django.contrib.auth.models import User from djpress.models import Post, Category from django.conf import settings +from django.utils import timezone from djpress.templatetags import djpress_tags @@ -120,3 +121,80 @@ def test_post_category_link_with_category_path_with_two_link_classes(category): category_url = reverse("djpress:category_posts", args=[category.slug]) expected_output = f'{category.name}' assert djpress_tags.post_category_link(category, "class1 class2") == expected_output + + +@pytest.mark.django_db +def test_post_date_link_without_date_archives_enabled(create_test_post): + settings.DATE_ARCHIVES_ENABLED = False + output = create_test_post.date.strftime("%b %-d, %Y") + assert djpress_tags.post_date_link(create_test_post.date) == output + + +@pytest.mark.django_db +def test_post_date_link_with_date_archives_enabled(create_test_post): + settings.DATE_ARCHIVES_ENABLED = True + + post_date = create_test_post.date + post_year = post_date.strftime("%Y") + post_month = post_date.strftime("%m") + post_month_name = post_date.strftime("%b") + post_day = post_date.strftime("%d") + post_day_name = post_date.strftime("%-d") + post_time = post_date.strftime("%-I:%M %p") + + output = ( + f'{post_month_name} ' + f'{post_day_name}, ' + f'{post_year}, ' + f"{post_time}." + ) + + assert djpress_tags.post_date_link(create_test_post.date) == output + + +@pytest.mark.django_db +def test_post_date_link_with_date_archives_enabled_with_one_link_class( + create_test_post, +): + settings.DATE_ARCHIVES_ENABLED = True + + post_date = create_test_post.date + post_year = post_date.strftime("%Y") + post_month = post_date.strftime("%m") + post_month_name = post_date.strftime("%b") + post_day = post_date.strftime("%d") + post_day_name = post_date.strftime("%-d") + post_time = post_date.strftime("%-I:%M %p") + + output = ( + f'{post_month_name} ' + f'{post_day_name}, ' + f'{post_year}, ' + f"{post_time}." + ) + + assert djpress_tags.post_date_link(create_test_post.date, "class1") == output + + +@pytest.mark.django_db +def test_post_date_link_with_date_archives_enabled_with_two_link_classes( + create_test_post, +): + settings.DATE_ARCHIVES_ENABLED = True + + post_date = create_test_post.date + post_year = post_date.strftime("%Y") + post_month = post_date.strftime("%m") + post_month_name = post_date.strftime("%b") + post_day = post_date.strftime("%d") + post_day_name = post_date.strftime("%-d") + post_time = post_date.strftime("%-I:%M %p") + + output = ( + f'{post_month_name} ' + f'{post_day_name}, ' + f'{post_year}, ' + f"{post_time}." + ) + + assert djpress_tags.post_date_link(create_test_post.date, "class1 class2") == output diff --git a/templates/djpress/snippets/post_detail.html b/templates/djpress/snippets/post_detail.html index 16a1c3e..8c2bf17 100644 --- a/templates/djpress/snippets/post_detail.html +++ b/templates/djpress/snippets/post_detail.html @@ -9,11 +9,7 @@

{{ post.title }}