From 8f8ac354d994666728e5a8864d11ef898ba16ea2 Mon Sep 17 00:00:00 2001 From: Stuart Maxwell Date: Thu, 26 Dec 2024 21:56:57 +1300 Subject: [PATCH] Remove permalink property from Post model --- src/djpress/models/post.py | 31 -------------- src/djpress/url_utils.py | 2 +- tests/test_models_post.py | 57 ------------------------- tests/test_templatetags_djpress_tags.py | 4 +- 4 files changed, 2 insertions(+), 92 deletions(-) diff --git a/src/djpress/models/post.py b/src/djpress/models/post.py index 4f00c05..b1193e2 100644 --- a/src/djpress/models/post.py +++ b/src/djpress/models/post.py @@ -562,37 +562,6 @@ def url(self: "Post") -> str: return get_post_url(self) - @property - def permalink(self: "Post") -> str: - """Return the post's permalink. - - The posts permalink is constructed of the following elements: - - The post prefix - this is configured in POST_PREFIX and could be an empty - string or a custom string. - - The post slug - this is a unique identifier for the post. TODO: should this be - a database unique constraint, or should we handle it in software instead? - """ - # If the post type is a page, we return the full page path - if self.post_type == "page": - return self.full_page_path - - prefix = djpress_settings.POST_PREFIX - - # Replace placeholders in POST_PREFIX with actual values - replacements = { - "{{ year }}": self.date.strftime("%Y"), - "{{ month }}": self.date.strftime("%m"), - "{{ day }}": self.date.strftime("%d"), - } - - for placeholder, value in replacements.items(): - prefix = prefix.replace(placeholder, value) - - # Ensure there's no leading or trailing slash, then join with the slug - url_parts = [part for part in prefix.split("/") if part] + [self.slug] - - return "/".join(url_parts) - @property def full_page_path(self) -> str: """Return the full page path. diff --git a/src/djpress/url_utils.py b/src/djpress/url_utils.py index 48483c4..2479058 100644 --- a/src/djpress/url_utils.py +++ b/src/djpress/url_utils.py @@ -214,7 +214,7 @@ def get_archives_url(year: int, month: int | None = None, day: int | None = None def get_page_url(page: Post) -> str: """Return the URL for the page.""" - url = f"/{page.permalink}" + url = f"/{page.full_page_path}" if django_settings.APPEND_SLASH: return f"{url}/" diff --git a/tests/test_models_post.py b/tests/test_models_post.py index a372433..e06097b 100644 --- a/tests/test_models_post.py +++ b/tests/test_models_post.py @@ -249,51 +249,6 @@ def test_post_is_truncated_property(user, settings): assert post4.is_truncated is True -@pytest.mark.django_db -def test_post_permalink(user, settings): - post = Post( - title="Test Post", - slug="test-post", - content="This is a test post.", - author=user, - date=timezone.make_aware(timezone.datetime(2024, 1, 1)), - status="published", - post_type="post", - ) - - # Confirm the post prefix and permalink settings are set according to settings_testing.py - assert settings.DJPRESS_SETTINGS["POST_PREFIX"] == "test-posts" - assert post.permalink == "test-posts/test-post" - - # Test with no post prefix - settings.DJPRESS_SETTINGS["POST_PREFIX"] = "" - assert post.permalink == "test-post" - - # Test with text, year, month, day post prefix - settings.DJPRESS_SETTINGS["POST_PREFIX"] = "test-posts/{{ year }}/{{ month }}/{{ day }}" - assert post.permalink == "test-posts/2024/01/01/test-post" - - # Test with text, year, month post prefix - settings.DJPRESS_SETTINGS["POST_PREFIX"] = "test-posts/{{ year }}/{{ month }}" - assert post.permalink == "test-posts/2024/01/test-post" - - # Test with text, year post prefix - settings.DJPRESS_SETTINGS["POST_PREFIX"] = "test-posts/{{ year }}" - assert post.permalink == "test-posts/2024/test-post" - - # Test with year, month, day post prefix - settings.DJPRESS_SETTINGS["POST_PREFIX"] = "{{ year }}/{{ month }}/{{ day }}" - assert post.permalink == "2024/01/01/test-post" - - # Test with year, month post prefix - settings.DJPRESS_SETTINGS["POST_PREFIX"] = "{{ year }}/{{ month }}" - assert post.permalink == "2024/01/test-post" - - # Test with year post prefix - settings.DJPRESS_SETTINGS["POST_PREFIX"] = "{{ year }}" - assert post.permalink == "2024/test-post" - - @pytest.mark.django_db def test_get_published_posts_by_author(user): # Create some published posts by the test user @@ -337,18 +292,6 @@ def test_get_published_posts_by_author(user): assert future_post not in published_posts -@pytest.mark.django_db -def test_page_permalink(test_page1): - assert test_page1.permalink == "test-page1" - - -@pytest.mark.django_db -def test_page_permalink_parent(test_page1, test_page2): - test_page1.parent = test_page2 - test_page1.save() - assert test_page1.permalink == "test-page2/test-page1" - - @pytest.mark.django_db def test_get_recent_published_posts(user, settings): """Test that the get_recent_published_posts method returns the correct posts.""" diff --git a/tests/test_templatetags_djpress_tags.py b/tests/test_templatetags_djpress_tags.py index 281e5ed..7438697 100644 --- a/tests/test_templatetags_djpress_tags.py +++ b/tests/test_templatetags_djpress_tags.py @@ -102,9 +102,7 @@ def test_get_post_title_no_post_context(): @pytest.mark.django_db def test_post_title_posts(settings, test_post1): - """Test the post_title template tag. - - This uses the `post.permalink` property to generate the link.""" + """Test the post_title template tag.""" # Context should have both a posts and a post to simulate the for post in posts loop context = Context({"posts": [test_post1], "post": test_post1})