Skip to content

Commit

Permalink
#17 TestElement:test_db_create
Browse files Browse the repository at this point in the history
  • Loading branch information
lastorel committed Apr 24, 2022
1 parent b8cb623 commit 7a522c8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion pytion/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
21 changes: 18 additions & 3 deletions pytion/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -53,14 +55,25 @@ 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)

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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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


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

0 comments on commit 7a522c8

Please sign in to comment.