Skip to content

Commit 0f54c9c

Browse files
committed
Tweaks to get_backlinks to handle children referencing parents
1 parent 87ee46a commit 0f54c9c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

notion/block.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,16 @@ def get_backlinks(self):
550550
"getBacklinksForBlock",
551551
{"blockId": self.id},
552552
).json()
553-
if "backlinks" not in data:
554-
return None
555-
return [self._client.get_block(block.get("mentioned_from").get("block_id")) for block in data.get("backlinks")]
553+
backlinks = []
554+
for block in data.get("backlinks") or []:
555+
mention = block.get("mentioned_from")
556+
if not mention:
557+
continue
558+
block_id = mention.get("block_id") or mention.get("parent_block_id")
559+
if block_id:
560+
print("Getting block", block_id)
561+
backlinks.append(self._client.get_block(block_id))
562+
return backlinks
556563

557564
class BulletedListBlock(BasicBlock):
558565

notion/smoke_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def run_live_smoke_test(token_v2, parent_page_url_or_id):
3737
assert video in page.children.filter(VideoBlock)
3838
assert col_list not in page.children.filter(VideoBlock)
3939

40+
# check that the parent does not yet consider this page to be backlinking
41+
assert page not in parent_page.get_backlinks()
42+
4043
page.children.add_new(SubheaderBlock, title="A link back to where I came from:")
4144
alias = page.children.add_alias(parent_page)
4245
assert alias.is_alias
@@ -48,6 +51,9 @@ def run_live_smoke_test(token_v2, parent_page_url_or_id):
4851
),
4952
)
5053

54+
# check that the parent now knows about the backlink
55+
assert page in parent_page.get_backlinks()
56+
5157
# ensure __repr__ methods are not breaking
5258
repr(page)
5359
repr(page.children)

0 commit comments

Comments
 (0)