Skip to content

Commit

Permalink
Add language code filter on pages query
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Oct 1, 2023
1 parent 5818f7b commit da27e16
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
43 changes: 30 additions & 13 deletions grapple/types/pages.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion grapple/types/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
3 changes: 2 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -193,7 +195,6 @@

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"


try:
from .local import * # noqa: F403
except ImportError:
Expand Down

0 comments on commit da27e16

Please sign in to comment.