Skip to content

Commit

Permalink
add #73
Browse files Browse the repository at this point in the history
  • Loading branch information
petrpaluba committed Oct 4, 2023
1 parent 909945b commit bb94042
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
Expand Down
13 changes: 12 additions & 1 deletion pytion/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})


Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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"
Expand Down

0 comments on commit bb94042

Please sign in to comment.