Skip to content

Commit b9df214

Browse files
authored
Merge pull request #197 from plone/blocktypes
Add endpoint to access block_types indexer
2 parents 75100ae + b25f101 commit b9df214

File tree

17 files changed

+291
-2
lines changed

17 files changed

+291
-2
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,47 @@ By default it is only added for new Plone sites.
111111
To add it to an existing site, run `plone.volto.upgrades.add_block_types_index` manually.
112112

113113

114+
### Block types endpoint
115+
116+
`plone.volto` exposes the `block_types` index via the `/@blocktypes` REST API endpoint.
117+
It can be used to get information about which and how many blocks are used where.
118+
119+
```py
120+
import requests
121+
requests.get("http://localhost:8080/Plone/@blocktypes")
122+
```
123+
124+
```json
125+
{
126+
"title": 3,
127+
"slate": 18,
128+
"teaser": 6,
129+
}
130+
```
131+
132+
It can also be used to get detailed information where a block is used and how often:
133+
134+
```py
135+
import requests
136+
requests.get("http://localhost:8080/Plone/@blocktypes/teaser")
137+
```
138+
139+
```json
140+
[
141+
{
142+
"@id": "http://localhost:8080/Plone",
143+
"title": "Website",
144+
"count": 2,
145+
},
146+
{
147+
"@id": "http://localhost:8080/Plone/lorem-ipsum",
148+
"title": "Lorem Ipsum",
149+
"count": 4,
150+
}
151+
]
152+
```
153+
154+
114155
### Multilingual support
115156

116157
`plone.volto` supports multilingual websites.

news/+block_types_count.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a `block_types` metadata column to the catalog to include a count for each type. @jnptk

news/+block_types_service.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `/@blocktypes` endpoint to expose `block_types` index. @jnptk

src/plone/volto/configure.zcml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010

1111
<i18n:registerTranslations directory="locales" />
1212

13+
<include
14+
package="Products.CMFCore"
15+
file="permissions.zcml"
16+
/>
17+
1318
<include file="dependencies.zcml" />
19+
<include file="permissions.zcml" />
1420

1521
<include package=".behaviors" />
1622
<include package=".browser" />
1723
<include package=".indexers" />
24+
<include package=".services" />
1825

1926
<include file="profiles.zcml" />
2027
<include file="patches.zcml" />

src/plone/volto/indexers/indexers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from plone.restapi.blocks import visit_blocks
55
from plone.volto.behaviors.preview import IPreview
66

7+
import collections
8+
79

810
@indexer(IPreview)
911
def hasPreviewImage(obj):
@@ -40,9 +42,9 @@ def image_field_indexer(obj):
4042
def block_types_indexer(obj):
4143
"""Indexer for all block types included in a page."""
4244
obj = aq_base(obj)
43-
block_types = set()
45+
block_types = collections.Counter()
4446
for block in visit_blocks(obj, obj.blocks):
4547
block_type = block.get("@type")
4648
if block_type:
47-
block_types.add(block_type)
49+
block_types[block_type] += 1
4850
return block_types

src/plone/volto/permissions.zcml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<configure xmlns="http://namespaces.zope.org/zope">
2+
3+
<permission
4+
id="plone.volto.service.BlockTypes"
5+
title="Plone Site Setup: Block Types"
6+
/>
7+
8+
</configure>

src/plone/volto/profiles/default/catalog.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<column value="head_title" />
55
<column value="hasPreviewImage" />
66
<column value="image_field" />
7+
<column value="block_types" />
78
<index meta_type="KeywordIndex"
89
name="block_types"
910
>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<rolemap>
3+
<permissions>
4+
<permission acquire="True"
5+
name="Plone Site Setup: Block Types"
6+
>
7+
<role name="Manager" />
8+
<role name="Site Administrator" />
9+
</permission>
10+
</permissions>
11+
</rolemap>

src/plone/volto/services/__init__.py

Whitespace-only changes.

src/plone/volto/services/blocktypes/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)