Skip to content

Commit

Permalink
add basic support for unique_id values
Browse files Browse the repository at this point in the history
  • Loading branch information
thrau committed Apr 3, 2024
1 parent 4003116 commit f7dab67
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion notion_objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from .properties import *
from .values import *

__version__ = "0.6.0"
__version__ = "0.6.1"
6 changes: 4 additions & 2 deletions notion_objects/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Iterable

from .properties import DateRange, DateTimeRange, Property, RichTextProperty
from .values import DateValue, UserValue
from .values import DateValue, UniqueIdValue, UserValue


class JSONEncoder(json.JSONEncoder):
Expand All @@ -29,7 +29,9 @@ def to_dict(self, flat: bool = False) -> dict:
continue
key = prop.attr

if isinstance(value, DateValue):
if isinstance(value, UniqueIdValue):
result[key] = str(value)
elif isinstance(value, DateValue):
# TODO: timezone
if flat:
result[f"{key}_start"] = value.start
Expand Down
14 changes: 13 additions & 1 deletion notion_objects/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import dateutil.parser

from . import rich_text
from .values import DateValue, UserValue
from .values import DateValue, UniqueIdValue, UserValue

property_types = [
"title",
Expand All @@ -43,6 +43,7 @@
"status",
"emoji", # TODO
"external", # TODO
"unique_id",
]

PropertyType = Literal[
Expand All @@ -65,6 +66,7 @@
"select",
"status",
"title",
"unique_id",
"url",
]
"""The notion data type of a property, see https://developers.notion.com/reference/property-object."""
Expand Down Expand Up @@ -332,6 +334,16 @@ def set(self, field: str, value: Optional[Union[float, int, str]], obj: dict):
obj[field] = {self.type: value}


class UniqueId(Property[UniqueIdValue]):
type = "unique_id"

def get(self, field: str, obj: dict) -> UniqueIdValue:
return UniqueIdValue(**obj["properties"][field][self.type])

def set(self, field: str, value: _T, obj: dict):
raise NotImplementedError("setting unique IDs is not possible yet")


class Integer(Property[Optional[int]]):
type = "number"

Expand Down
9 changes: 9 additions & 0 deletions notion_objects/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ def __str__(self):
return self.name
else:
return self.id


@dataclass
class UniqueIdValue:
prefix: str
number: int

def __str__(self):
return f"{self.prefix}-{self.number}"

0 comments on commit f7dab67

Please sign in to comment.