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 query param to search registry records. #1861

Merged
merged 12 commits into from
Jan 22, 2025
20 changes: 20 additions & 0 deletions docs/source/endpoints/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ Example response:
:language: http
```

## Filter list of registry records


stevepiercy marked this conversation as resolved.
Show resolved Hide resolved
```{versionadded} plone.restapi 9.10.0
```

You can filter a list of registry records and batch the results.
To do so, append a query string to the listing endpoint with a `q` parameter and its value set to the prefix of the desired record name.
See {doc}`../usage/batching` for details of how to work with batched results.

```{eval-rst}
.. http:example:: curl httpie python-requests
:request: ../../../src/plone/restapi/tests/http-examples/registry_get_list_filtered.req
```

Example response:

```{literalinclude} ../../../src/plone/restapi/tests/http-examples/registry_get_list_filtered.resp
:language: http
```

## Updating registry records

Expand Down
1 change: 1 addition & 0 deletions news/1861.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support filtering the list of registry records. @Faakhir30
davisagli marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 19 additions & 1 deletion src/plone/restapi/services/registry/get.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from plone.registry import Registry
from plone.registry.interfaces import IRegistry
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.serializer.converters import json_compatible
Expand All @@ -6,6 +7,8 @@
from zope.component import getUtility
from zope.interface import implementer
from zope.publisher.interfaces import IPublishTraverse
import plone.protect.interfaces
from zope.interface import alsoProvides
davisagli marked this conversation as resolved.
Show resolved Hide resolved


@implementer(IPublishTraverse)
Expand Down Expand Up @@ -35,5 +38,20 @@ def reply(self):
value = registry[self._get_record_name]
return json_compatible(value)
else: # batched listing
serializer = getMultiAdapter((registry, self.request), ISerializeToJson)
if q := self.request.form.get("q"):
# Disable CSRF protection
if "IDisableCSRFProtection" in dir(plone.protect.interfaces):
alsoProvides(
self.request, plone.protect.interfaces.IDisableCSRFProtection
)
davisagli marked this conversation as resolved.
Show resolved Hide resolved

tmp_registry = Registry()
for key in registry.records.keys():
if key.startswith(q):
tmp_registry.records[key] = registry.records[key]
registry = tmp_registry
serializer = getMultiAdapter(
(registry, self.request),
ISerializeToJson,
)
return serializer()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /plone/@registry?q=Products.CMFPlone HTTP/1.1
Accept: application/json
Authorization: Basic YWRtaW46c2VjcmV0
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
HTTP/1.1 200 OK
Content-Type: application/json

{
"@id": "http://localhost:55001/plone/@registry?q=Products.CMFPlone",
"items": [
{
"name": "Products.CMFPlone.i18nl10n.override_dateformat.Enabled",
"schema": {
"properties": {
"description": "Override the translation machinery",
"factory": "Yes/No",
"title": "Enabled",
"type": "boolean"
}
},
"value": false
},
{
"name": "Products.CMFPlone.i18nl10n.override_dateformat.date_format_long",
"schema": {
"properties": {
"description": "Default value: %Y-%m-%d %H:%M (2038-01-19 03:14)",
"factory": "Text line (String)",
"title": "old ZMI property: localLongTimeFormat",
"type": "string"
}
},
"value": "%Y-%m-%d %H:%M"
},
{
"name": "Products.CMFPlone.i18nl10n.override_dateformat.date_format_short",
"schema": {
"properties": {
"description": "Default value: %Y-%m-%d (2038-01-19)",
"factory": "Text line (String)",
"title": "old ZMI property: localTimeFormat",
"type": "string"
}
},
"value": "%Y-%m-%d"
},
{
"name": "Products.CMFPlone.i18nl10n.override_dateformat.time_format",
"schema": {
"properties": {
"description": "Default value: %H:%M (03:14)",
"factory": "Text line (String)",
"title": "old ZMI property: localTimeOnlyFormat",
"type": "string"
}
},
"value": "%H:%M"
}
],
"items_total": 4
}
4 changes: 4 additions & 0 deletions src/plone/restapi/tests/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ def test_documentation_registry_get_list(self):
response = self.api_session.get("/@registry")
save_request_and_response_for_docs("registry_get_list", response)

def test_documentation_registry_get_list_filtered(self):
response = self.api_session.get("/@registry?q=Products.CMFPlone")
save_request_and_response_for_docs("registry_get_list_filtered", response)

def test_documentation_types(self):
response = self.api_session.get("/@types")
save_request_and_response_for_docs("types", response)
Expand Down
9 changes: 9 additions & 0 deletions src/plone/restapi/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,12 @@ def test_get_listing(self):
self.assertIn("items", response)
self.assertIn("batching", response)
self.assertIn("next", response["batching"])

def test_get_filtered_listing(self):
response = self.api_session.get("/@registry?q=foo.bar1")
self.assertEqual(response.status_code, 200)
response = response.json()
# 10 records from foo.bar10 to foo.bar19 and 1 record foo.bar1
self.assertEqual(len(response["items"]), 11)
self.assertEqual(response["items"][0]["name"], "foo.bar1")
self.assertEqual(response["items"][0]["value"], "Lorem Ipsum")
Loading