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 all 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
3 changes: 2 additions & 1 deletion example/example/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"images",
"documents",
"search",
"news",
"wagtail.contrib.forms",
"wagtail.contrib.redirects",
"wagtail.embeds",
Expand Down Expand Up @@ -169,7 +170,7 @@
"MIDDLEWARE": ["grapple.middleware.GrappleMiddleware"],
}
GRAPPLE = {
"APPS": ["images", "home", "documents"],
"APPS": ["images", "home", "documents", "news"],
"ADD_SEARCH_HIT": True,
"EXPOSE_GRAPHIQL": True,
}
Expand Down
85 changes: 80 additions & 5 deletions example/example/tests/test_grapple.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from graphene.test import Client
from home.factories import BlogPageFactory
from home.models import HomePage
from news.factories import NewsPageFactory
from wagtail.core.models import Page, Site
from wagtail.documents import get_document_model
from wagtail.images import get_image_model
Expand All @@ -32,6 +33,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 +75,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 +171,81 @@ 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())

@override_settings(GRAPPLE={"APPS": ["home", "news"]})
def test_pages_with_news_app(self):
news_post = NewsPageFactory(parent=self.home)
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")
self.assertEquals(pages_data[2]["contentType"], "news.NewsPage")

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


class PageUrlPathTest(BaseGrappleTest):
def _query_by_path(self, path, in_site=False):
Expand Down Expand Up @@ -373,7 +444,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 +547,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 +573,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
22 changes: 22 additions & 0 deletions example/home/migrations/0023_alter_blogpage_body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.13 on 2022-04-24 07:18
Copy link
Member

Choose a reason for hiding this comment

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

I can't see the changes that triggered this migration 🤔


from django.db import migrations
import wagtail.core.blocks
import wagtail.core.fields
import wagtail.embeds.blocks
import wagtail.images.blocks


class Migration(migrations.Migration):

dependencies = [
('home', '0022_alter_blogpage_body'),
]

operations = [
migrations.AlterField(
model_name='blogpage',
name='body',
field=wagtail.core.fields.StreamField([('heading', wagtail.core.blocks.CharBlock(form_classname='full title')), ('paragraph', wagtail.core.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock()), ('decimal', wagtail.core.blocks.DecimalBlock()), ('date', wagtail.core.blocks.DateBlock()), ('datetime', wagtail.core.blocks.DateTimeBlock()), ('gallery', wagtail.core.blocks.StructBlock([('title', wagtail.core.blocks.CharBlock(form_classname='full title')), ('images', wagtail.core.blocks.StreamBlock([('image', wagtail.core.blocks.StructBlock([('caption', wagtail.core.blocks.CharBlock(form_classname='full title')), ('image', wagtail.images.blocks.ImageChooserBlock())]))]))])), ('video', wagtail.core.blocks.StructBlock([('youtube_link', wagtail.embeds.blocks.EmbedBlock(required=False))])), ('objectives', wagtail.core.blocks.ListBlock(wagtail.core.blocks.CharBlock())), ('carousel', wagtail.core.blocks.StreamBlock([('text', wagtail.core.blocks.CharBlock(form_classname='full title')), ('image', wagtail.images.blocks.ImageChooserBlock()), ('markup', wagtail.core.blocks.RichTextBlock())])), ('callout', wagtail.core.blocks.StructBlock([('text', wagtail.core.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())])), ('text_and_buttons', wagtail.core.blocks.StructBlock([('text', wagtail.core.blocks.TextBlock()), ('buttons', wagtail.core.blocks.ListBlock(wagtail.core.blocks.StructBlock([('button_text', wagtail.core.blocks.CharBlock(label='Text', max_length=50, required=True)), ('button_link', wagtail.core.blocks.CharBlock(label='Link', max_length=255, required=True))]))), ('mainbutton', wagtail.core.blocks.StructBlock([('button_text', wagtail.core.blocks.CharBlock(label='Text', max_length=50, required=True)), ('button_link', wagtail.core.blocks.CharBlock(label='Link', max_length=255, required=True))]))])), ('page', wagtail.core.blocks.PageChooserBlock()), ('text_with_callable', wagtail.core.blocks.StructBlock([('text', wagtail.core.blocks.CharBlock()), ('integer', wagtail.core.blocks.IntegerBlock()), ('decimal', wagtail.core.blocks.FloatBlock())]))]),
),
]
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
Empty file added example/news/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions example/news/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import factory
import wagtail_factories

from news.models import NewsPage


class NewsPageFactory(wagtail_factories.PageFactory):
title = factory.Sequence(lambda n: f"News post {n}")

class Meta:
model = NewsPage
26 changes: 26 additions & 0 deletions example/news/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.13 on 2022-04-24 07:18

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('wagtailcore', '0066_collection_management_permissions'),
]

operations = [
migrations.CreateModel(
name='NewsPage',
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.page')),
],
options={
'abstract': False,
},
bases=('wagtailcore.page',),
),
]
Empty file.
5 changes: 5 additions & 0 deletions example/news/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from wagtail.core.models import Page


class NewsPage(Page):
pass
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