From 7a522c870d88778d4a3fb7c2d27ca521cb9a9b50 Mon Sep 17 00:00:00 2001 From: lastorel Date: Mon, 25 Apr 2022 00:51:45 +0300 Subject: [PATCH] #17 TestElement:test_db_create --- README.md | 2 ++ pytion/api.py | 7 ++++++- pytion/models.py | 21 ++++++++++++++++++--- tests/test_api.py | 17 ++++++++++++++++- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d37bf69..fce3462 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,8 @@ some text `.db_create(database_obj, parent, properties, title)` - Create Database. +**_There is no way to delete a database object yet!_** + `.db_update(id_, title, properties)` - Update Database. `.page_create(page_obj, parent, properties, title)` - Create Page. diff --git a/pytion/api.py b/pytion/api.py index 46dd127..ec66610 100644 --- a/pytion/api.py +++ b/pytion/api.py @@ -297,7 +297,12 @@ def db_create( `parent = LinkTo.create(database_id="24512345125123421")` `p1 = Property.create(name="renamed")` `p2 = Property.create(type_="multi_select", name="multiselected")` - `props = {"Property1_name": p1, "Property2_name": p2}` + `props = {"Property1_name": p1, "Property2_name": p2}` OR + ```props = { + "Name": Property.create("title") + "Digit": Property.create("number"), + "Status": Property.create("select") + }``` `db = db.db_create(parent=parent, properties=props, title=RichTextArray.create("NEW DB"))` """ if self.name != "databases": diff --git a/pytion/models.py b/pytion/models.py index 26716db..c719244 100644 --- a/pytion/models.py +++ b/pytion/models.py @@ -13,6 +13,8 @@ def __init__(self, **kwargs) -> None: self.plain_text: str = kwargs.get("plain_text") self.href: Optional[str] = kwargs.get("href") self.annotations: Dict[str, Union[bool, str]] = kwargs.get("annotations") + # if not self.annotations: + # self._create_default_annotations() self.type: str = kwargs.get("type") if self.type == "mention": subtype = kwargs[self.type].get("type") @@ -53,6 +55,12 @@ def __repr__(self): def __bool__(self): return bool(self.plain_text) + def _create_default_annotations(self): + self.annotations = { + "bold": False, "italic": False, "strikethrough": False, + "underline": False, "code": False, "color": "default" + } + # def __len__(self): # return len(self.plain_text) @@ -60,7 +68,12 @@ def get(self) -> Dict[str, Any]: """ Text type supported only """ - return {"type": "text", "text": {"content": self.plain_text, "link": None}} + return { + "type": "text", + "text": {"content": self.plain_text, "link": None}, + # "annotations": self.annotations, + # "plain_text": self.plain_text, + } class RichTextArray(MutableSequence): @@ -455,7 +468,7 @@ def get(self) -> Dict[str, Dict]: "parent": self.parent.get(), "properties": {name: value.get() for name, value in self.properties.items()} } - if self.title: + if isinstance(self.title, RichTextArray): new_dict["title"] = self.title.get() return new_dict @@ -742,7 +755,7 @@ def __repr__(self): def get(self, with_object_type: bool = False): if self.type in [ "paragraph", "quote", "heading_1", "heading_2", "heading_3", "to_do", - "bulleted_list_item", "numbered_list_item", "toggle", "callout", "code" + "bulleted_list_item", "numbered_list_item", "toggle", "callout", "code", "child_database" ]: text = RichTextArray.create(self.text) if isinstance(self.text, str) else self.text @@ -753,6 +766,8 @@ def get(self, with_object_type: bool = False): new_dict[self.type]["language"] = getattr(self, "language", "plain text") if hasattr(self, "caption"): new_dict[self.type]["caption"] = self.caption.get() + if self.type == "child_database": + new_dict = {self.type: {"title": str(text)}} if with_object_type: new_dict["object"] = "block" new_dict["type"] = self.type diff --git a/tests/test_api.py b/tests/test_api.py index 45285ac..2d9d3a1 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,7 +1,7 @@ import pytest from pytion.models import Page, Block, Database, User -from pytion.models import BlockArray, PropertyValue, PageArray +from pytion.models import BlockArray, PropertyValue, PageArray, LinkTo, Property from pytion import InvalidRequestURL, ObjectNotFound, ValidationError @@ -350,3 +350,18 @@ def test_db_filter__sort_asc(self, little_database): assert len(pages.obj) == 4 assert str(pages.obj[0].title) == "wait, what?" assert str(pages.obj[2].title) == "We are best friends, body" + + def test_db_create(self, no): + parent = LinkTo.create(page_id="2dff77eb43d44ce097ffb421499f82aa") # Page for creating databases + properties = { + "Name": Property.create("title"), + "Digit": Property.create("number"), + "Status": Property.create("select"), + } + title = "DB 1" + database = no.databases.db_create(parent=parent, properties=properties, title=title) + assert isinstance(database.obj, Database) + assert str(database.obj.title) == title + assert "Status" in database.obj.properties + + # Delete database manually. There is no way to delete a database by API