diff --git a/n2y/notion.py b/n2y/notion.py index 8c9dc88..aa3c586 100644 --- a/n2y/notion.py +++ b/n2y/notion.py @@ -139,10 +139,8 @@ def _override_notion_classes(self, notion_object, object_types, default_object_t self.notion_classes[notion_object].append(plugin_class) else: raise PluginError( - ( - f'Cannot use "{plugin_class.__name__}", as it doesn\'t ' - f'override the base class "{base_class.__name__}"' - ), + f'Cannot use "{plugin_class.__name__}", as it doesn\'t ' + f'override the base class "{base_class.__name__}"', ) def _organize_notion_classes( @@ -156,10 +154,8 @@ def _organize_notion_classes( self.notion_classes[notion_object][object_type].append(plugin_class) else: raise PluginError( - ( - f'Cannot use "{plugin_class.__name__}", as it doesn\'t ' - f'override the base class "{base_class.__name__}"' - ), + f'Cannot use "{plugin_class.__name__}", as it doesn\'t ' + f'override the base class "{base_class.__name__}"', ) else: raise PluginError(f'Invalid type "{object_type}" for "{notion_object}"') diff --git a/n2y/page.py b/n2y/page.py index 2791e29..ad93a00 100644 --- a/n2y/page.py +++ b/n2y/page.py @@ -1,37 +1,34 @@ +from n2y.blocks import ChildDatabaseBlock, ChildPageBlock, TableOfContentsBlock from n2y.logger import logger -from n2y.utils import fromisoformat from n2y.property_values import TitlePropertyValue -from n2y.blocks import ChildDatabaseBlock, ChildPageBlock, TableOfContentsBlock +from n2y.utils import fromisoformat class Page: def __init__(self, client, notion_data): logger.debug("Instantiating page") + self.notion_parent = notion_data["parent"] + self.archived = notion_data["archived"] + self.notion_url = notion_data["url"] + self.notion_id = notion_data["id"] + self.notion_data = notion_data + self.plugin_data = {} + self._children = None self.client = client + self._block = None - self.notion_data = notion_data - self.notion_id = notion_data["id"] self.created_time = fromisoformat(notion_data["created_time"]) - self.created_by = client.wrap_notion_user(notion_data["created_by"]) self.last_edited_time = fromisoformat(notion_data["last_edited_time"]) + self.created_by = client.wrap_notion_user(notion_data["created_by"]) self.last_edited_by = client.wrap_notion_user(notion_data["last_edited_by"]) - self.archived = notion_data["archived"] self.icon = self._init_icon(notion_data["icon"]) self.cover = notion_data["cover"] and client.wrap_notion_file( notion_data["cover"] ) - self.archived = notion_data["archived"] self.properties = { k: client.wrap_notion_property_value(npv, self) for k, npv in notion_data["properties"].items() } - self.notion_parent = notion_data["parent"] - self.notion_url = notion_data["url"] - - self._block = None - self._children = None - - self.plugin_data = {} def _init_icon(self, icon_notion_data): """ diff --git a/n2y/properties.py b/n2y/properties.py index 44c5760..b79d6a2 100644 --- a/n2y/properties.py +++ b/n2y/properties.py @@ -24,6 +24,32 @@ def __init__(self, client, notion_data): self.format = notion_data["number"]["format"] +class StatusProperty(Property): + def __init__(self, client, notion_data): + super().__init__(client, notion_data) + notion_groups = notion_data["status"]["groups"] + notion_options = notion_data["status"]["options"] + self.groups = [StatusGroup(self.client, ng) for ng in notion_groups] + self.options = [StatusOption(self.client, no) for no in notion_options] + + +class StatusOption: + def __init__(self, client, notion_option): + self.client = client + self.notion_id = notion_option["id"] + self.name = notion_option["name"] + self.color = notion_option["color"] + + +class StatusGroup: + def __init__(self, client, notion_group): + self.client = client + self.name = notion_group["name"] + self.color = notion_group["color"] + self.notion_id = notion_group["id"] + self.option_ids = notion_group["option_ids"] + + class SelectProperty(Property): def __init__(self, client, notion_data): super().__init__(client, notion_data) @@ -135,6 +161,7 @@ class LastEditedBy(Property): "title": TitleProperty, "rich_text": TextProperty, "number": NumberProperty, + "status": StatusProperty, "select": SelectProperty, "multi_select": MultiSelectProperty, "date": DateProperty, diff --git a/n2y/property_values.py b/n2y/property_values.py index 04acb4e..ff27599 100644 --- a/n2y/property_values.py +++ b/n2y/property_values.py @@ -56,6 +56,17 @@ def to_value(self, _=None): return self.number +class StatusPropertyValue(PropertyValue): + def __init__(self, client, notion_data, page): + super().__init__(client, notion_data, page) + self.status = notion_data["status"] + + def to_value(self, _=None): + # Note: the Notion UI shouldn't allow you to have two statuses with the + # same name nor should it allow no status at all + return self.status + + class SelectPropertyValue(PropertyValue): def __init__(self, client, notion_data, page): super().__init__(client, notion_data, page) @@ -270,6 +281,7 @@ def to_value(self, _=None): "title": TitlePropertyValue, "rich_text": TextPropertyValue, "number": NumberPropertyValue, + "status": StatusPropertyValue, "select": SelectPropertyValue, "multi_select": MultiSelectPropertyValue, "date": DatePropertyValue, diff --git a/tests/test_property_values.py b/tests/test_property_values.py index 2e61b40..39544d8 100644 --- a/tests/test_property_values.py +++ b/tests/test_property_values.py @@ -56,6 +56,11 @@ def test_number_non_empty(): assert process_property_value(notion_data) == 3 +def test_status(): + notion_data = mock_property_value("status", "red") + assert process_property_value(notion_data) == "red" + + def test_select_empty(): notion_data = mock_property_value("select", None) assert process_property_value(notion_data) is None