Skip to content

Commit

Permalink
Merge pull request #101 from stuartmaxwell:remove-permalink
Browse files Browse the repository at this point in the history
Remove permalink property from Post model
  • Loading branch information
stuartmaxwell authored Dec 26, 2024
2 parents 0a09c41 + 8f8ac35 commit 2f52454
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 92 deletions.
31 changes: 0 additions & 31 deletions src/djpress/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/djpress/url_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}/"
Expand Down
57 changes: 0 additions & 57 deletions tests/test_models_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
4 changes: 1 addition & 3 deletions tests/test_templatetags_djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down

0 comments on commit 2f52454

Please sign in to comment.