Skip to content
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

Fixed #1756 -- hide unpublished events from side menus for staff #1820

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import timedelta
from io import StringIO

from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
Expand Down Expand Up @@ -191,6 +192,38 @@ def test_no_past_upcoming_events(self):
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context["events"], [])

def test_no_unpublished_future_events(self):
"""
Make sure there are no unpublished future events in the "upcoming events" sidebar
"""
# We need a published entry on the index page so that it doesn't return a 404
Entry.objects.create(pub_date=self.yesterday, is_active=True, slug="a")
Event.objects.create(
date=self.tomorrow,
pub_date=self.yesterday,
is_active=False,
headline="inactive",
)
Event.objects.create(
date=self.tomorrow,
pub_date=self.tomorrow,
is_active=True,
headline="future publish date",
)

for user in [
None,
User.objects.create(username="non-staff", is_staff=False),
User.objects.create(username="staff", is_staff=True),
User.objects.create_superuser(username="superuser"),
]:
if user:
self.client.force_login(user)
response = self.client.get(reverse("weblog:index"))
with self.subTest(user=user):
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context["events"], [])


class SitemapTests(DateTimeMixin, TestCase):
def test_sitemap(self):
Expand Down
4 changes: 1 addition & 3 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def get_queryset(self):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

events_queryset = Event.objects.future()
if not self.request.user.is_staff:
events_queryset = events_queryset.published()
events_queryset = Event.objects.future().published()

context["events"] = events_queryset[:3]

Expand Down
Loading