Skip to content

Commit 88f2bc9

Browse files
Faakhir30stevepiercydavisagli
authored
add query param to search registry records. (#1861)
* add query param to search registry records. * refactor serializer. * format using black. * use tmp registry isntead of seperate serializer class. * udpate http resp files * update docs. * update docs. * version added * Update docs/source/endpoints/registry.md * Apply suggestions from code review --------- Co-authored-by: Steve Piercy <web@stevepiercy.com> Co-authored-by: David Glick <david@glicksoftware.com>
1 parent b8f1594 commit 88f2bc9

File tree

7 files changed

+105
-1
lines changed

7 files changed

+105
-1
lines changed

docs/source/endpoints/registry.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ Example response:
5252
:language: http
5353
```
5454

55+
## Filter list of registry records
56+
57+
```{versionadded} plone.restapi 9.10.0
58+
```
59+
60+
You can filter a list of registry records and batch the results.
61+
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.
62+
See {doc}`../usage/batching` for details of how to work with batched results.
63+
64+
```{eval-rst}
65+
.. http:example:: curl httpie python-requests
66+
:request: ../../../src/plone/restapi/tests/http-examples/registry_get_list_filtered.req
67+
```
68+
69+
Example response:
70+
71+
```{literalinclude} ../../../src/plone/restapi/tests/http-examples/registry_get_list_filtered.resp
72+
:language: http
73+
```
5574

5675
## Updating registry records
5776

news/1861.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In the `@registry` endpoint, added support for filtering the list of registry records. @Faakhir30

src/plone/restapi/services/registry/get.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from plone.registry import Registry
12
from plone.registry.interfaces import IRegistry
23
from plone.restapi.interfaces import ISerializeToJson
34
from plone.restapi.serializer.converters import json_compatible
@@ -35,5 +36,15 @@ def reply(self):
3536
value = registry[self._get_record_name]
3637
return json_compatible(value)
3738
else: # batched listing
38-
serializer = getMultiAdapter((registry, self.request), ISerializeToJson)
39+
if q := self.request.form.get("q"):
40+
41+
tmp_registry = Registry()
42+
for key in registry.records.keys():
43+
if key.startswith(q):
44+
tmp_registry.records[key] = registry.records[key]
45+
registry = tmp_registry
46+
serializer = getMultiAdapter(
47+
(registry, self.request),
48+
ISerializeToJson,
49+
)
3950
return serializer()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GET /plone/@registry?q=Products.CMFPlone HTTP/1.1
2+
Accept: application/json
3+
Authorization: Basic YWRtaW46c2VjcmV0
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
HTTP/1.1 200 OK
2+
Content-Type: application/json
3+
4+
{
5+
"@id": "http://localhost:55001/plone/@registry?q=Products.CMFPlone",
6+
"items": [
7+
{
8+
"name": "Products.CMFPlone.i18nl10n.override_dateformat.Enabled",
9+
"schema": {
10+
"properties": {
11+
"description": "Override the translation machinery",
12+
"factory": "Yes/No",
13+
"title": "Enabled",
14+
"type": "boolean"
15+
}
16+
},
17+
"value": false
18+
},
19+
{
20+
"name": "Products.CMFPlone.i18nl10n.override_dateformat.date_format_long",
21+
"schema": {
22+
"properties": {
23+
"description": "Default value: %Y-%m-%d %H:%M (2038-01-19 03:14)",
24+
"factory": "Text line (String)",
25+
"title": "old ZMI property: localLongTimeFormat",
26+
"type": "string"
27+
}
28+
},
29+
"value": "%Y-%m-%d %H:%M"
30+
},
31+
{
32+
"name": "Products.CMFPlone.i18nl10n.override_dateformat.date_format_short",
33+
"schema": {
34+
"properties": {
35+
"description": "Default value: %Y-%m-%d (2038-01-19)",
36+
"factory": "Text line (String)",
37+
"title": "old ZMI property: localTimeFormat",
38+
"type": "string"
39+
}
40+
},
41+
"value": "%Y-%m-%d"
42+
},
43+
{
44+
"name": "Products.CMFPlone.i18nl10n.override_dateformat.time_format",
45+
"schema": {
46+
"properties": {
47+
"description": "Default value: %H:%M (03:14)",
48+
"factory": "Text line (String)",
49+
"title": "old ZMI property: localTimeOnlyFormat",
50+
"type": "string"
51+
}
52+
},
53+
"value": "%H:%M"
54+
}
55+
],
56+
"items_total": 4
57+
}

src/plone/restapi/tests/test_documentation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ def test_documentation_registry_get_list(self):
549549
response = self.api_session.get("/@registry")
550550
save_request_and_response_for_docs("registry_get_list", response)
551551

552+
def test_documentation_registry_get_list_filtered(self):
553+
response = self.api_session.get("/@registry?q=Products.CMFPlone")
554+
save_request_and_response_for_docs("registry_get_list_filtered", response)
555+
552556
def test_documentation_types(self):
553557
response = self.api_session.get("/@types")
554558
save_request_and_response_for_docs("types", response)

src/plone/restapi/tests/test_registry.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,12 @@ def test_get_listing(self):
107107
self.assertIn("items", response)
108108
self.assertIn("batching", response)
109109
self.assertIn("next", response["batching"])
110+
111+
def test_get_filtered_listing(self):
112+
response = self.api_session.get("/@registry?q=foo.bar1")
113+
self.assertEqual(response.status_code, 200)
114+
response = response.json()
115+
# 10 records from foo.bar10 to foo.bar19 and 1 record foo.bar1
116+
self.assertEqual(len(response["items"]), 11)
117+
self.assertEqual(response["items"][0]["name"], "foo.bar1")
118+
self.assertEqual(response["items"][0]["value"], "Lorem Ipsum")

0 commit comments

Comments
 (0)