|
17 | 17 | from ultimate_notion.blocks import Image as UnoImage |
18 | 18 | from ultimate_notion.blocks import Video as UnoVideo |
19 | 19 | from ultimate_notion.obj_api.blocks import Block as UnoObjAPIBlock |
| 20 | +from ultimate_notion.obj_api.core import Unset, UnsetType |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class _SerializedBlockTreeNode(TypedDict): |
@@ -120,6 +121,75 @@ def _upload_blocks_recursively( |
120 | 121 | ) |
121 | 122 |
|
122 | 123 |
|
| 124 | +@beartype |
| 125 | +def _reconstruct_nested_structure( |
| 126 | + *, |
| 127 | + items: list[dict[str, Any]], |
| 128 | +) -> list[dict[str, Any]]: |
| 129 | + """ |
| 130 | + Reconstruct nested structure from flattened block items. |
| 131 | + """ |
| 132 | + result: list[dict[str, Any]] = [] |
| 133 | + for item in items: |
| 134 | + block = item["block"] |
| 135 | + block_type = block["type"] |
| 136 | + |
| 137 | + # Handle blocks that can have children |
| 138 | + if block_type in { |
| 139 | + "paragraph", |
| 140 | + "callout", |
| 141 | + "bulleted_list_item", |
| 142 | + "numbered_list_item", |
| 143 | + "toggle", |
| 144 | + }: |
| 145 | + pass |
| 146 | + # children = item.get("children", []) |
| 147 | + # nested_blocks = _reconstruct_nested_structure( |
| 148 | + # items=children, |
| 149 | + # ) |
| 150 | + # block[block_type]["children"] = nested_blocks |
| 151 | + |
| 152 | + result.append(block) |
| 153 | + return result |
| 154 | + |
| 155 | + |
| 156 | +@beartype |
| 157 | +def _sanitize_serialized_block( |
| 158 | + *, |
| 159 | + serialized_block: dict[str, Any], |
| 160 | +) -> dict[str, Any]: |
| 161 | + """ |
| 162 | + Sanitize a serialized block. |
| 163 | + """ |
| 164 | + return serialized_block |
| 165 | + if isinstance(serialized_block, dict): |
| 166 | + return { |
| 167 | + key: _sanitize_serialized_block(serialized_block=value) |
| 168 | + for key, value in serialized_block.items() |
| 169 | + } |
| 170 | + return serialized_block |
| 171 | + |
| 172 | + |
| 173 | +@beartype |
| 174 | +def _sanitize_block( |
| 175 | + *, |
| 176 | + block: Block, |
| 177 | + session: Session, |
| 178 | +) -> Any: |
| 179 | + """ |
| 180 | + Sanitize a block. |
| 181 | + """ |
| 182 | + if block.obj_ref.id and not isinstance(block.obj_ref.id, UnsetType): |
| 183 | + block = session.get_block(block_ref=block.obj_ref.id) |
| 184 | + block.obj_ref.created_by = Unset |
| 185 | + block.obj_ref.last_edited_by = Unset |
| 186 | + block.obj_ref.created_time = Unset |
| 187 | + block.obj_ref.last_edited_time = Unset |
| 188 | + block.obj_ref.id = Unset |
| 189 | + block.obj_ref.parent = Unset |
| 190 | + return block |
| 191 | + |
| 192 | + |
123 | 193 | @click.command() |
124 | 194 | @click.option( |
125 | 195 | "--file", |
@@ -185,6 +255,53 @@ def main( |
185 | 255 |
|
186 | 256 | for child in page.children: |
187 | 257 | child.delete() |
| 258 | + block_list = _reconstruct_nested_structure(items=blocks) |
| 259 | + block_obj_list = [ |
| 260 | + Block.wrap_obj_ref(UnoObjAPIBlock.model_validate(obj=block)) |
| 261 | + for block in block_list |
| 262 | + ] |
| 263 | + sanitized_block_list = [ |
| 264 | + _sanitize_block(block=block, session=session) |
| 265 | + for block in block_obj_list |
| 266 | + ] |
| 267 | + children_list = list(page.children) |
| 268 | + sanitized_children_list = [ |
| 269 | + _sanitize_block(block=child, session=session) |
| 270 | + for child in children_list |
| 271 | + ] |
| 272 | + |
| 273 | + import difflib |
| 274 | + import pprint |
| 275 | + |
| 276 | + for index, page_child in enumerate(sanitized_children_list): |
| 277 | + block_child = sanitized_block_list[index] |
| 278 | + if page_child != block_child: |
| 279 | + s1 = pprint.pformat( |
| 280 | + page_child.__dict__, sort_dicts=True |
| 281 | + ).splitlines() |
| 282 | + s2 = pprint.pformat( |
| 283 | + block_child.__dict__, sort_dicts=True |
| 284 | + ).splitlines() |
| 285 | + |
| 286 | + diff = difflib.unified_diff(s1, s2) |
| 287 | + |
| 288 | + diff_list = list(diff) |
| 289 | + nice_diff = "\n".join(diff) |
| 290 | + breakpoint() |
| 291 | + # children_obj_list = [ |
| 292 | + # Block.wrap_obj_ref(UnoObjAPIBlock.model_validate(obj=child)) |
| 293 | + # for child in children_list |
| 294 | + # ] |
| 295 | + # serialized_children_list = [ |
| 296 | + # child.obj_ref.serialize_for_api() for child in children_list |
| 297 | + # ] |
| 298 | + # sanitized_serialized_children_list = [ |
| 299 | + # _sanitize_serialized_block(serialized_block=serialized_block) |
| 300 | + # for serialized_block in serialized_children_list |
| 301 | + # ] |
| 302 | + breakpoint() |
| 303 | + # for child in page.children: |
| 304 | + # child.delete() |
188 | 305 |
|
189 | 306 | # See https://developers.notion.com/reference/request-limits#limits-for-property-values |
190 | 307 | # which shows that the max number of blocks per request is 100. |
|
0 commit comments