Skip to content

fix: prevent duplicate blog slug 500 in public blog view#145

Merged
gregagi merged 1 commit intomainfrom
fix/sentry-tuxseo-ce
Mar 3, 2026
Merged

fix: prevent duplicate blog slug 500 in public blog view#145
gregagi merged 1 commit intomainfrom
fix/sentry-tuxseo-ce

Conversation

@gregagi
Copy link
Copy Markdown
Collaborator

@gregagi gregagi commented Mar 3, 2026

Summary

  • fix BlogPostView to avoid MultipleObjectsReturned when duplicate published slugs exist
  • return the most recent published post for a slug (-created_at, then -id)
  • add regression test for duplicate-slug behavior on /blog/<slug>

Incident

Verification

  • Added test: core/tests/test_blog_post_view.py::test_blog_post_view_returns_most_recent_published_post_when_slug_is_duplicated
  • Test execution in this runtime is blocked by missing PostgreSQL service (127.0.0.1:5432 connection refused), so full pytest run could not complete here.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed blog post resolution to correctly return the most recent published post when multiple posts share the same slug.
  • Tests

    • Added tests to verify correct resolution of blog posts with duplicate slugs.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 3, 2026

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

📥 Commits

Reviewing files that changed from the base of the PR and between 49e9f7c and 2a0e781.

📒 Files selected for processing (2)
  • core/tests/test_blog_post_view.py
  • core/views.py
 ____________________________________________________________
< Your code has 99 problems and bug fixes ain't none of 'em. >
 ------------------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ

✏️ Tip: You can disable in-progress messages and the fortune message in your review settings.

Tip

You can make CodeRabbit's review stricter and more nitpicky using the `assertive` profile, if that's what you prefer.

Change the reviews.profile setting to assertive to make CodeRabbit's nitpick more issues in your PRs.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/sentry-tuxseo-ce

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 3, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 49e9f7c and 2a0e781.

📒 Files selected for processing (2)
  • core/tests/test_blog_post_view.py
  • core/views.py

📝 Walkthrough

Walkthrough

A new test module validates the BlogPostView's behavior when multiple blog posts share identical slugs, ensuring the view returns the most recently created published post. The BlogPostView is updated with a get_queryset() method that filters published posts and a custom get_object() method that retrieves posts by slug from the filtered results.

Changes

Cohort / File(s) Summary
Test Suite
core/tests/test_blog_post_view.py
New test module verifying that BlogPostView returns the newest published post when multiple posts share the same slug, with assertions on HTTP 200 response and correct post context.
View Logic
core/views.py
Updated BlogPostView with get_queryset() to filter and order published blog posts, and custom get_object() to fetch by slug with Http404 fallback. Added Http404 import.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Hop hop, the newest post appears,
When slugs collide through years and tears,
We fetch the fresh, the published bright,
Ordered just right, a rabbit's delight!

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/sentry-tuxseo-ce

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gregagi gregagi merged commit 8cecbf3 into main Mar 3, 2026
1 of 4 checks passed
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 3, 2026

Greptile Summary

This PR fixes a MultipleObjectsReturned 500 error in BlogPostView that was triggered when two published BlogPost records shared the same slug. The fix overrides get_queryset() to filter by PUBLISHED status with deterministic ordering (-created_at, -id), and overrides get_object() to call .first() instead of Django's default .get(), gracefully returning the most recent matching post or raising Http404.

Key changes:

  • core/views.py: BlogPostView now overrides get_queryset() and get_object() to use .first() with explicit ordering, preventing MultipleObjectsReturned.
  • core/tests/test_blog_post_view.py: Adds a targeted regression test confirming the most recently created post is returned for a duplicated published slug.

Assessment:
The fix is correct and will prevent the Sentry-reported 500 immediately. The implementation is a well-understood Django pattern that introduces no regressions. The test is reliable thanks to the -id fallback ordering, ensuring deterministic results even for posts created in quick succession.

Confidence Score: 5/5

  • Safe to merge — the fix correctly prevents the reported 500 error with no regressions and is covered by a targeted regression test.
  • The fix uses a well-established Django pattern (.first() with explicit ordering) to prevent MultipleObjectsReturned. The ordering by -created_at, -id is deterministic and correct. The regression test validates the core behavior change. There are no syntax, logic, or integration issues.
  • No files require special attention.

Last reviewed commit: 2a0e781

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant