Skip to content

Commit 974448b

Browse files
Merge pull request #252 from adamtheturtle/database-support
Add support for publishing to a database
2 parents 91452ee + c86d576 commit 974448b

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

README.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ Usage
141141
142142
Arguments:
143143

144-
- ``-f, --file``: Path to the JSON file generated by the Notion builder
145-
- ``-p, --parent-page-id``: The ID of the parent page in Notion (must be shared with your integration)
146-
- ``-t, --title``: Title for the new page in Notion
144+
- ``--file``: Path to the JSON file generated by the Notion builder
145+
- ``--parent-id``: The ID of the parent page or database in Notion (must be shared with your integration)
146+
- ``--parent-type``: "page" or "database"
147+
- ``--title``: Title for the new page in Notion
147148

148149
The command will create a new page if one with the given title doesn't exist, or update the existing page if one with the given title already exists.
149150

src/_notion_scripts/upload.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
import json
77
import sys
8+
from enum import Enum
89
from pathlib import Path
9-
from typing import Any
10+
from typing import TYPE_CHECKING, Any
1011
from urllib.parse import urlparse
1112
from urllib.request import url2pathname
1213

@@ -21,6 +22,10 @@
2122
from ultimate_notion.file import UploadedFile
2223
from ultimate_notion.obj_api.blocks import Block as UnoObjAPIBlock
2324

25+
if TYPE_CHECKING:
26+
from ultimate_notion.database import Database
27+
from ultimate_notion.page import Page
28+
2429

2530
@beartype
2631
def _upload_local_file(
@@ -66,6 +71,16 @@ def _block_from_details(
6671
return block
6772

6873

74+
@beartype
75+
class _ParentType(Enum):
76+
"""
77+
Type of parent that new page will live under.
78+
"""
79+
80+
PAGE = "page"
81+
DATABASE = "database"
82+
83+
6984
@click.command()
7085
@click.option(
7186
"--file",
@@ -79,10 +94,16 @@ def _block_from_details(
7994
),
8095
)
8196
@click.option(
82-
"--parent-page-id",
83-
help="Parent page ID (integration connected)",
97+
"--parent-id",
98+
help="Parent page or database ID (integration connected)",
8499
required=True,
85100
)
101+
@click.option(
102+
"--parent-type",
103+
help="Parent type",
104+
required=True,
105+
type=click.Choice(choices=_ParentType, case_sensitive=False),
106+
)
86107
@click.option(
87108
"--title",
88109
help="Title of the page to update (or create if it does not exist)",
@@ -97,7 +118,8 @@ def _block_from_details(
97118
def main(
98119
*,
99120
file: Path,
100-
parent_page_id: str,
121+
parent_id: str,
122+
parent_type: _ParentType,
101123
title: str,
102124
icon: str | None = None,
103125
) -> None:
@@ -108,11 +130,17 @@ def main(
108130

109131
blocks = json.loads(s=file.read_text(encoding="utf-8"))
110132

111-
parent_page = session.get_page(page_ref=parent_page_id)
133+
parent: Page | Database
134+
match parent_type:
135+
case _ParentType.PAGE:
136+
parent = session.get_page(page_ref=parent_id)
137+
subpages = parent.subpages
138+
case _ParentType.DATABASE:
139+
parent = session.get_db(db_ref=parent_id)
140+
subpages = parent.get_all_pages().to_pages()
141+
112142
pages_matching_title = [
113-
child_page
114-
for child_page in parent_page.subpages
115-
if child_page.title == title
143+
child_page for child_page in subpages if child_page.title == title
116144
]
117145

118146
if pages_matching_title:
@@ -123,7 +151,7 @@ def main(
123151
assert len(pages_matching_title) == 1, msg
124152
(page,) = pages_matching_title
125153
else:
126-
page = session.create_page(parent=parent_page, title=title)
154+
page = session.create_page(parent=parent, title=title)
127155
sys.stdout.write(f"Created new page: {title} (ID: {page.id})\n")
128156

129157
if icon:

0 commit comments

Comments
 (0)