55
66import json
77import sys
8+ from enum import Enum
89from pathlib import Path
9- from typing import Any
10+ from typing import TYPE_CHECKING , Any
1011from urllib .parse import urlparse
1112from urllib .request import url2pathname
1213
2122from ultimate_notion .file import UploadedFile
2223from 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
2631def _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(
97118def 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