Skip to content

Commit

Permalink
add blocks and tests for the status property (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
sHermanGriffiths authored Dec 14, 2023
1 parent 08f1edd commit 55c805c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
12 changes: 4 additions & 8 deletions n2y/notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}"')
Expand Down
25 changes: 11 additions & 14 deletions n2y/page.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down
27 changes: 27 additions & 0 deletions n2y/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -135,6 +161,7 @@ class LastEditedBy(Property):
"title": TitleProperty,
"rich_text": TextProperty,
"number": NumberProperty,
"status": StatusProperty,
"select": SelectProperty,
"multi_select": MultiSelectProperty,
"date": DateProperty,
Expand Down
12 changes: 12 additions & 0 deletions n2y/property_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions tests/test_property_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55c805c

Please sign in to comment.