Skip to content

Commit bafc848

Browse files
authored
Merge pull request #33 from kevinjalbert/add-and-use-block-presenter
2 parents bde9552 + a0ffbdd commit bafc848

File tree

5 files changed

+62
-31
lines changed

5 files changed

+62
-31
lines changed

server/src/api.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def block_append(notion_token, block_id):
4141

4242
block = notion_api.block_append(block_id, request.json)
4343

44-
return jsonify(id=block.id), 200
44+
return jsonify(block), 200
4545
except Exception as error:
4646
return jsonify(error=str(error)), 500
4747

@@ -54,7 +54,7 @@ def block_update(notion_token, block_id):
5454

5555
block = notion_api.block_update(block_id, request.json)
5656

57-
return jsonify(id=block.id), 200
57+
return jsonify(block), 200
5858
except Exception as error:
5959
return jsonify(error=str(error)), 500
6060

@@ -67,7 +67,7 @@ def block_delete(notion_token, block_id):
6767

6868
block = notion_api.block_delete(block_id)
6969

70-
return jsonify(id=block.id), 200
70+
return jsonify(block), 200
7171
except Exception as error:
7272
return jsonify(error=str(error)), 500
7373

@@ -78,9 +78,9 @@ def block_view(notion_token, block_id):
7878
try:
7979
notion_api = NotionApi(notion_token)
8080

81-
content = notion_api.block_content(block_id)
81+
block = notion_api.block_content(block_id)
8282

83-
return jsonify(content), 200
83+
return jsonify(block), 200
8484
except Exception as error:
8585
return jsonify(error=str(error)), 500
8686

@@ -91,9 +91,9 @@ def block_children(notion_token, block_id):
9191
try:
9292
notion_api = NotionApi(notion_token)
9393

94-
content = notion_api.block_children(block_id)
94+
children = notion_api.block_children(block_id)
9595

96-
return jsonify(content), 200
96+
return jsonify(children), 200
9797
except Exception as error:
9898
return jsonify(error=str(error)), 500
9999

@@ -106,7 +106,7 @@ def collection_append(notion_token, collection_id, view_id):
106106

107107
row = notion_api.collection_append(collection_id, view_id, request.json)
108108

109-
return jsonify(row=row), 200
109+
return jsonify(row), 200
110110
except Exception as error:
111111
return jsonify(error=str(error)), 500
112112

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})

shared/notionscripts/notion_api.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env -S PATH="${PATH}:/usr/local/bin" python3
2+
from notionscripts.block_presenter import BlockPresenter
23

34
from cachetools import cached
45

@@ -20,23 +21,19 @@ def __collection_view(self, collection_id, view_id):
2021
def block_content(self, block_id):
2122
block = self.client().get_block(block_id)
2223

23-
return {"id": block.id, "title": block.title}
24+
return BlockPresenter(block)
2425

2526
def block_children(self, block_id):
2627
block = self.client().get_block(block_id)
2728

28-
content = []
29-
for child in block.children:
30-
content.append({"id": child.id, "title": child.title})
31-
32-
return content
29+
return [BlockPresenter(child) for child in block.children]
3330

3431
def block_append(self, block_id, data):
3532
block = self.client().get_block(block_id)
3633

3734
new_block = block.children.add_new(TextBlock, title=data["title"])
3835

39-
return new_block
36+
return BlockPresenter(new_block)
4037

4138
def block_update(self, block_id, data):
4239
block = self.client().get_block(block_id)
@@ -45,28 +42,24 @@ def block_update(self, block_id, data):
4542
for key, val in data.items():
4643
setattr(block, key, val)
4744

48-
return block
45+
return BlockPresenter(block)
4946

5047
def block_delete(self, block_id):
5148
block = self.client().get_block(block_id)
5249

5350
block.remove()
5451

55-
return block
52+
return BlockPresenter(block)
5653

5754
def collection_view_content(self, collection_id, view_id):
5855
collection_view = self.__collection_view(collection_id, view_id)
5956
results = collection_view.default_query().execute()
6057

61-
content = []
62-
for row in results:
63-
content.append(row.get_all_properties())
64-
65-
return content
58+
return [BlockPresenter(row) for row in results]
6659

6760
def collection_append(self, collection_id, view_id, data):
6861
collection_view = self.__collection_view(collection_id, view_id)
6962

7063
row = collection_view.collection.add_row(**data)
7164

72-
return row.get_all_properties()
65+
return BlockPresenter(row)
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

shared/notionscripts/tests/test_notion_api.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def test_block_append(notion_token):
3838
block = test_page.children.add_new(TextBlock, title="test block append")
3939
new_block = notion_api.block_append(block.id, {"title": "appending text"})
4040

41-
assert new_block.title == "appending text"
42-
assert new_block.parent.id == block.id
41+
assert new_block == {"id": new_block.block.id, "title": "appending text"}
42+
assert new_block.block.parent.id == block.id
4343

4444

4545
def test_block_update_with_text_block(notion_token):
@@ -49,8 +49,7 @@ def test_block_update_with_text_block(notion_token):
4949
block = test_page.children.add_new(TextBlock, title="test block update")
5050
updated_block = notion_api.block_update(block.id, {"title": "test block has been updated"})
5151

52-
assert updated_block.title == "test block has been updated"
53-
assert updated_block.id == block.id
52+
assert updated_block == {"id": block.id, "title": "test block has been updated"}
5453

5554

5655
def test_block_update_with_collection_block(notion_token):
@@ -59,11 +58,9 @@ def test_block_update_with_collection_block(notion_token):
5958
collection_view = create_collection_view()
6059
block = collection_view.collection.add_row(name="test row", value=10, enabled=True)
6160

62-
updated_block = notion_api.block_update(block.id, {"title": "test block has been updated", "value": 5})
61+
updated_block = notion_api.block_update(block.id, {"name": "test block has been updated", "value": 5})
6362

64-
assert updated_block.title == "test block has been updated"
65-
assert updated_block.value == 5
66-
assert updated_block.id == block.id
63+
assert updated_block == {"id": block.id, "name": "test block has been updated", "value": 5, "category": None, "enabled": True, "tags": []}
6764

6865

6966
def test_block_delete_with_text_block(notion_token):

0 commit comments

Comments
 (0)