diff --git a/website/api/tests.py b/website/api/tests.py index 069b039..330429f 100644 --- a/website/api/tests.py +++ b/website/api/tests.py @@ -25,7 +25,7 @@ class PageLinksAPIViewTestCase(APITestCase): ContentPageFactory(parent=cls.home_page) def test_accessible(self) -> None: - with self.assertNumQueries(3): + with self.assertNumQueries(4): response = self.client.get(self.url) self.assertEqual(response.status_code, 200) @@ -43,29 +43,29 @@ class LMOTFYAPIViewTestCase(APITestCase): cls.exact = BlogPostPageFactory(parent=cls.home_page, title="Post exact") def test_accessible(self) -> None: - with self.assertNumQueries(4): + with self.assertNumQueries(5): response = self.client.get(self.url, {"search": "Post"}) self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 5) def test_case_insensitive_search(self) -> None: - with self.assertNumQueries(4): + with self.assertNumQueries(5): response = self.client.get(self.url, {"search": "post"}) self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 5) def test_no_search_term(self) -> None: - with self.assertNumQueries(0): + with self.assertNumQueries(1): response = self.client.get(self.url) self.assertEqual(response.status_code, 200) def test_empty_search_term(self) -> None: - with self.assertNumQueries(0): + with self.assertNumQueries(1): response = self.client.get(self.url, {"search": ""}) self.assertEqual(response.status_code, 200) def test_exact(self) -> None: - with self.assertNumQueries(4): + with self.assertNumQueries(5): response = self.client.get(self.url, {"search": "Post exact"}) self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 1) diff --git a/website/api/views.py b/website/api/views.py index b56c122..7293cb4 100644 --- a/website/api/views.py +++ b/website/api/views.py @@ -27,6 +27,7 @@ class PageLinksAPIView(ListAPIView): def get_queryset(self) -> PageQuerySet: return ( Page.objects.live() + .public() .exclude(depth__lte=1) .only("id", "url_path", "title") .order_by("title") @@ -45,6 +46,8 @@ class LMOTFYAPIView(ListAPIView): pagination_class = CustomPageNumberPagination def get_queryset(self) -> PageQuerySet: - return BlogPostPage.objects.live().select_related( - "hero_image", "hero_unsplash_photo" + return ( + BlogPostPage.objects.live() + .public() + .select_related("hero_image", "hero_unsplash_photo") ) diff --git a/website/blog/models.py b/website/blog/models.py index 7eef6c6..1ac09c7 100644 --- a/website/blog/models.py +++ b/website/blog/models.py @@ -41,6 +41,7 @@ class BlogPostListPage(BaseListingPage): return prefetch_for_listing( BlogPostPage.objects.descendant_of(self) .live() + .public() .order_by("-date", "title") .prefetch_related("tags") ) @@ -86,7 +87,7 @@ class BlogPostTagPage(BaseListingPage): return f"Pages tagged with '{super().html_title}'" def get_listing_pages(self) -> models.QuerySet: - blog_list_page = BlogPostListPage.objects.all().live().get() + blog_list_page = BlogPostListPage.objects.get() return blog_list_page.get_listing_pages().filter(tags=self) @property @@ -109,7 +110,7 @@ class BlogPostCollectionListPage(BaseListingPage): def get_listing_pages(self) -> models.QuerySet: blog_list_page = BlogPostListPage.objects.all().live().get() - return BlogPostCollectionPage.objects.child_of(blog_list_page).live() + return BlogPostCollectionPage.objects.child_of(blog_list_page).live().public() class BlogPostCollectionPage(BaseListingPage): @@ -120,6 +121,7 @@ class BlogPostCollectionPage(BaseListingPage): return prefetch_for_listing( BlogPostPage.objects.child_of(self) .live() + .public() .prefetch_related("tags") .order_by("-date", "title") ) diff --git a/website/common/models.py b/website/common/models.py index 538613c..3dfc5b4 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -203,7 +203,7 @@ class BaseListingPage(RoutablePageMixin, BaseContentPage): def get_listing_pages(self) -> models.QuerySet: return prefetch_for_listing( - self.get_children().live().specific().order_by("title") + self.get_children().live().public().specific().order_by("title") ) def get_paginator_page(self) -> PaginatorPage: diff --git a/website/common/templatetags/footer_tags.py b/website/common/templatetags/footer_tags.py index b50f0de..356efae 100644 --- a/website/common/templatetags/footer_tags.py +++ b/website/common/templatetags/footer_tags.py @@ -8,5 +8,5 @@ register = Library() @register.inclusion_tag("common/footer.html") def footer() -> dict: return { - "homepage": HomePage.objects.live().get(), + "homepage": HomePage.objects.get(), } diff --git a/website/common/templatetags/navbar_tags.py b/website/common/templatetags/navbar_tags.py index 728ce77..c94cb60 100644 --- a/website/common/templatetags/navbar_tags.py +++ b/website/common/templatetags/navbar_tags.py @@ -13,7 +13,8 @@ def navbar() -> dict: "homepage": homepage, "nav_pages": homepage.get_children() .live() + .public() .filter(show_in_menus=True) .order_by("title"), - "search_page": SearchPage.objects.all().live().defer_streamfields().first(), + "search_page": SearchPage.objects.defer_streamfields().first(), } diff --git a/website/common/tests/test_pages.py b/website/common/tests/test_pages.py index b85e573..433397c 100644 --- a/website/common/tests/test_pages.py +++ b/website/common/tests/test_pages.py @@ -35,7 +35,7 @@ class ContentPageTestCase(TestCase): self.assertEqual(response.status_code, 200) def test_queries(self) -> None: - with self.assertNumQueries(25): + with self.assertNumQueries(26): self.client.get(self.page.url) @@ -52,14 +52,14 @@ class ListingPageTestCase(TestCase): ContentPageFactory(parent=cls.page) def test_accessible(self) -> None: - with self.assertNumQueries(29): + with self.assertNumQueries(31): response = self.client.get(self.page.url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context["listing_pages"]), 2) self.assertContains(response, self.page.reverse_subpage("feed")) def test_feed_accessible(self) -> None: - with self.assertNumQueries(11): + with self.assertNumQueries(12): response = self.client.get( self.page.url + self.page.reverse_subpage("feed") ) diff --git a/website/common/tests/test_views.py b/website/common/tests/test_views.py index 7451237..ca98711 100644 --- a/website/common/tests/test_views.py +++ b/website/common/tests/test_views.py @@ -16,7 +16,7 @@ class Error404PageTestCase(TestCase): ) def test_queries(self) -> None: - with self.assertNumQueries(16): + with self.assertNumQueries(17): self.client.get(self.url) diff --git a/website/common/views.py b/website/common/views.py index 16894d5..b077793 100644 --- a/website/common/views.py +++ b/website/common/views.py @@ -25,7 +25,7 @@ class Error404View(TemplateView): def get_context_data(self, **kwargs: dict) -> dict: context = super().get_context_data(**kwargs) - context["homepage"] = HomePage.objects.live().get() + context["homepage"] = HomePage.objects.get() return context @@ -58,7 +58,7 @@ class AllPagesFeed(Feed): return super().__call__(request, *args, **kwargs) def items(self) -> PageQuerySet: - return Page.objects.live().exclude(depth__lte=2) + return Page.objects.live().public().exclude(depth__lte=2) def item_title(self, item: BasePage) -> str: return item.title diff --git a/website/home/models.py b/website/home/models.py index ea4568c..26e65e1 100644 --- a/website/home/models.py +++ b/website/home/models.py @@ -42,9 +42,11 @@ class HomePage(BasePage, WagtailImageMetadataMixin): context = super().get_context(request) context["latest_blog_post"] = ( - BlogPostPage.objects.live().defer_streamfields().order_by("-date").first() - ) - context["search_page"] = ( - SearchPage.objects.all().live().defer_streamfields().first() + BlogPostPage.objects.live() + .public() + .defer_streamfields() + .order_by("-date") + .first() ) + context["search_page"] = SearchPage.objects.defer_streamfields().first() return context diff --git a/website/legacy/views.py b/website/legacy/views.py index 273018a..fd06e54 100644 --- a/website/legacy/views.py +++ b/website/legacy/views.py @@ -9,7 +9,7 @@ from website.blog.models import BlogPostListPage @method_decorator(cache_page(60 * 60), name="dispatch") class PostsFeedView(RedirectView): def get_redirect_url(self) -> str: - post_list = get_object_or_404(BlogPostListPage.objects.live()) + post_list = get_object_or_404(BlogPostListPage) return post_list.url + post_list.reverse_subpage("feed") diff --git a/website/search/models.py b/website/search/models.py index 6544207..127029b 100644 --- a/website/search/models.py +++ b/website/search/models.py @@ -64,6 +64,7 @@ class SearchPage(RoutablePageMixin, BaseContentPage): filters, query = parse_query_string(search_query) pages = ( Page.objects.live() + .public() .not_type(self.__class__, HomePage) .search(query, order_by_relevance=True) ) diff --git a/website/search/tests.py b/website/search/tests.py index 4e4174e..860c6ca 100644 --- a/website/search/tests.py +++ b/website/search/tests.py @@ -55,7 +55,7 @@ class SearchPageResultsTestCase(TestCase): cls.url = cls.page.url + cls.page.reverse_subpage("results") def test_returns_results(self) -> None: - with self.assertNumQueries(11): + with self.assertNumQueries(12): response = self.client.get(self.url, {"q": "post"}, HTTP_HX_REQUEST="true") self.assertEqual(response.status_code, 200) @@ -89,7 +89,7 @@ class SearchPageResultsTestCase(TestCase): ) def test_too_high_page(self) -> None: - with self.assertNumQueries(46): + with self.assertNumQueries(48): response = self.client.get( self.url, {"q": "post", "page": 3}, HTTP_HX_REQUEST="true" ) diff --git a/website/well_known/views.py b/website/well_known/views.py index 6f121e6..e0b2098 100644 --- a/website/well_known/views.py +++ b/website/well_known/views.py @@ -23,7 +23,7 @@ class SecurityView(TemplateView): def get_context_data(self, **kwargs: dict) -> dict: context = super().get_context_data(**kwargs) context["security_txt"] = self.request.build_absolute_uri(self.request.path) - context["contact_page"] = ContactPage.objects.live().first() + context["contact_page"] = ContactPage.objects.first() context["expires"] = ( (timezone.now() + self.expires).replace(microsecond=0).isoformat() )