Skip to content

Commit 9b84b3e

Browse files
committed
fix: normalize the ids for blocks to not have "-" in them
This makes it so the URL scheme opening to blocks in iOS works.
1 parent c5a2022 commit 9b84b3e

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

shared/notionscripts/block_presenter.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class BlockPresenter(dict):
88
def __init__(self, block):
99
self.block = block
1010
if isinstance(block, CollectionRowBlock):
11-
dict.__init__(self, **{"id": block.id, **block.get_all_properties()})
11+
dict.__init__(self, **{"id": block.id.replace("-", ""), **block.get_all_properties()})
1212
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})
13+
dict.__init__(self, **{"collection_id": block.parent.id.replace("-", ""), "view_id": block.id.replace("-", ""),
14+
"collection_title": block.parent.title, "view_title": block.name})
1415
else:
15-
dict.__init__(self, **{"id": block.id, "title": block.title})
16+
dict.__init__(self, **{"id": block.id.replace("-", ""), "title": block.title})

shared/notionscripts/tests/test_block_presenter.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,25 @@
99
import pytest # noqa, F401
1010

1111

12+
def clean_id(id):
13+
return id.replace("-", "")
14+
15+
1216
def test_block_presentation(notion_token):
1317
test_page = get_test_page()
1418

1519
block = test_page.children.add_new(TextBlock, title="textblock")
1620

1721
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
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)
2024

2125

2226
def test_collection_row_block_presentation(notion_token):
2327
collection_view = create_collection_view()
2428
block = collection_view.collection.add_row(name="test row", value=10, enabled=True)
2529

2630
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
31+
assert BlockPresenter(block) == {"id": clean_id(block.id), "enabled": True, "tags": [], "category": None, "value": 10, "name": "test row"}
32+
assert json.dumps(BlockPresenter(
33+
block)) == '{"id": "%s", "enabled": true, "tags": [], "category": null, "value": 10, "name": "test row"}' % clean_id(block.id)

shared/notionscripts/tests/test_notion_api.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
import pytest # noqa, F401
99

1010

11+
def clean_id(id):
12+
return id.replace("-", "")
13+
14+
1115
def test_block_content(notion_token):
1216
notion_api = NotionApi(token=notion_token)
1317
test_page = get_test_page()
1418

1519
block = test_page.children.add_new(TextBlock, title="test get block content")
1620
content = notion_api.block_content(block.id)
1721

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

2024

2125
def test_block_children(notion_token):
@@ -28,9 +32,9 @@ def test_block_children(notion_token):
2832

2933
content = notion_api.block_children(block.id)
3034

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"}
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"}
3438

3539

3640
def test_block_append(notion_token):
@@ -40,8 +44,8 @@ def test_block_append(notion_token):
4044
block = test_page.children.add_new(TextBlock, title="test block append")
4145
new_block = notion_api.block_append(block.id, {"title": "appending text"})
4246

43-
assert new_block == {"id": new_block.block.id, "title": "appending text"}
44-
assert new_block.block.parent.id == block.id
47+
assert new_block == {"id": clean_id(new_block.block.id), "title": "appending text"}
48+
assert clean_id(new_block.block.parent.id) == clean_id(block.id)
4549

4650

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

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

5660

5761
def test_block_update_with_collection_block(notion_token):
@@ -62,7 +66,7 @@ def test_block_update_with_collection_block(notion_token):
6266

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

65-
assert updated_block == {"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), "name": "test block has been updated", "value": 5, "category": None, "enabled": True, "tags": []}
6670

6771

6872
def test_block_delete_with_text_block(notion_token):
@@ -114,8 +118,8 @@ def test_collection_view_content(notion_token):
114118
collection_view_content = notion_api.collection_view_content(collection_id.replace("-", ""), view_id.replace("-", ""))
115119

116120
assert collection_view_content["collection"] == {
117-
"collection_id": collection_id,
118-
"view_id": view_id,
121+
"collection_id": clean_id(collection_id),
122+
"view_id": clean_id(view_id),
119123
"collection_title": "Test collection",
120124
"view_title": "Test view"
121125
}

0 commit comments

Comments
 (0)