Skip to content

Commit

Permalink
Support comma separated list of content types for the content_type fi…
Browse files Browse the repository at this point in the history
…lter on pages query (#250)
  • Loading branch information
dopry committed Aug 9, 2022
1 parent 1613c6e commit a05e4b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/general-usage/graphql-types.rst
Expand Up @@ -70,7 +70,7 @@ accepts the following arguments:
offset: PositiveInt
order: String
searchQuery: String
contentType: String
contentType: String # comma separated list of content types in app.Model notation
inSite: Boolean


Expand Down
10 changes: 10 additions & 0 deletions example/example/tests/test_grapple.py
Expand Up @@ -156,6 +156,16 @@ def test_pages_content_type_filter(self):
[int(p["id"]) for p in data], [self.blog_post.id, another_post.id]
)

results = self.client.execute(
query, variables={"content_type": "home.HomePage,home.BlogPage"}
)
data = results["data"]["pages"]
self.assertEquals(len(data), 3)
self.assertListEqual(
[int(p["id"]) for p in data],
[self.home.id, self.blog_post.id, another_post.id],
)

results = self.client.execute(
query, variables={"content_type": "bogus.ContentType"}
)
Expand Down
19 changes: 13 additions & 6 deletions grapple/types/pages.py
@@ -1,5 +1,6 @@
import graphene
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _
from graphene_django.types import DjangoObjectType
Expand Down Expand Up @@ -233,7 +234,9 @@ class Mixin:
graphene.NonNull(lambda: PageInterface),
content_type=graphene.Argument(
graphene.String,
description=_("Filter by content type. Uses the `app.Model` notation."),
description=_(
"Filter by content type. Uses the `app.Model` notation. Accepts a comma separated list of content types."
),
),
in_site=graphene.Argument(
graphene.Boolean,
Expand Down Expand Up @@ -284,11 +287,15 @@ def resolve_pages(self, info, **kwargs):
pages = pages.in_site(site)

content_type = kwargs.pop("content_type", None)
if content_type:
app_label, model = content_type.strip().lower().split(".")
pages = pages.filter(
content_type__app_label=app_label, content_type__model=model
)
content_types = content_type.split(",") if content_type else None
if content_types:
filters = Q()
for content_type in content_types:
app_label, model = content_type.strip().lower().split(".")
filters |= Q(
content_type__app_label=app_label, content_type__model=model
)
pages = pages.filter(filters)

return resolve_queryset(pages, info, **kwargs)

Expand Down

0 comments on commit a05e4b3

Please sign in to comment.