Skip to content

Commit

Permalink
Merge pull request #22 from guilherme-abcam/master
Browse files Browse the repository at this point in the history
Fixed Notion pages mention bug and added function to get them
  • Loading branch information
arturtamborski authored Jul 1, 2020
2 parents 425ef3b + f4063e3 commit 6e49dee
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions notion/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ def get_property(self, identifier):

return self._convert_notion_to_python(val, prop)

def get_mentioned_pages_on_property(self, identifier):
prop = self.collection.get_schema_property(identifier)
if prop is None:
raise AttributeError("Object does not have property '{}'".format(identifier))
val = self.get(["properties", prop["id"]])
return self._convert_mentioned_pages_to_python(val, prop)

def _convert_diff_to_changelist(self, difference, old_val, new_val):

changed_props = set()
Expand Down Expand Up @@ -420,9 +427,29 @@ def _convert_diff_to_changelist(self, difference, old_val, new_val):
remaining, old_val, new_val
)

def _convert_mentioned_pages_to_python(self, val, prop):
if not prop["type"] in ["title", "text"]:
raise TypeError("The property must be an title or text to convert mentioned pages to Python.")

pages = []
for i, part in enumerate(val):
if len(part) == 2:
for format in part[1]:
if "p" in format:
pages.append(self._client.get_block(format[1]))

return pages

def _convert_notion_to_python(self, val, prop):

if prop["type"] in ["title", "text"]:
for i, part in enumerate(val):

This comment has been minimized.

Copy link
@ivanistheone

ivanistheone Jul 22, 2020

this breaks if val is None (empty property).

if len(part) == 2:
for format in part[1]:
if "p" in format:
page = self._client.get_block(format[1])
val[i] = (["[" + page.icon + " " + page.title + "](" + page.get_browseable_url() + ")"])

val = notion_to_markdown(val) if val else ""
if prop["type"] in ["number"]:
if val is not None:
Expand Down

0 comments on commit 6e49dee

Please sign in to comment.