-
-
Notifications
You must be signed in to change notification settings - Fork 253
fix: Prevent publishing blog posts that have future publish date #2474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Prevent publishing blog posts that have future publish date #2474
Conversation
Summary by CodeRabbit
WalkthroughImported timezone and updated Post.recent_posts to filter posts with published_at <= timezone.now() before ordering by published_at descending. Tests updated to stub/filter by the published_at cutoff and assert ordering on the filtered queryset. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
backend/tests/apps/owasp/models/post_test.py (1)
110-132: Consider enhancing test to explicitly verify future post exclusion.The test correctly verifies that
filteris called withpublished_at__lte=current_timeand validates the chaining withorder_by. However, it could be more comprehensive by including a scenario with a future-dated post to explicitly demonstrate exclusion behavior.Example enhancement:
@patch("apps.owasp.models.post.timezone.now") @patch("apps.owasp.models.post.Post.objects.filter") def test_recent_posts_filters_by_published_date(self, mock_filter, mock_now): """Test recent_posts returns only posts published before current time.""" current_time = datetime(2025, 6, 15, 12, 0, 0, tzinfo=UTC) mock_now.return_value = current_time past_post = Mock( title="Past Post", url="https://example.com/past", published_at=datetime(2025, 6, 10, 12, 0, 0, tzinfo=UTC), ) # Include a future post in the test setup to make exclusion explicit future_post = Mock( title="Future Post", url="https://example.com/future", published_at=datetime(2025, 6, 20, 12, 0, 0, tzinfo=UTC), ) mock_queryset = Mock() mock_queryset.order_by.return_value = [past_post] # Only past post in result mock_filter.return_value = mock_queryset result = Post.recent_posts() mock_filter.assert_called_once_with(published_at__lte=current_time) mock_queryset.order_by.assert_called_once_with("-published_at") assert result == [past_post] assert future_post not in result # Explicitly verify future post exclusionNote: Since you're mocking
filter, the actual Django ORM filtering isn't tested here—only that the method is called correctly. Integration tests with a real database would provide end-to-end verification of the filtering behavior.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 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-85)recent_posts(38-44)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Run Code Scan
- GitHub Check: Run CI Denendencies Scan
- GitHub Check: CodeQL (python)
- GitHub Check: CodeQL (javascript-typescript)
🔇 Additional comments (3)
backend/apps/owasp/models/post.py (2)
6-6: LGTM! Correct import for timezone-aware datetime.The
timezoneimport is necessary for thetimezone.now()call and ensures timezone-aware datetime handling.
40-44: LGTM! Correctly filters and orders posts.The implementation correctly addresses issue #2327 by filtering posts with
published_at__lte=timezone.now()to exclude future-dated posts, then ordering by most recent first. The existing database index on"-published_at"(line 18) will ensure good query performance.backend/tests/apps/owasp/models/post_test.py (1)
97-109: LGTM! Test correctly verifies ordering on filtered queryset.The test has been properly updated to patch
Post.objects.filterand verify thatorder_by("-published_at")is called on the filtered queryset, which aligns with the new implementation.



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
make check-testlocally; all checks and tests passed.