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

Add querystring_search get method #1616

Merged
merged 10 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
1 change: 1 addition & 0 deletions news/1616.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add querystring_search get method. @robgietema
16 changes: 16 additions & 0 deletions src/plone/restapi/services/querystringsearch/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@
permission="zope2.View"
name="@querystring-search"
/>

<plone:service
method="GET"
factory=".get.QuerystringSearchGet"
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
permission="zope2.View"
name="@querystring-search"
/>

<plone:service
method="GET"
factory=".get.QuerystringSearchGet"
for="Products.CMFCore.interfaces.IContentish"
permission="zope2.View"
name="@querystring-search"
/>
</configure>
30 changes: 27 additions & 3 deletions src/plone/restapi/services/querystringsearch/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
from plone.restapi.deserializer import json_body
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.services import Service
from urllib import parse
from zope.component import getMultiAdapter

import json


zcatalog_version = get_distribution("Products.ZCatalog").version
if parse_version(zcatalog_version) >= parse_version("5.1"):
Expand All @@ -14,11 +17,14 @@
SUPPORT_NOT_UUID_QUERIES = False


class QuerystringSearchPost(Service):
robgietema marked this conversation as resolved.
Show resolved Hide resolved
class QuerystringSearch:
"""Returns the querystring search results given a p.a.querystring data."""

def reply(self):
data = json_body(self.request)
def __init__(self, context, request):
self.context = context
self.request = request

def __call__(self, data):
query = data.get("query", None)
b_start = int(data.get("b_start", 0))
b_size = int(data.get("b_size", 25))
Expand Down Expand Up @@ -60,3 +66,21 @@ def reply(self):
fullobjects=fullobjects
)
return results


class QuerystringSearchPost(Service):
"""Returns the querystring search results given a p.a.querystring data."""

def reply(self):
querystring_search = QuerystringSearch(self.context, self.request)
return querystring_search(data=json_body(self.request))


class QuerystringSearchGet(Service):
"""Returns the querystring search results given a p.a.querystring data."""

def reply(self):
querystring_search = QuerystringSearch(self.context, self.request)
return querystring_search(
data=json.loads(parse.unquote(self.request.form.get("query", "{}")))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /plone/@querystring-search?query=%257B%2522query%2522%253A%255B%257B%2522i%2522%253A%2522portal_type%2522%252C%2522o%2522%253A%2520%2522plone.app.querystring.operation.selection.any%2522%252C%2522v%2522%253A%255B%2522Document%2522%255D%257D%255D%257D HTTP/1.1
Accept: application/json
Authorization: Basic YWRtaW46c2VjcmV0
23 changes: 23 additions & 0 deletions src/plone/restapi/tests/http-examples/querystringsearch_get.resp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
HTTP/1.1 200 OK
Content-Type: application/json

{
"@id": "http://localhost:55001/plone/@querystring-search?query=%257B%2522query%2522%253A%255B%257B%2522i%2522%253A%2522portal_type%2522%252C%2522o%2522%253A%2520%2522plone.app.querystring.operation.selection.any%2522%252C%2522v%2522%253A%255B%2522Document%2522%255D%257D%255D%257D",
"items": [
{
"@id": "http://localhost:55001/plone/front-page",
"@type": "Document",
"description": "Congratulations! You have successfully installed Plone.",
"review_state": "private",
"title": "Welcome to Plone"
},
{
"@id": "http://localhost:55001/plone/testdocument",
"@type": "Document",
"description": "",
"review_state": "private",
"title": "Test Document"
}
],
"items_total": 2
}
12 changes: 12 additions & 0 deletions src/plone/restapi/tests/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,18 @@ def test_querystringsearch_post(self):
)
save_request_and_response_for_docs("querystringsearch_post", response)

def test_querystringsearch_get(self):
query = {
"query": "%7B%22query%22%3A%5B%7B%22i%22%3A%22portal_type%22%2C%22o%22%3A%20%22plone.app.querystring.operation.selection.any%22%2C%22v%22%3A%5B%22Document%22%5D%7D%5D%7D"
}
url = "/@querystring-search"

self.portal.invokeFactory("Document", "testdocument", title="Test Document")
transaction.commit()

response = self.api_session.get(url, params=query)
save_request_and_response_for_docs("querystringsearch_get", response)

def test_system_get(self):
response = self.api_session.get("/@system")
save_request_for_docs("system_get", response)
Expand Down
12 changes: 12 additions & 0 deletions src/plone/restapi/tests/test_services_querystringsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ def test_querystringsearch_basic(self):
self.assertEqual(len(response.json()["items"]), 1)
self.assertNotIn("effective", response.json()["items"][0])

def test_querystringsearch_basic_get(self):
response = self.api_session.get(
"/@querystring-search?query=%7B%22query%22%3A%5B%7B%22i%22%3A%22portal_type%22%2C%22o%22%3A%20%22plone.app.querystring.operation.selection.any%22%2C%22v%22%3A%5B%22Document%22%5D%7D%5D%7D"
)

self.assertEqual(response.status_code, 200)
self.assertIn("items", response.json())
self.assertIn("items_total", response.json())
self.assertEqual(response.json()["items_total"], 1)
self.assertEqual(len(response.json()["items"]), 1)
self.assertNotIn("effective", response.json()["items"][0])

def test_querystringsearch_fullobjects(self):
response = self.api_session.post(
"/@querystring-search",
Expand Down