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

Only retrieve the pages that are registered in the settings #226

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 52 additions & 5 deletions example/example/tests/test_grapple.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
locate(middleware) for middleware in settings.GRAPHENE["MIDDLEWARE"]
]
MIDDLEWARE = [item() if inspect.isclass(item) else item for item in MIDDLEWARE_OBJECTS]
DEFAULT_APPS = ["images", "home", "documents"]


class BaseGrappleTest(TestCase):
Expand Down Expand Up @@ -73,7 +74,9 @@ def test_pages(self):
pages = Page.objects.filter(depth__gt=1)
self.assertEquals(len(executed["data"]["pages"]), pages.count())

@override_settings(GRAPPLE={"PAGE_SIZE": 1, "MAX_PAGE_SIZE": 1})
@override_settings(
GRAPPLE={"PAGE_SIZE": 1, "MAX_PAGE_SIZE": 1, "APPS": DEFAULT_APPS}
)
def test_pages_limit(self):
query = """
{
Expand Down Expand Up @@ -167,14 +170,54 @@ def test_page(self):
"""

executed = self.client.execute(query, variables={"id": self.blog_post.id})

self.assertEquals(type(executed["data"]), dict_type)
self.assertEquals(type(executed["data"]["page"]), dict_type)

page_data = executed["data"]["page"]
self.assertEquals(page_data["contentType"], "home.BlogPage")
self.assertEquals(page_data["parent"]["contentType"], "home.HomePage")

@override_settings(GRAPPLE={"APPS": []})
def test_pages_with_no_apps(self):
query = """
{
pages {
id
title
contentType
}
}
"""

executed = self.client.execute(query)
self.assertEquals(type(executed["data"]), dict_type)
self.assertEquals(type(executed["data"]["pages"]), list)
self.assertEquals(len(executed["data"]["pages"]), 0)

@override_settings(GRAPPLE={"APPS": ["home"]})
def test_pages_with_home_app(self):
query = """
{
pages {
id
title
contentType
}
}
"""

executed = self.client.execute(query)

self.assertEquals(type(executed["data"]), dict_type)
self.assertEquals(type(executed["data"]["pages"]), list)

pages_data = executed["data"]["pages"]
self.assertEquals(pages_data[0]["contentType"], "home.HomePage")
self.assertEquals(pages_data[1]["contentType"], "home.BlogPage")
zerolab marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

I suggest adding news_post = NewsPageFactory(parent=self.home) before the execute and add a check to ensure the news page doesn't appear in the results


pages = Page.objects.filter(depth__gt=1)
self.assertEquals(len(executed["data"]["pages"]), pages.count())


class PageUrlPathTest(BaseGrappleTest):
def _query_by_path(self, path, in_site=False):
Expand Down Expand Up @@ -373,7 +416,7 @@ def test_site_pages_content_type_filter(self):
self.assertEquals(data[0]["title"], blog.title)


@override_settings(GRAPPLE={"AUTO_CAMELCASE": False})
@override_settings(GRAPPLE={"AUTO_CAMELCASE": False, "APPS": DEFAULT_APPS})
class DisableAutoCamelCaseTest(TestCase):
def setUp(self):
schema = create_schema()
Expand Down Expand Up @@ -476,7 +519,9 @@ def test_renditions(self):
executed = self.client.execute(query)
self.assertIn("width-100", executed["data"]["image"]["rendition"]["url"])

@override_settings(GRAPPLE={"ALLOWED_IMAGE_FILTERS": ["width-200"]})
@override_settings(
GRAPPLE={"ALLOWED_IMAGE_FILTERS": ["width-200"], "APPS": DEFAULT_APPS}
)
def test_renditions_with_allowed_image_filters_restrictions(self):
def get_query(**kwargs):
params = ",".join([f"{key}: {value}" for key, value in kwargs.items()])
Expand All @@ -500,7 +545,9 @@ def get_query(**kwargs):
self.assertIsNotNone(executed["data"]["image"]["rendition"])
self.assertIn("width-200", executed["data"]["image"]["rendition"]["url"])

@override_settings(GRAPPLE={"ALLOWED_IMAGE_FILTERS": ["width-200"]})
@override_settings(
GRAPPLE={"ALLOWED_IMAGE_FILTERS": ["width-200"], "APPS": DEFAULT_APPS}
)
def test_src_set(self):
query = """
{
Expand Down
9 changes: 7 additions & 2 deletions example/home/test/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ def test_pages_search(self):
self.assertEqual(pages[1]["title"], "Test post 3")

def test_pages_search_with_min_max_page_size_settings(self):
exposed_apps = ["images", "home", "documents"]
query = """
query ($term: String, $limit: PositiveInt, $offset: PositiveInt) {
pages(searchQuery: $term, limit: $limit, offset: $offset) {
Expand All @@ -433,7 +434,9 @@ def test_pages_search_with_min_max_page_size_settings(self):

# the max page size is 1 and we should get only one,
# even if default page size and the requested limit are higher
with override_settings(GRAPPLE={"PAGE_SIZE": 10, "MAX_PAGE_SIZE": 1}):
with override_settings(
GRAPPLE={"PAGE_SIZE": 10, "MAX_PAGE_SIZE": 1, "APPS": exposed_apps}
):
results = self.client.execute(
query,
variables={"term": "t", "limit": 2},
Expand All @@ -444,7 +447,9 @@ def test_pages_search_with_min_max_page_size_settings(self):
self.assertEqual(pages[0]["title"], "Test post 1")

# Default page size is one, but we ask for two which is still less than max page size
with override_settings(GRAPPLE={"PAGE_SIZE": 1, "MAX_PAGE_SIZE": 5}):
with override_settings(
GRAPPLE={"PAGE_SIZE": 1, "MAX_PAGE_SIZE": 5, "APPS": exposed_apps}
):
results = self.client.execute(
query,
variables={"term": "t", "limit": 2},
Expand Down
8 changes: 7 additions & 1 deletion grapple/types/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from wagtail_headless_preview.signals import preview_update

from ..registry import registry
from ..settings import has_channels
from ..settings import grapple_settings, has_channels
from ..utils import resolve_queryset
from .structures import QuerySetList

Expand Down Expand Up @@ -273,6 +273,12 @@ def resolve_pages(self, info, **kwargs):
WagtailPage.objects.live().public().filter(depth__gt=1).specific()
) # no need to the root page

# Retrieve only the pages that are registered in the settings
content_types = ContentType.objects.filter(
app_label__in=grapple_settings.APPS
)
pages = pages.filter(content_type__in=content_types)
Comment on lines +276 to +280
Copy link
Member

Choose a reason for hiding this comment

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

this could be simplified as:

pages = pages.filter(content_type__app_label__in=grapple_settings.APPS)

Copy link
Member

Choose a reason for hiding this comment

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


if kwargs.get("in_site", False):
site = Site.find_for_request(info.context)
pages = pages.in_site(site)
Expand Down