Skip to content

Conversation

@marksweb
Copy link
Contributor

This fixes #1755 to only return published blog entries, even for staff users.

Unpublished entries can still be previewed via the admin.

@marksweb
Copy link
Contributor Author

marksweb commented Nov 20, 2024

On a side note, my test suite is failing me and I'm not sure why.

======================================================================
ERROR: blog.tests (unittest.loader._FailedTest.blog.tests)
----------------------------------------------------------------------
ImportError: Failed to import test module: blog.tests
Traceback (most recent call last):
  File "/Users/mark/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/unittest/loader.py", line 396, in _find_test_path
    module = self._get_module_from_name(name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mark/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/unittest/loader.py", line 339, in _get_module_from_name
    __import__(name)
  File "/Users/mark/dev/djangoproject.com/blog/tests.py", line 2, in <module>
    from test.support import captured_stderr
ModuleNotFoundError: No module named 'test'

Discovered the reason for this; #1758

Copy link
Member

@bmispelon bmispelon left a comment

Choose a reason for hiding this comment

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

Thanks for the quick response on this, nice work 👍🏻

If you allow me to change/extend the scope of this PR a little bit, I think it's not enough to test for user.is_staff but we should also make sure that the user has write permissions on the BlogEntry model (since this feature is meant to be used for previewing).

And there should also be some tests added (I'm suprised there weren't already, or that if there were they were still passing after this change):

  • anon users can't see unpublished entries at all (list or detail)
  • logged in users (non-staff) can't see unpublished entries at all
  • staff users without write permission on BlogEntry can't see unpublished entries at all
  • staff users with write permission on BlogEntry can't see unpublished entries in the list, but can view the detail page

@jefftriplett
Copy link
Member

@marksweb this is great. Thank you for jumping in on this.

@marksweb
Copy link
Contributor Author

And there should also be some tests added (I'm suprised there weren't already

Yes I was surprised & expecting you to suggest this 😄

@marksweb
Copy link
Contributor Author

Thanks for the quick response on this, nice work 👍🏻

If you allow me to change/extend the scope of this PR a little bit, I think it's not enough to test for user.is_staff but we should also make sure that the user has write permissions on the BlogEntry model (since this feature is meant to be used for previewing).

And there should also be some tests added (I'm suprised there weren't already, or that if there were they were still passing after this change):

* anon users can't see unpublished entries at all (list or detail)

* logged in users (non-staff) can't see unpublished entries at all

* staff users without write permission on `BlogEntry` can't see unpublished entries at all

* staff users with write permission on `BlogEntry` can't see unpublished entries in the list, but can view the detail page

Ok, it took longer than I expected, but I've added tests and enforced permissions.

Copy link
Member

@adamzap adamzap left a comment

Choose a reason for hiding this comment

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

👍 Looks great!

I guess the last thing to do here is squash commits and rebase the branch, but maybe the merger will do that?

Copy link
Member

@pauloxnet pauloxnet left a comment

Choose a reason for hiding this comment

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

I added 2 minor suggestions.

@adamzap
Copy link
Member

adamzap commented Jul 14, 2025

@marksweb Could you check the CI failure here?

marksweb added 10 commits July 15, 2025 02:52
diff --git blog/views.py blog/views.py
index 4629754..49c1fdc 100644
--- blog/views.py
+++ blog/views.py
@@ -21,7 +21,6 @@ def get_queryset(self):
         return Entry.objects.published()

     def get_context_data(self, **kwargs):
-        """ """
         context = super().get_context_data(**kwargs)

         events_queryset = Event.objects.future().published()
@@ -50,3 +49,10 @@ class BlogDayArchiveView(BlogViewMixin, DayArchiveView):

 class BlogDateDetailView(BlogViewMixin, DateDetailView):
     banner_is_title = False
+
+    def get_queryset(self):
+        """Allows staff users to view unpublished entries"""
+        if self.request.user.is_staff:
+            return Entry.objects.all()
+        else:
+            return Entry.objects.published()
@marksweb
Copy link
Contributor Author

@marksweb Could you check the CI failure here?

Done - there was an actual issue I'd not spotted 🤦🏼

@adamzap
Copy link
Member

adamzap commented Jul 16, 2025

@bmispelon Do you have any concerns here, or can we merge this?

Copy link
Member

@bmispelon bmispelon left a comment

Choose a reason for hiding this comment

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

Nice work Mark, thanks for this! 👍🏻

@sabderemane sabderemane merged commit b2a5462 into django:main Aug 3, 2025
4 checks passed
@marksweb marksweb deleted the fix/1755 branch October 16, 2025 18:05
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.

Unpublished Blog Entries show up for staff in list pages

6 participants