Only show public pages

This commit is contained in:
Jake Howard 2022-08-28 16:51:27 +01:00
parent 47980032b0
commit 557a28833b
Signed by: jake
GPG key ID: 57AFB45680EDD477
14 changed files with 36 additions and 27 deletions

View file

@ -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)

View file

@ -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")
)

View file

@ -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")
)

View file

@ -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:

View file

@ -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(),
}

View file

@ -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(),
}

View file

@ -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")
)

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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")

View file

@ -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)
)

View file

@ -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"
)

View file

@ -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()
)