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

feat: in_menu filter #402

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions grapple/types/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,40 @@ class PageInterface(graphene.Interface):

parent = graphene.Field(get_page_interface)
children = QuerySetList(
graphene.NonNull(get_page_interface), enable_search=True, required=True
graphene.NonNull(get_page_interface),
enable_search=True,
required=True,
enable_in_menu=True,
)
siblings = QuerySetList(
graphene.NonNull(get_page_interface), enable_search=True, required=True
graphene.NonNull(get_page_interface),
enable_search=True,
required=True,
enable_in_menu=True,
)
next_siblings = QuerySetList(
graphene.NonNull(get_page_interface), enable_search=True, required=True
graphene.NonNull(get_page_interface),
enable_search=True,
required=True,
enable_in_menu=True,
)
previous_siblings = QuerySetList(
graphene.NonNull(get_page_interface), enable_search=True, required=True
graphene.NonNull(get_page_interface),
enable_search=True,
required=True,
enable_in_menu=True,
)
descendants = QuerySetList(
graphene.NonNull(get_page_interface), enable_search=True, required=True
graphene.NonNull(get_page_interface),
enable_search=True,
required=True,
enable_in_menu=True,
)
ancestors = QuerySetList(
graphene.NonNull(get_page_interface), enable_search=True, required=True
graphene.NonNull(get_page_interface),
enable_search=True,
required=True,
enable_in_menu=True,
)

search_score = graphene.Float()
Expand Down
1 change: 1 addition & 0 deletions grapple/types/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class Mixin:
required=False,
),
enable_search=True,
enable_in_menu=True,
required=True,
)
page = graphene.Field(
Expand Down
1 change: 1 addition & 0 deletions grapple/types/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SiteObjectType(DjangoObjectType):
description=_("Filter by content type. Uses the `app.Model` notation."),
),
enable_search=True,
enable_in_menu=True,
required=True,
)
page = graphene.Field(
Expand Down
22 changes: 22 additions & 0 deletions grapple/types/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ class QuerySetList(graphene.List):
This list setts the following arguments on itself:

* ``id``
* ``in_menu``
* ``limit``
* ``offset``
* ``search_query``
* ``order``

:param enable_in_menu: Enable in_menu filter.
:type enable_in_menu: bool
:param enable_limit: Enable limit argument.
:type enable_limit: bool
:param enable_offset: Enable offset argument.
Expand All @@ -44,6 +47,7 @@ class QuerySetList(graphene.List):
"""

def __init__(self, of_type, *args, **kwargs):
enable_in_menu = kwargs.pop("enable_in_menu", False)
enable_limit = kwargs.pop("enable_limit", True)
enable_offset = kwargs.pop("enable_offset", True)
enable_search = kwargs.pop("enable_search", True)
Expand All @@ -58,6 +62,16 @@ def __init__(self, of_type, *args, **kwargs):
f"{of_type} is not a subclass of DjangoObjectType and it "
"cannot be used with QuerySetList."
)

# Enable in_menu for Page models.
if enable_in_menu is True and "in_menu" not in kwargs:
kwargs["in_menu"] = graphene.Argument(
graphene.Boolean,
description=_(
"Filter pages by Page.show_in_menus property. ie) The "
"'show in menus' checkbox in the page editor is checked."
dopry marked this conversation as resolved.
Show resolved Hide resolved
),
)
# Enable limiting on the queryset.
if enable_limit is True and "limit" not in kwargs:
kwargs["limit"] = graphene.Argument(
Expand Down Expand Up @@ -141,6 +155,7 @@ def PaginatedQuerySet(of_type, type_class, **kwargs):
This type setts the following arguments on itself:

* ``id``
* ``in_menu``
* ``page``
* ``per_page``
* ``search_query``
Expand All @@ -152,6 +167,7 @@ def PaginatedQuerySet(of_type, type_class, **kwargs):
:type enable_order: bool
"""

enable_in_menu = kwargs.pop("enable_in_menu", False)
enable_search = kwargs.pop("enable_search", True)
enable_order = kwargs.pop("enable_order", True)
required = kwargs.get("required", False)
Expand All @@ -168,6 +184,12 @@ def PaginatedQuerySet(of_type, type_class, **kwargs):
"cannot be used with QuerySetList."
)

# Enable in_menu for Page models.
if enable_in_menu is True and "in_menu" not in kwargs:
kwargs["in_menu"] = graphene.Argument(
graphene.Boolean, description=_("Filter by in menu.")
zerolab marked this conversation as resolved.
Show resolved Hide resolved
)

# Enable page for Django Paginator.
if "page" not in kwargs:
kwargs["page"] = graphene.Argument(
Expand Down
5 changes: 5 additions & 0 deletions grapple/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def resolve_queryset(
id=None,
order=None,
collection=None,
in_menu=None,
**kwargs,
):
"""
Expand All @@ -125,6 +126,10 @@ def resolve_queryset(

qs = qs.all() if id is None else qs.filter(pk=id)

# filter by in_menu
if in_menu is not None:
qs = qs.in_menu() if in_menu else qs.not_in_menu()

order_by_relevance = True
if order is not None:
qs = qs.order_by(*(x.strip() for x in order.split(",")))
Expand Down
27 changes: 26 additions & 1 deletion tests/test_grapple.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ class PagesSearchTest(BaseGrappleTest):
@classmethod
def setUpTestData(cls):
cls.home = HomePage.objects.first()
BlogPageFactory(title="Alpha", parent=cls.home)
BlogPageFactory(title="Alpha", parent=cls.home, show_in_menus=True)
BlogPageFactory(title="Alpha Alpha", parent=cls.home)
BlogPageFactory(title="Alpha Beta", parent=cls.home)
BlogPageFactory(title="Alpha Gamma", parent=cls.home)
Expand Down Expand Up @@ -568,6 +568,31 @@ def test_explicit_order(self):
self.assertEqual(page_data[4]["title"], "Beta Gamma")
self.assertEqual(page_data[5]["title"], "Alpha Gamma")

def test_search_in_menus(self):
query = """
query($searchQuery: String, $inMenu: Boolean) {
pages(searchQuery: $searchQuery, inMenu: $inMenu) {
title
}
}
"""
executed = self.client.execute(query, variables={"inMenu": True})
page_data = executed["data"].get("pages")
self.assertEqual(len(page_data), 1)
self.assertEqual(page_data[0]["title"], "Alpha")

def test_search_not_in_menus(self):
query = """
query($searchQuery: String, $inMenu: Boolean) {
pages(searchQuery: $searchQuery, inMenu: $inMenu, limit: 100) {
title
}
}
"""
executed = self.client.execute(query, variables={"inMenu": False})
page_data = executed["data"].get("pages")
self.assertEqual(len(page_data), 12) # 11 blog pages + home page


class PageUrlPathTest(BaseGrappleTest):
def _query_by_path(self, path, *, in_site=False):
Expand Down
Loading