Skip to content

Commit 801264f

Browse files
authored
Merge pull request #28 from kevinjalbert/add-ability-to-update-a-block
2 parents 8bbc6e2 + 8953876 commit 801264f

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

server/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ As of right now the following endpoints exist:
1414

1515
```python
1616
@app.route('/blocks/<block_id>/append', methods=['POST'])
17+
@app.route('/blocks/<block_id>/update', methods=['PUT'])
1718
@app.route('/blocks/<block_id>/view', methods=['GET'])
1819
@app.route('/collections/<collection_id>/<view_id>/append', methods=['POST'])
1920
@app.route('/collections/<collection_id>/<view_id>/view', methods=['GET'])

server/src/api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ def block_append(notion_token, block_id):
4646
return jsonify(error=str(error)), 500
4747

4848

49+
@app.route('/blocks/<block_id>/update', methods=['PUT'])
50+
@token_required
51+
def block_update(notion_token, block_id):
52+
try:
53+
notion_api = NotionApi(notion_token)
54+
55+
block = notion_api.block_update(block_id, request.json['text'])
56+
57+
return jsonify(block_id=block.id), 200
58+
except Exception as error:
59+
return jsonify(error=str(error)), 500
60+
61+
4962
@app.route('/blocks/<block_id>/view', methods=['GET'])
5063
@token_required
5164
def block_view(notion_token, block_id):

shared/notionscripts/notion_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ def block_append(self, block_id, text):
3232

3333
return block.children.add_new(TextBlock, title=text)
3434

35+
def block_update(self, block_id, text):
36+
block = self.client().get_block(block_id)
37+
38+
block.title = text
39+
40+
return block
41+
3542
def collection_view_content(self, collection_id, view_id):
3643
collection_view = self.__collection_view(collection_id, view_id)
3744
results = collection_view.default_query().execute()

shared/notionscripts/tests/test_notion_api.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from notion.block import TextBlock
66

7-
import pytest
7+
import pytest # noqa, F401
88

99

1010
def test_block_content(notion_token):
@@ -17,7 +17,6 @@ def test_block_content(notion_token):
1717
assert content == "test get block content"
1818

1919

20-
@pytest.mark.focus
2120
def test_block_append(notion_token):
2221
notion_api = NotionApi(token=notion_token)
2322
test_page = get_test_page()
@@ -29,6 +28,17 @@ def test_block_append(notion_token):
2928
assert new_block.parent.id == block.id
3029

3130

31+
def test_block_update(notion_token):
32+
notion_api = NotionApi(token=notion_token)
33+
test_page = get_test_page()
34+
35+
block = test_page.children.add_new(TextBlock, title="test block update")
36+
updated_block = notion_api.block_update(block.id, "test block has been updated")
37+
38+
assert updated_block.title == "test block has been updated"
39+
assert updated_block.id == block.id
40+
41+
3242
def test_collection_view_content(notion_token):
3343
notion_api = NotionApi(token=notion_token)
3444

0 commit comments

Comments
 (0)