From da27e16f91162cf365fbb13fc360e2caf5143260 Mon Sep 17 00:00:00 2001 From: Dan Braghis Date: Fri, 25 Feb 2022 17:22:52 +0000 Subject: [PATCH] Add language code filter on pages query --- grapple/types/pages.py | 43 ++++++++++++++++++++++++++----------- grapple/types/structures.py | 2 +- tests/settings.py | 3 ++- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/grapple/types/pages.py b/grapple/types/pages.py index 58a460db..c741976e 100644 --- a/grapple/types/pages.py +++ b/grapple/types/pages.py @@ -1,5 +1,8 @@ +from __future__ import annotations + import graphene +from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.db.models import Q from django.utils.module_loading import import_string @@ -303,41 +306,51 @@ def PagesQuery(): registry.pages[type(WagtailPage)] = Page class Mixin: - pages = QuerySetList( - graphene.NonNull(get_page_interface), - content_type=graphene.Argument( + pages_kwargs: dict[str, graphene.Argument | bool] = { + "content_type": graphene.Argument( graphene.String, description=_( - "Filter by content type. Uses the `app.Model` notation. Accepts a comma separated list of content types." + "Filter by content type. Uses the `app.Model` notation. " + "Accepts a comma separated list of content types." ), ), - in_site=graphene.Argument( + "in_site": graphene.Argument( graphene.Boolean, description=_("Filter to pages in the current site only."), default_value=False, ), - site=graphene.Argument( + "site": graphene.Argument( graphene.String, - description=_("Filter to pages in the give site."), + description=_("Filter to pages in the given site."), ), - ancestor=graphene.Argument( + "ancestor": graphene.Argument( graphene.ID, description=_( "Filter to pages that are descendants of the given page." ), required=False, ), - parent=graphene.Argument( + "parent": graphene.Argument( graphene.ID, description=_( "Filter to pages that are children of the given page. " - "When using both `parent` and `ancestor`, then `parent` will take precendence." + "When using both `parent` and `ancestor`, then `parent` will take precedence." ), required=False, ), - enable_search=True, - required=True, - ) + "enable_search": True, + "required": True, + } + if getattr(settings, "WAGTAIL_I18N_ENABLED", False): + pages_kwargs["locale"] = graphene.Argument( + graphene.String, + description=_( + "Filter to pages with the given locale code as defined in `WAGTAIL_CONTENT_LANGUAGES`." + ), + ) + + pages = QuerySetList(graphene.NonNull(get_page_interface), **pages_kwargs) + page = graphene.Field( get_page_interface(), id=graphene.ID(), @@ -413,6 +426,10 @@ def resolve_pages(self, info, **kwargs): ) pages = pages.filter(filters) + locale = kwargs.pop("locale", "") + if locale and getattr(settings, "WAGTAIL_I18N_ENABLED", False): + pages = pages.filter(locale__language_code=locale) + return resolve_queryset(pages, info, **kwargs) # Return a specific page, identified by ID or Slug. diff --git a/grapple/types/structures.py b/grapple/types/structures.py index 3262c4b2..0b91d752 100644 --- a/grapple/types/structures.py +++ b/grapple/types/structures.py @@ -25,7 +25,7 @@ class QuerySetList(graphene.List): """ List type with arguments used by Django's query sets. - This list setts the following arguments on itself: + This list sets the following arguments on itself: * ``id`` * ``limit`` diff --git a/tests/settings.py b/tests/settings.py index 54beee43..b3a116fc 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -164,6 +164,8 @@ # when testing with Wagtail 2.0 WAGTAILADMIN_BASE_URL = "http://localhost:8000" +WAGTAIL_I18N_ENABLED = True + CORS_ORIGIN_ALLOW_ALL = True # Grapple Config: @@ -193,7 +195,6 @@ EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" - try: from .local import * # noqa: F403 except ImportError: