Skip to content

Commit 14118fa

Browse files
committed
Add language code filter on pages query
1 parent 6b01779 commit 14118fa

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

example/example/settings/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@
180180

181181
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
182182

183+
WAGTAIL_I18N_ENABLED = True
184+
183185
try:
184186
import channels
185187

grapple/types/pages.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import graphene
2+
from django.conf import settings
23
from django.contrib.contenttypes.models import ContentType
34
from django.dispatch import receiver
45
from django.utils.translation import gettext_lazy as _
@@ -223,20 +224,29 @@ def PagesQuery():
223224
registry.pages[type(WagtailPage)] = Page
224225

225226
class Mixin:
226-
pages = QuerySetList(
227-
graphene.NonNull(lambda: PageInterface),
228-
content_type=graphene.Argument(
227+
pages_kwargs = {
228+
"content_type": graphene.Argument(
229229
graphene.String,
230230
description=_("Filter by content type. Uses the `app.Model` notation."),
231231
),
232-
in_site=graphene.Argument(
232+
"in_site": graphene.Argument(
233233
graphene.Boolean,
234234
description=_("Filter to pages in the current site only."),
235235
default_value=False,
236236
),
237-
enable_search=True,
238-
required=True,
239-
)
237+
"enable_search": True,
238+
"required": True,
239+
}
240+
if getattr(settings, "WAGTAIL_I18N_ENABLED", False):
241+
pages_kwargs["language_code"] = graphene.Argument(
242+
graphene.String,
243+
description=_(
244+
"Filter to pages with the given language code as defined in `WAGTAIL_CONTENT_LANGUAGES`."
245+
),
246+
)
247+
248+
pages = QuerySetList(graphene.NonNull(lambda: PageInterface), **pages_kwargs)
249+
240250
page = graphene.Field(
241251
PageInterface,
242252
id=graphene.Int(),
@@ -289,6 +299,10 @@ def resolve_pages(self, info, **kwargs):
289299
else:
290300
pages = pages.filter(content_type=ctype)
291301

302+
language_code = kwargs.pop("language_code", "")
303+
if language_code and getattr(settings, "WAGTAIL_I18N_ENABLED", False):
304+
pages = pages.filter(locale__language_code=language_code)
305+
292306
return resolve_queryset(pages, info, **kwargs)
293307

294308
# Return a specific page, identified by ID or Slug.

grapple/types/structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class QuerySetList(graphene.List):
2525
"""
2626
List type with arguments used by Django's query sets.
2727
28-
This list setts the following arguments on itself:
28+
This list sets the following arguments on itself:
2929
3030
* ``id``
3131
* ``limit``

0 commit comments

Comments
 (0)