Skip to content

Commit 449f236

Browse files
committed
feat: provide error response that a block has already been deleted
This checks to see if the block is alive or not before doing a delete on it. This makes it so we can at least tell the block is in the trash (as we don't do permanent deletes).
1 parent 444d6ca commit 449f236

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env -S PATH="${PATH}:/usr/local/bin" python3
2+
3+
4+
class AlreadyDeletedError(Exception):
5+
def __init__(self, block_id):
6+
super().__init__(f"Block {block_id} has already been deleted")

shared/notionscripts/notion_api.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env -S PATH="${PATH}:/usr/local/bin" python3
22
from notionscripts.block_presenter import BlockPresenter
3+
from notionscripts.already_deleted_error import AlreadyDeletedError
34

45
from cachetools import cached
56

@@ -47,9 +48,11 @@ def block_update(self, block_id, data):
4748
def block_delete(self, block_id):
4849
block = self.client().get_block(block_id)
4950

50-
block.remove()
51-
52-
return BlockPresenter(block)
51+
if block.alive:
52+
block.remove()
53+
return BlockPresenter(block)
54+
else:
55+
raise AlreadyDeletedError(block_id)
5356

5457
def collection_view_content(self, collection_id, view_id):
5558
collection_view = self.__collection_view(collection_id, view_id)

shared/notionscripts/tests/test_notion_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from notionscripts.notion_api import NotionApi
2+
from notionscripts.already_deleted_error import AlreadyDeletedError
23

34
from .notion_api_page_helper import get_test_page, create_collection_view
45

@@ -87,6 +88,20 @@ def test_block_delete_with_collection_block(notion_token):
8788
assert block not in collection_view.collection.get_rows()
8889

8990

91+
def test_block_delete_an_already_deleted_block(notion_token):
92+
notion_api = NotionApi(token=notion_token)
93+
test_page = get_test_page()
94+
95+
block = test_page.children.add_new(TextBlock, title="test block delete")
96+
parent_block = block.parent
97+
98+
notion_api.block_delete(block.id)
99+
parent_block.refresh()
100+
101+
with pytest.raises(AlreadyDeletedError):
102+
notion_api.block_delete(block.id)
103+
104+
90105
def test_collection_view_content(notion_token):
91106
notion_api = NotionApi(token=notion_token)
92107

0 commit comments

Comments
 (0)