Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Children #81

Merged
merged 5 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions n2y/notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,18 @@ def save_file(self, content, page, extension):
with open(full_filepath, 'wb') as temp_file:
temp_file.write(content)
return urljoin(self.media_url, relative_filepath)

def append_block_children(self, block_id, children):
response = requests.patch(
f"{self.base_url}blocks/{block_id}/children",
json={"children": children}, headers=self.headers
)
return self._parse_response(response)

def delete_block(self, block_id):
headers = {**self.headers}
del headers['Content-Type']
johndgiese marked this conversation as resolved.
Show resolved Hide resolved
response = requests.delete(
f"{self.base_url}blocks/{block_id}", headers=headers
)
return self._parse_response(response)
2 changes: 1 addition & 1 deletion n2y/notion_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def mock_rich_text(text, annotations=None, href=None, mention=None):
annotations = []
if mention is None:
rich_text_type = 'text'
content = {'content': text, 'link': None},
johndgiese marked this conversation as resolved.
Show resolved Hide resolved
content = {'content': text, 'link': None}
else:
rich_text_type = 'mention'
content = mention
Expand Down
25 changes: 25 additions & 0 deletions tests/test_children.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from n2y.notion import Client
from n2y.notion_mocks import mock_block, mock_rich_text
from n2y.utils import id_from_share_link
from tests.utils import NOTION_ACCESS_TOKEN


def test_create_and_delete_blocks():
url = "cfa8ff07bba244c8b967c9b6a7a954c1"
client = Client(NOTION_ACCESS_TOKEN, plugins=None)
object_id = id_from_share_link(url)
page = client.get_page_or_database(object_id)
notion_block = mock_block(
"heading_2",
{
"color": "default",
"rich_text": [mock_rich_text("m")],
},
has_children=True
)
del notion_block["url"]
creation_response = client.append_block_children(page.notion_id, [notion_block])
new_block_id = creation_response['results'][0]['id']
deletion_response = client.delete_block(new_block_id)
assert creation_response
assert deletion_response
johndgiese marked this conversation as resolved.
Show resolved Hide resolved