Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ishanBahuguna
Copy link
Contributor

Proposed change

Resolves #2327

Update recent_posts method to filter posts by publish date and order by most recent

This PR updates the recent_posts() static method in the Post model to ensure only posts published up to the current time are returned, ordered by most recent first.

Checklist

  • I've read and followed the contributing guidelines.
  • I've run make check-test locally; all checks and tests passed.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 22, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Recent posts now filter to display only published content, ordered by publication date.
  • Tests

    • Updated tests to validate filtering and ordering behavior for recent posts.

Walkthrough

Added timezone import and modified Post.recent_posts queryset to filter for published posts where published_at is less than or equal to the current time before ordering. Updated corresponding test to validate the filter behavior.

Changes

Cohort / File(s) Summary
Model filtering logic
backend/apps/owasp/models/post.py
Added import from django.utils.timezone. Modified Post.recent_posts method to filter published posts (published_at <= now()) before ordering by published_at descending.
Test validation
backend/tests/apps/owasp/models/post_test.py
Updated test_recent_posts_ordering to patch filter() instead of order_by(). Now validates that filter is called with published_at__lte condition and that order_by("-published_at") is invoked on the filtered queryset.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "fix: Prevent publishing blog posts that have future publish date" clearly and concisely describes the main change in the changeset. It accurately reflects the modification to the Post model's recent_posts() method, which now filters posts to exclude those with future publication dates. The title is specific enough that a developer reviewing the commit history would understand the primary objective without requiring additional context. The title directly aligns with the changeset modifications and the linked issue #2327.
Linked Issues Check ✅ Passed The PR implementation directly satisfies the requirements specified in linked issue #2327. The issue requires that only posts with a publish date up to the current time should be displayed on the site, preventing posts with future dates from appearing prematurely. The code changes implement this by adding a filter condition published_at__lte=now() to the recent_posts() method, which ensures only published posts are returned. Additionally, the PR maintains the ordering by most recent posts first via order_by("-published_at"), and the updated test validates both the filtering and ordering behavior, confirming compliance with the issue's requirements [#2327].
Out of Scope Changes Check ✅ Passed All code changes in this PR are directly in scope and related to the linked issue #2327. The modifications include adding the necessary django.utils.timezone import to support the now() function, updating the Post.recent_posts() method to filter by publication date, and modifying the corresponding test to validate the new filtering behavior. These changes are all focused on the single objective of preventing blog posts with future publication dates from being displayed. No extraneous modifications or unrelated changes are present in the changeset.
Description Check ✅ Passed The pull request description provides relevant context for the changes made in this PR. It explicitly references issue #2327 and describes the main change: updating the recent_posts() method to filter posts by publish date and order by most recent first. The description explains the purpose of the modification and includes a checklist indicating the contributor followed contributing guidelines and ran local tests. While the level of detail is reasonable, it is directly related to the changeset and conveys meaningful information about the implementation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 860bc5f and df6541a.

📒 Files selected for processing (2)
  • backend/apps/owasp/models/post.py (2 hunks)
  • backend/tests/apps/owasp/models/post_test.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
backend/tests/apps/owasp/models/post_test.py (1)
backend/apps/owasp/models/post.py (2)
  • Post (12-81)
  • recent_posts (38-40)
🔇 Additional comments (3)
backend/apps/owasp/models/post.py (2)

7-7: Correct import for timezone-aware datetime.

Using django.utils.timezone.now is the proper approach for timezone-aware datetime comparisons in Django.


40-40: Implementation correctly addresses the issue and no other queries require similar filtering.

Verification confirms:

  • The API queries use Post.recent_posts() which now includes the published_at__lte filter
  • The sync management command uses Post.update_data() for upserting, which intentionally doesn't filter (it must handle all posts during sync)
  • The only other direct query (Post.objects.get(url=url)) is for internal upsert logic and appropriately doesn't filter by published_at
backend/tests/apps/owasp/models/post_test.py (1)

97-107: Test properly verifies the filter and ordering logic.

The test correctly validates that:

  • The filter is invoked with published_at__lte
  • The ordering is applied to the filtered queryset
  • The return value is as expected

The unit test appropriately mocks the database layer and focuses on verifying the method's behavior.


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.

@sonarqubecloud
Copy link

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

News items published too early

1 participant