Skip to content

Commit c5a2022

Browse files
committed
feat: more information on block children and collection view endpoints
This change puts more information on the blocks/<block_id>/children and /collections/<collection_id>/<view_id> endpoints. This allows the 'parent' block or collection itself to be represented in the response. Ultimately, this can save an API call to get the title of the parent and collection.
1 parent 449f236 commit c5a2022

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

server/src/api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ def block_children_view(notion_token, block_id):
9898
try:
9999
notion_api = NotionApi(notion_token)
100100

101-
children = notion_api.block_children(block_id)
101+
content = notion_api.block_children(block_id)
102102

103-
return jsonify(children=children), 200
103+
return jsonify(content), 200
104104
except Exception as error:
105105
return jsonify(error=str(error)), 500
106106

@@ -126,7 +126,7 @@ def collection_view(notion_token, collection_id, view_id):
126126

127127
content = notion_api.collection_view_content(collection_id, view_id)
128128

129-
return jsonify(rows=content), 200
129+
return jsonify(content), 200
130130
except Exception as error:
131131
return jsonify(error=str(error)), 500
132132

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env -S PATH="${PATH}:/usr/local/bin" python3
22

3-
from notion.collection import CollectionRowBlock
3+
from notion.collection import CollectionRowBlock, CollectionView
44

55

66
class BlockPresenter(dict):
@@ -9,5 +9,7 @@ def __init__(self, block):
99
self.block = block
1010
if isinstance(block, CollectionRowBlock):
1111
dict.__init__(self, **{"id": block.id, **block.get_all_properties()})
12+
elif isinstance(block, CollectionView):
13+
dict.__init__(self, **{"collection_id": block.parent.id, "view_id": block.id, "collection_title": block.parent.title, "view_title": block.name})
1214
else:
1315
dict.__init__(self, **{"id": block.id, "title": block.title})

shared/notionscripts/notion_api.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def block_content(self, block_id):
2727
def block_children(self, block_id):
2828
block = self.client().get_block(block_id)
2929

30-
return [BlockPresenter(child) for child in block.children]
30+
return {
31+
"parent": BlockPresenter(block),
32+
"children": [BlockPresenter(child) for child in block.children]
33+
}
3134

3235
def block_append(self, block_id, data):
3336
block = self.client().get_block(block_id)
@@ -58,7 +61,10 @@ def collection_view_content(self, collection_id, view_id):
5861
collection_view = self.__collection_view(collection_id, view_id)
5962
results = collection_view.default_query().execute()
6063

61-
return [BlockPresenter(row) for row in results]
64+
return {
65+
"collection": BlockPresenter(collection_view),
66+
"rows": [BlockPresenter(row) for row in results]
67+
}
6268

6369
def collection_append(self, collection_id, view_id, data):
6470
collection_view = self.__collection_view(collection_id, view_id)

shared/notionscripts/tests/notion_api_page_helper.py

+1
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,6 @@ def create_collection_view():
9090
)
9191
cvb.title = "Test collection"
9292
view = cvb.views.add_new(view_type="table")
93+
view.name = "Test view"
9394

9495
return view

shared/notionscripts/tests/test_notion_api.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def test_block_children(notion_token):
2828

2929
content = notion_api.block_children(block.id)
3030

31-
assert content[0] == {"id": child_block_1.id, "title": "child block 1"}
32-
assert content[1] == {"id": child_block_2.id, "title": "child block 2"}
31+
assert content["parent"] == {"id": block.id, "title": "a parent block"}
32+
assert content["children"][0] == {"id": child_block_1.id, "title": "child block 1"}
33+
assert content["children"][1] == {"id": child_block_2.id, "title": "child block 2"}
3334

3435

3536
def test_block_append(notion_token):
@@ -106,13 +107,19 @@ def test_collection_view_content(notion_token):
106107
notion_api = NotionApi(token=notion_token)
107108

108109
collection_view = create_collection_view()
109-
collection_id = collection_view.parent.id.replace("-", "")
110-
view_id = collection_view.id.replace("-", "")
110+
collection_id = collection_view.parent.id
111+
view_id = collection_view.id
111112

112113
collection_view.collection.add_row(name="test row")
113-
collection_view_content = notion_api.collection_view_content(collection_id, view_id)
114+
collection_view_content = notion_api.collection_view_content(collection_id.replace("-", ""), view_id.replace("-", ""))
114115

115-
assert collection_view_content[0]["name"] == "test row"
116+
assert collection_view_content["collection"] == {
117+
"collection_id": collection_id,
118+
"view_id": view_id,
119+
"collection_title": "Test collection",
120+
"view_title": "Test view"
121+
}
122+
assert collection_view_content["rows"][0]["name"] == "test row"
116123

117124

118125
def test_collection_view_append(notion_token):
@@ -125,6 +132,6 @@ def test_collection_view_append(notion_token):
125132
notion_api.collection_append(collection_id, view_id, {"enabled": True, "value": 10, "name": "test row"})
126133
collection_view_content = notion_api.collection_view_content(collection_id, view_id)
127134

128-
assert collection_view_content[0]["name"] == "test row"
129-
assert collection_view_content[0]["enabled"] is True
130-
assert collection_view_content[0]["value"] == 10
135+
assert collection_view_content["rows"][0]["name"] == "test row"
136+
assert collection_view_content["rows"][0]["enabled"] is True
137+
assert collection_view_content["rows"][0]["value"] == 10

0 commit comments

Comments
 (0)