Skip to content

Commit

Permalink
add id and url property to database and some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thrau committed Oct 14, 2023
1 parent 8ef7863 commit 53560f7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
10 changes: 9 additions & 1 deletion notion_objects/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _construct(
):
self.database_id = database_id
self.client = client
self.type = mapped_type or DynamicNotionObject
self.type = mapped_type or self._from_database_properties

def __iter__(self):
return self.query(query={"page_size": self.default_page_size})
Expand All @@ -110,6 +110,14 @@ def title(self) -> str:
def _from_database_properties(self, obj: dict) -> DynamicNotionObject:
return DynamicNotionObject(obj, self.properties)

@property
def id(self) -> str:
return self._database_object()["id"]

@property
def url(self) -> str:
return self._database_object()["url"]

@lru_cache()
def _database_object(self):
return self.client.databases.retrieve(self.database_id)
Expand Down
39 changes: 35 additions & 4 deletions notion_objects/properties.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid
from datetime import date, datetime
from functools import cached_property
from typing import Any, Dict, Generic, List, Optional, Tuple, Type, TypeVar, Union
from typing import Any, Dict, Generic, Iterable, List, Optional, Tuple, Type, TypeVar, Union

import dateutil.parser

Expand Down Expand Up @@ -33,24 +33,47 @@
]

_T = TypeVar("_T")
_P = TypeVar("_P", bound="Property")


class Property(Generic[_T]):
"""
A descriptor for mappings between a notion database property and a python attribute.
"""

field: str
"""The name of the property in the notion database."""
attr: str
"""The name of the attribute in the mapped python object."""
target_type: Optional[Type] = None
"""The python type this property should be mapped to."""

def __init__(self, field: str = None, object_locator: str = "_obj"):
self.field = field
self.attr = field
self.object_locator = object_locator

def get(self, field: str, obj: dict) -> _T:
"""
Extract from the given field in a notion object the corresponding python value.
:param field: the notion database property name
:param obj: the notion database object
:return: a python value representing the value in the notion property
"""
raise NotImplementedError(
f"get operation not implemented for property '{self.__class__.__name__}'"
)

def set(self, field: str, value: _T, obj: dict):
"""
Maps a given python attribute value to the corresponding notion object property.
:param field: the notion database property name
:param value: the python value to set
:param obj: the notion database object
"""
raise NotImplementedError(
f"set operation not implemented for property '{self.__class__.__name__}'"
)
Expand Down Expand Up @@ -96,6 +119,8 @@ def __str__(self):


class ChangeTracker:
id: str

@cached_property
def __changes__(self) -> Dict[str, Any]:
return {}
Expand Down Expand Up @@ -448,7 +473,7 @@ def set(self, field: str, value: List[str], obj: dict):
PeopleProperty.set_value(field, value, obj)


class Properties:
class Properties(Iterable[_P]):
factories: Dict[str, Type[Property]] = {
"title": TitleText,
"created_time": CreatedTime,
Expand All @@ -465,14 +490,20 @@ class Properties:
# TODO: ...
}

properties: List[Property]
properties: List[_P]

def __init__(self, properties: List[Property]):
def __init__(self, properties: List[_P]):
self.properties = properties

def __iter__(self):
return self.properties.__iter__()

def __getitem__(self, item) -> _P:
for prop in self.properties:
if prop.field == item:
return prop
raise KeyError(f"No such property field {item}")

@classmethod
def parse(cls, obj: dict) -> "Properties":
created_time = RootProperty("created_time")
Expand Down

0 comments on commit 53560f7

Please sign in to comment.