Skip to content

Commit

Permalink
Merge 7e4a6c2 into 3d03e5b
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Apr 20, 2020
2 parents 3d03e5b + 7e4a6c2 commit bbed2ae
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions news/911.feature
@@ -0,0 +1,2 @@
- Make ``@querystring-search`` endpoint context aware
[sneridagh]
7 changes: 7 additions & 0 deletions src/plone/restapi/services/querystringsearch/configure.zcml
Expand Up @@ -10,4 +10,11 @@
permission="zope2.View"
/>

<plone:service
method="POST"
for="Products.CMFCore.interfaces.IContentish"
factory=".get.QuerystringSearchPost"
name="@querystring-search"
permission="zope2.View"
/>
</configure>
19 changes: 18 additions & 1 deletion src/plone/restapi/services/querystringsearch/get.py
@@ -1,9 +1,18 @@
# -*- coding: utf-8 -*-
from pkg_resources import get_distribution
from pkg_resources import parse_version
from plone.restapi.deserializer import json_body
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.services import Service
from Products.CMFPlone.interfaces import IPloneSiteRoot
from zope.component import getMultiAdapter

zcatalog_version = get_distribution("Products.ZCatalog").version
if parse_version(zcatalog_version) >= parse_version("5.1"):
SUPPORT_NOT_UUID_QUERIES = True
else:
SUPPORT_NOT_UUID_QUERIES = False


class QuerystringSearchPost(Service):
"""Returns the querystring search results given a p.a.querystring data.
Expand All @@ -28,7 +37,8 @@ def reply(self):
querybuilder = getMultiAdapter(
(self.context, self.request), name="querybuilderresults"
)
results = querybuilder(

querybuilder_parameters = dict(
query=query,
brains=True,
b_start=b_start,
Expand All @@ -38,6 +48,13 @@ def reply(self):
limit=limit,
)

if SUPPORT_NOT_UUID_QUERIES and not (IPloneSiteRoot.providedBy(self.context)):
querybuilder_parameters.update(
dict(custom_query={"UID": {"not": self.context.UID()}})
)

results = querybuilder(**querybuilder_parameters)

results = getMultiAdapter((results, self.request), ISerializeToJson)(
fullobjects=fullobjects
)
Expand Down
38 changes: 38 additions & 0 deletions src/plone/restapi/tests/test_services_querystringsearch.py
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from pkg_resources import get_distribution
from pkg_resources import parse_version
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
Expand All @@ -9,6 +11,12 @@
import transaction
import unittest

zcatalog_version = get_distribution("Products.ZCatalog").version
if parse_version(zcatalog_version) >= parse_version("5.1"):
SUPPORT_NOT_UUID_QUERIES = True
else:
SUPPORT_NOT_UUID_QUERIES = False


class TestQuerystringSearchEndpoint(unittest.TestCase):

Expand Down Expand Up @@ -129,3 +137,33 @@ def test_querystringsearch_complex(self):
self.assertEqual(len(response.json()["items"]), 5)
self.assertNotIn("effective", response.json()["items"][0])
self.assertEqual(response.json()["items"][4]["title"], u"Test Document 9")

@unittest.skipIf(
not SUPPORT_NOT_UUID_QUERIES,
"Skipping because ZCatalog allows not queries on UUIDIndex from >=5.1",
)
def test_querystringsearch_do_not_return_context(self):
self.portal.invokeFactory("Document", "testdocument2", title="Test Document 2")
self.doc = self.portal.testdocument

transaction.commit()

response = self.api_session.post(
"/testdocument/@querystring-search",
json={
"query": [
{
"i": "portal_type",
"o": "plone.app.querystring.operation.selection.is",
"v": ["Document"],
}
],
},
)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["items_total"], 1)
self.assertEqual(
response.json()["items"][0]["@id"],
"http://localhost:55001/plone/testdocument2",
)

0 comments on commit bbed2ae

Please sign in to comment.