Skip to content

Commit

Permalink
Merge pull request #38 from lastorel/dev
Browse files Browse the repository at this point in the history
v1.3.2
  • Loading branch information
lastorel authored Nov 25, 2022
2 parents 29bcbc7 + ee61f27 commit 72ea250
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v1.3.2
- [#34](https://github.com/lastorel/pytion/issues/34): `status` Property type added

## v1.3.1
- [#32](https://github.com/lastorel/pytion/issues/32): Rollback of Page retrieving with properties
- [#35](https://github.com/lastorel/pytion/issues/35): Fix PropertyValue with rollup type
Expand Down
53 changes: 27 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,36 +309,37 @@ Extension attributes are listed below in support matrix:
### Supported Property types

| Property type | value type | read (DB) | read value (Page) | create (DB) | create value (Page) | Oper attrs | Config attrs |
|-----------------------|---------------------|-----------|-------------------|-------------|---------------------|------------------------------------|-----------------------------------|
| `title` | `RichTextArray` | + | + | + | + | | |
| `rich_text` | `RichTextArray` | + | + | + | + | | |
| `number` | `int`/`float` | + | + | + | + | | ~~format~~ |
| `select` | `str` | + | + | + | + | | ~~options~~ |
| `multi_select` | `List[str]` | + | + | + | + | | ~~options~~ |
| `status` | | - | - | - | - | | ~~options~~ ~~groups~~ |
| `date` | `str` | + | + | + | + | `start: datetime` `end: datetime`* | |
| `people` | `List[User]` | + | + | + | +** | | |
| `files` | | + | - | + | - | | |
| `checkbox` | `bool` | + | + | + | + | | |
| `url` | `str` | + | + | + | + | | |
| `email` | `str` | + | + | + | + | | |
| `phone_number` | `str` | + | + | + | + | | |
| `formula` | | - | + | - | - | | |
| `relation` | `List[LinkTo]` | + | + | + | + | | `single_property`/`dual_property` |
| `rollup` | depends on relation | - | + | - | - | | |
| `created_time`*** | `datetime` | + | + | + | - | | |
| `created_by`*** | `User` | + | + | + | - | | |
| `last_edited_time`*** | `datetime` | + | + | + | - | | |
| `last_edited_by`*** | `User` | + | + | + | - | | |

> [*] - Create examples:
| Property type | value type | read (DB) | read value (Page) | create (DB) | create value (Page) | Oper attrs | Config attrs |
|--------------------------|---------------------|-----------|-------------------|-------------|---------------------|-------------------------------------|-----------------------------------|
| `title` | `RichTextArray` | + | + | + | + | | |
| `rich_text` | `RichTextArray` | + | + | + | + | | |
| `number` | `int`/`float` | + | + | + | + | | ~~format~~ |
| `select` | `str` | + | + | + | + | | ~~options~~ |
| `multi_select` | `List[str]` | + | + | + | + | | ~~options~~ |
| `status` | `str` | + | + | + | +\*\*\*\* | | `options`, `groups` (read-only) |
| `date` | `str` | + | + | + | + | `start: datetime` `end: datetime`\* | |
| `people` | `List[User]` | + | + | + | +\*\* | | |
| `files` | | + | - | + | - | | |
| `checkbox` | `bool` | + | + | + | + | | |
| `url` | `str` | + | + | + | + | | |
| `email` | `str` | + | + | + | + | | |
| `phone_number` | `str` | + | + | + | + | | |
| `formula` | | - | + | - | - | | |
| `relation` | `List[LinkTo]` | + | + | + | + | | `single_property`/`dual_property` |
| `rollup` | depends on relation | - | + | - | - | | |
| `created_time`\*\*\* | `datetime` | + | + | + | - | | |
| `created_by`\*\*\* | `User` | + | + | + | - | | |
| `last_edited_time`\*\*\* | `datetime` | + | + | + | - | | |
| `last_edited_by`\*\*\* | `User` | + | + | + | - | | |

> [\*] - Create examples:
> `pv = PropertyValue.create(type_="date", value=datetime.now())`
> `pv = PropertyValue.create(type_="date", date={"start": str(datetime(2022, 2, 1, 5)), "end": str(datetime.now())})`
> [**] - Create example:
> [\*\*] - Create example:
> `user = User.create('1d393ffb5efd4d09adfc2cb6738e4812')`
> `pv = PropertyValue.create(type_="people", value=[user])`
> [***] - Every Base model like Page already has mandatory attributes created/last_edited returned by API
> [\*\*\*] - Every Base model like Page already has mandatory attributes created/last_edited returned by API
> [\*\*\*\*] - Status type is not configurable. API doesn't support NEW options added via Property modify or updating a Page
### Block creating examples

Expand Down
22 changes: 22 additions & 0 deletions pytion/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ def __init__(self, data: Dict[str, Any]):
self.relation_property_id = data[self.type][self.subtype].get("synced_property_id")
self.relation_property_name = data[self.type][self.subtype].get("synced_property_name")

if self.type == "status":
if isinstance(data[self.type], dict):
self.options = data[self.type].get("options", [])
self.groups = data[self.type].get("groups", [])

def __str__(self):
return self.name if self.name else self.type

Expand All @@ -251,6 +256,13 @@ def get(self) -> Optional[Dict[str, Dict]]:
# create relation type property with configuration
if self.type == "relation":
data[self.type] = {self.subtype: {}, "database_id": self.relation.id}
elif self.type == "status":
data[self.type] = {}
# not configurable
# if self.options:
# data[self.type]["options"] = self.options
# if self.groups:
# data[self.type]["groups"] = self.groups
else:
data[self.type] = {}
return data
Expand All @@ -259,6 +271,7 @@ def get(self) -> Optional[Dict[str, Dict]]:
def create(cls, type_: Optional[str] = "", **kwargs):
"""
Property Schema Object (watch docs)
:param type_: see "create (DB)" column in "Supported Property types" matrix of README
+ addons:
set type_ = `None` to delete this Property
Expand All @@ -272,6 +285,8 @@ def create(cls, type_: Optional[str] = "", **kwargs):
if type_ == "relation":
subtype = next(kwarg for kwarg in kwargs if kwarg in ("single_property", "dual_property"))
kwargs["relation"] = {"type": subtype, subtype: {}, "database_id": kwargs[subtype]}
elif type_ == "status":
kwargs["status"] = {}
return cls({"type": type_, **kwargs})


Expand Down Expand Up @@ -359,6 +374,9 @@ def __init__(self, data: Dict, name: str, **kwargs):
for item in data[self.type]
]

if self.type == "status":
self.value = data[self.type].get("name") if isinstance(data[self.type], dict) else data[self.type]

if self.type == "rollup":
rollup_type = data["rollup"]["type"]
if rollup_type == "array":
Expand Down Expand Up @@ -450,6 +468,10 @@ def get(self):
if self.type == "relation":
return {self.type: [{"id": lt.id} for lt in self.value]}

# status type
if self.type == "status":
return {self.type: {"name": self.value}}

# unsupported types:
if self.type in ["files"]:
return {self.type: []}
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pytion",
version="1.3.1",
version="1.3.2",
author="Yegor Gomzin",
author_email="slezycmex@mail.ru",
description="Unofficial Python client for official Notion API",
Expand All @@ -20,6 +20,7 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
],
Expand Down
19 changes: 19 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,22 @@ def test_create__relation_dual(self):
assert isinstance(p.relation, LinkTo)
assert p.relation.uri == "databases"
assert p.subtype == "dual_property"

def test_create__status(self):
p = Property.create("status")
p_dict = p.get()
assert p.id is None
assert p.type == "status"
assert p.to_delete is False
assert isinstance(p.options, list)
assert isinstance(p.groups, list)
assert bool(p_dict["status"]) is False


class TestPropertyValue:
def test_create__status(self):
p = PropertyValue.create("status", value="Done")
p_dict = p.get()
assert p.id is None
assert p.type == "status"
assert p_dict["status"]["name"] == "Done"

0 comments on commit 72ea250

Please sign in to comment.