Skip to content

Commit

Permalink
Merge pull request #65 from stuartmaxwell:pages-should-not-appear-in-…
Browse files Browse the repository at this point in the history
…rss-feeds

Pages-should-not-appear-in-rss-feeds
  • Loading branch information
stuartmaxwell authored Nov 19, 2024
2 parents bf32b14 + 2b6984e commit 3815407
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/djpress/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,18 @@ def __str__(self: "Post") -> str:
return self.title

def save(self: "Post", *args, **kwargs) -> None: # noqa: ANN002, ANN003
"""Override the save method to auto-generate the slug."""
"""Override the save method."""
# auto-generate the slug.
if not self.slug:
self.slug = slugify(self.title)
if not self.slug or self.slug.strip("-") == "":
msg = "Invalid title. Unable to generate a valid slug."
raise ValueError(msg)

# If the post is a post, we need to ensure that the parent is None
if self.post_type == "post":
self.parent = None

self.full_clean()
super().save(*args, **kwargs)

Expand Down
33 changes: 33 additions & 0 deletions tests/test_feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,36 @@ def test_truncated_posts_feed(client, user):
assert "<title>Post 1</title>" in feed
assert "Truncated content" not in feed
assert f'&lt;a href="/{post_prefix}/post-1/"&gt;Read more&lt;/a&gt;&lt;/p&gt;' in feed


@pytest.mark.django_db
def test_pages_not_in_feed(client, test_post1, test_post2, test_post3, test_page1, user):
url = get_rss_url()
response = client.get(url)

assert response.status_code == 200
assert response["Content-Type"] == "application/rss+xml; charset=utf-8"

feed = response.content.decode("utf-8")
assert "<item>" in feed
assert test_post1.title in feed
assert test_post2.title in feed
assert test_post3.title in feed
assert test_page1.title not in feed

Post.page_objects.create(
title="New Test Page",
content="Content of page 1.",
author=user,
status="published",
post_type="page",
)

response = client.get(url)

assert response.status_code == 200
assert response["Content-Type"] == "application/rss+xml; charset=utf-8"

feed = response.content.decode("utf-8")
assert "<item>" in feed
assert "New Test Page" not in feed
16 changes: 16 additions & 0 deletions tests/test_models_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,3 +1101,19 @@ def test_page_is_child(test_page1, test_page2):
test_page1.parent = test_page2
test_page1.save()
assert test_page1.is_child is True


@pytest.mark.django_db
def test_parent_page_cant_have_post_child(test_page1, test_post1):
test_post1.parent = test_page1
test_post1.save()

assert test_post1 not in test_page1.children.all()


@pytest.mark.django_db
def test_post_parent_is_none(test_post1, test_page1):
test_post1.parent = test_page1
test_post1.save()

assert test_post1.parent is None

0 comments on commit 3815407

Please sign in to comment.