Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Notion pages mention bug and added function to get them #22

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions notion/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,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 @@ -385,9 +392,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):
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