Skip to content

Commit e5d1d07

Browse files
committed
feat: add the "block_type" attribute to the BlockPresenter
This will provide more information on what each block is when being returned. Had to update all the testing... which is tedious. I'll have to find something better for the JSON/attribute comparisons.
1 parent 6324280 commit e5d1d07

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

shared/notionscripts/block_presenter.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ class BlockPresenter(dict):
88

99
def __init__(self, block):
1010
self.block = block
11+
12+
default_attributes = {"id": block.id.replace("-", ""), "block_type": block.type}
13+
1114
if isinstance(block, CollectionRowBlock):
12-
dict.__init__(self, **{"id": block.id.replace("-", ""), **block.get_all_properties()})
15+
dict.__init__(self, **{**default_attributes, **block.get_all_properties()})
1316
elif isinstance(block, CollectionView):
1417
dict.__init__(self, **{"collection_id": block.parent.id.replace("-", ""), "view_id": block.id.replace("-", ""),
1518
"collection_title": block.parent.title, "view_title": block.name})
1619
elif isinstance(block, DividerBlock):
17-
dict.__init__(self, **{"id": block.id.replace("-", "")})
20+
dict.__init__(self, **{**default_attributes})
1821
else:
19-
dict.__init__(self, **{"id": block.id.replace("-", ""), "title": block.title})
22+
dict.__init__(self, **{**default_attributes, "title": block.title})

shared/notionscripts/tests/test_block_presenter.py

+28-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .notion_api_page_helper import get_test_page, create_collection_view
44

5-
from notion.block import TextBlock
5+
from notion.block import TextBlock, DividerBlock
66

77
import json
88

@@ -13,21 +13,43 @@ def clean_id(id):
1313
return id.replace("-", "")
1414

1515

16-
def test_block_presentation(notion_token):
16+
def test_text_block_presentation(notion_token):
1717
test_page = get_test_page()
1818

1919
block = test_page.children.add_new(TextBlock, title="textblock")
20+
assert BlockPresenter(block).block == block
21+
assert BlockPresenter(block) == {"id": clean_id(block.id), "block_type": "text", "title": "textblock"}
22+
assert json.dumps(BlockPresenter(block)) == '{"id": "%s", "block_type": "text", "title": "textblock"}' % clean_id(block.id)
23+
2024

25+
def test_divider_block_presentation(notion_token):
26+
test_page = get_test_page()
27+
28+
block = test_page.children.add_new(DividerBlock)
2129
assert BlockPresenter(block).block == block
22-
assert BlockPresenter(block) == {"id": clean_id(block.id), "title": "textblock"}
23-
assert json.dumps(BlockPresenter(block)) == '{"id": "%s", "title": "textblock"}' % clean_id(block.id)
30+
assert BlockPresenter(block) == {"id": clean_id(block.id), "block_type": "divider"}
31+
assert json.dumps(BlockPresenter(block)) == '{"id": "%s", "block_type": "divider"}' % clean_id(block.id)
2432

2533

2634
def test_collection_row_block_presentation(notion_token):
2735
collection_view = create_collection_view()
2836
block = collection_view.collection.add_row(name="test row", value=10, enabled=True)
2937

3038
assert BlockPresenter(block).block == block
31-
assert BlockPresenter(block) == {"id": clean_id(block.id), "enabled": True, "tags": [], "category": None, "value": 10, "name": "test row"}
39+
assert BlockPresenter(block) == {"id": clean_id(block.id), "block_type": "page", "enabled": True,
40+
"tags": [], "category": None, "value": 10, "name": "test row"}
41+
assert json.dumps(BlockPresenter(
42+
block)) == '{"id": "%s", "block_type": "page", "enabled": true, "tags": [], "category": null, "value": 10, "name": "test row"}' % clean_id(block.id)
43+
44+
45+
def test_collection_view_presentation(notion_token):
46+
collection_view = create_collection_view()
47+
collection_view.name = "Test view"
48+
49+
assert BlockPresenter(collection_view).block == collection_view
50+
assert BlockPresenter(collection_view) == {"collection_id": clean_id(collection_view.parent.id),
51+
"collection_title": "Test collection", "view_id": clean_id(collection_view.id), "view_title": "Test view"}
52+
53+
json_string_template = '{"collection_id": "%s", "view_id": "%s", "collection_title": "Test collection", "view_title": "Test view"}'
3254
assert json.dumps(BlockPresenter(
33-
block)) == '{"id": "%s", "enabled": true, "tags": [], "category": null, "value": 10, "name": "test row"}' % clean_id(block.id)
55+
collection_view)) == json_string_template % (clean_id(collection_view.parent.id), clean_id(collection_view.id))

shared/notionscripts/tests/test_notion_api.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_block_content(notion_token):
1919
block = test_page.children.add_new(TextBlock, title="test get block content")
2020
content = notion_api.block_content(block.id)
2121

22-
assert content == {"id": clean_id(block.id), "title": "test get block content"}
22+
assert content == {"id": clean_id(block.id), "block_type": "text", "title": "test get block content"}
2323

2424

2525
def test_block_children(notion_token):
@@ -32,9 +32,9 @@ def test_block_children(notion_token):
3232

3333
content = notion_api.block_children(block.id)
3434

35-
assert content["parent"] == {"id": clean_id(block.id), "title": "a parent block"}
36-
assert content["children"][0] == {"id": clean_id(child_block_1.id), "title": "child block 1"}
37-
assert content["children"][1] == {"id": clean_id(child_block_2.id), "title": "child block 2"}
35+
assert content["parent"] == {"id": clean_id(block.id), "block_type": "text", "title": "a parent block"}
36+
assert content["children"][0] == {"id": clean_id(child_block_1.id), "block_type": "text", "title": "child block 1"}
37+
assert content["children"][1] == {"id": clean_id(child_block_2.id), "block_type": "text", "title": "child block 2"}
3838

3939

4040
def test_block_append(notion_token):
@@ -44,7 +44,7 @@ def test_block_append(notion_token):
4444
block = test_page.children.add_new(TextBlock, title="test block append")
4545
new_block = notion_api.block_append(block.id, {"title": "appending text"})
4646

47-
assert new_block == {"id": clean_id(new_block.block.id), "title": "appending text"}
47+
assert new_block == {"id": clean_id(new_block.block.id), "block_type": "text", "title": "appending text"}
4848
assert clean_id(new_block.block.parent.id) == clean_id(block.id)
4949

5050

@@ -55,7 +55,7 @@ def test_block_update_with_text_block(notion_token):
5555
block = test_page.children.add_new(TextBlock, title="test block update")
5656
updated_block = notion_api.block_update(block.id, {"title": "test block has been updated"})
5757

58-
assert updated_block == {"id": clean_id(block.id), "title": "test block has been updated"}
58+
assert updated_block == {"id": clean_id(block.id), "block_type": "text", "title": "test block has been updated"}
5959

6060

6161
def test_block_update_with_collection_block(notion_token):
@@ -66,7 +66,8 @@ def test_block_update_with_collection_block(notion_token):
6666

6767
updated_block = notion_api.block_update(block.id, {"name": "test block has been updated", "value": 5})
6868

69-
assert updated_block == {"id": clean_id(block.id), "name": "test block has been updated", "value": 5, "category": None, "enabled": True, "tags": []}
69+
assert updated_block == {"id": clean_id(block.id), "block_type": "page", "name": "test block has been updated",
70+
"value": 5, "category": None, "enabled": True, "tags": []}
7071

7172

7273
def test_block_delete_with_text_block(notion_token):

0 commit comments

Comments
 (0)