Skip to content

Commit 5dc3c9b

Browse files
committed
feat: add concept of BlockPresenter for NotionScripts
The idea here is that the block presenter wraps the block result from notion-py to be used in the API. It inherits from 'dict' so that jsonify will auto convert it to the raw values that the block presenter has.
1 parent bde9552 commit 5dc3c9b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env -S PATH="${PATH}:/usr/local/bin" python3
2+
3+
from notion.collection import CollectionRowBlock
4+
5+
6+
class BlockPresenter(dict):
7+
8+
def __init__(self, block):
9+
self.block = block
10+
if isinstance(block, CollectionRowBlock):
11+
dict.__init__(self, **{"id": block.id, **block.get_all_properties()})
12+
else:
13+
dict.__init__(self, **{"id": block.id, "title": block.title})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from notionscripts.block_presenter import BlockPresenter
2+
3+
from .notion_api_page_helper import get_test_page, create_collection_view
4+
5+
from notion.block import TextBlock
6+
7+
import json
8+
9+
import pytest # noqa, F401
10+
11+
12+
def test_block_presentation(notion_token):
13+
test_page = get_test_page()
14+
15+
block = test_page.children.add_new(TextBlock, title="textblock")
16+
17+
assert BlockPresenter(block).block == block
18+
assert BlockPresenter(block) == {"id": block.id, "title": "textblock"}
19+
assert json.dumps(BlockPresenter(block)) == '{"id": "%s", "title": "textblock"}' % block.id
20+
21+
22+
def test_collection_row_block_presentation(notion_token):
23+
collection_view = create_collection_view()
24+
block = collection_view.collection.add_row(name="test row", value=10, enabled=True)
25+
26+
assert BlockPresenter(block).block == block
27+
assert BlockPresenter(block) == {"id": block.id, "enabled": True, "tags": [], "category": None, "value": 10, "name": "test row"}
28+
assert json.dumps(BlockPresenter(block)) == '{"id": "%s", "enabled": true, "tags": [], "category": null, "value": 10, "name": "test row"}' % block.id

0 commit comments

Comments
 (0)