diff --git a/CHANGELOG.md b/CHANGELOG.md index 56132b5..35d38ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#68](https://github.com/lastorel/pytion/issues/68): insert Block support - [#70](https://github.com/lastorel/pytion/issues/70): fix database filtering using `*_time` attribute - [#72](https://github.com/lastorel/pytion/issues/72): `public_url` attr added +- [#73](https://github.com/lastorel/pytion/issues/73): Unique ID page property added ## v1.3.4 diff --git a/README.md b/README.md index ff2a304..7d38dbf 100644 --- a/README.md +++ b/README.md @@ -279,6 +279,7 @@ There are also useful **internal** classes: | `formula` | | - | + | - | n/a | | | | `relation` | `List[LinkTo]` | + | + | + | + | `has_more: bool` | `single_property` / `dual_property` | | `rollup` | depends on relation and function | + | + | + | n/a | ~~has_more~~\*\*\*\*\* | `function`, `relation_property_id` / `relation_property_name`, `rollup_property_id` / `rollup_property_name` | +| `unique_id` | `int` | + | + | + | n/a | | `prefix` | | `created_time`\*\*\* | `datetime` | + | + | + | n/a | | | | `created_by`\*\*\* | `User` | + | + | + | n/a | | | | `last_edited_time`\*\*\* | `datetime` | + | + | + | n/a | | | diff --git a/pytion/models.py b/pytion/models.py index c2290f7..82e73f0 100644 --- a/pytion/models.py +++ b/pytion/models.py @@ -250,6 +250,10 @@ def __init__(self, data: Dict[str, Any]): self.rollup_property_id = data[self.type].get("rollup_property_id") self.rollup_property_name = data[self.type].get("rollup_property_name") + elif self.type == "unique_id": + if isinstance(data[self.type], dict): + self.prefix = data[self.type].get("prefix") + def __str__(self): return self.name if self.name else self.type @@ -286,6 +290,8 @@ def get(self) -> Optional[Dict[str, Dict]]: data[self.type]["rollup_property_id"] = self.rollup_property_id if self.rollup_property_name: data[self.type]["rollup_property_name"] = self.rollup_property_name + elif self.type == "unique_id": + data[self.type] = {"prefix": self.prefix} else: data[self.type] = {} return data @@ -320,6 +326,8 @@ def create(cls, type_: Optional[str] = "", **kwargs): kwargs["status"] = {} elif type_ == "rollup": kwargs["rollup"] = kwargs + elif type_ == "unique_id": + kwargs["unique_id"] = kwargs return cls({"type": type_, **kwargs}) @@ -448,6 +456,9 @@ def __init__(self, data: Dict, name: str, **kwargs): if self.type == "phone_number": self.value: Optional[str] = data.get("phone_number") + if self.type == "unique_id": + self.value: int = data["unique_id"]["number"] if "number" in data["unique_id"] else 0 + def __str__(self): return str(self.value) @@ -511,7 +522,7 @@ def get(self): return {self.type: []} elif self.type in ["created_time", "last_edited_by", "last_edited_time", "created_by"]: return None - elif self.type in ["formula", "rollup"]: + elif self.type in ["formula", "rollup", "unique_id"]: return {self.type: {}} return None diff --git a/tests/test_models.py b/tests/test_models.py index 0ea397e..acdb19c 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -66,6 +66,15 @@ def test_create__rollup(self): assert p_dict["rollup"]["rollup_property_id"] == "mvpx" assert "rollup_property_name" not in p_dict + def test_create__unique_id(self): + p = Property.create("unique_id", prefix="TT") + p_dict = p.get() + assert p.id is None + assert p.type == "unique_id" + assert p.to_delete is False + assert hasattr(p, "prefix") + assert p_dict["unique_id"]["prefix"] == "TT" + class TestPropertyFull: def test_create__rollup_id(self, database_for_updates, database_for_pages): @@ -137,6 +146,11 @@ def test_change__rollup(self, database_for_updates): assert database.obj.properties["Rollup 1"].function == old_function assert database.obj.properties["Rollup 1"].rollup_property_id == old_rollup_id + def test_create__unique_id(self): + # don't test creating unique_id property. + # on second iteration it breaks the database (error 500) + pass + class TestPropertyValue: def test_create__status(self): @@ -155,6 +169,14 @@ def test_create__relation(self): assert p_dict["relation"][0]["id"] == "04262843082a478d97f741948a32613c" +class TestPropertyValueFull: + def test_read__unique_id(self, no, little_database): + page = no.pages.get("b85877eaf7bf4245a8c5218055eeb81f") + assert "uID" in little_database.obj.properties + assert little_database.obj.properties["uID"].prefix == "LD" + assert page.obj.properties["uID"].value == 3 + + class TestBlock: def test_get__heading_1(self, no): block_id = "15a5790980db4e8798b9b7801385afbb"