From f4063e39aab48e7ff669e4053beeaa4d6151f2da Mon Sep 17 00:00:00 2001 From: Guilherme Camargo Date: Sun, 29 Dec 2019 07:06:48 -0300 Subject: [PATCH] Fixed Notion pages mention bug and added function to get them --- notion/collection.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/notion/collection.py b/notion/collection.py index 938a0fd..893a757 100644 --- a/notion/collection.py +++ b/notion/collection.py @@ -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() @@ -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: