-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_database.py
More file actions
92 lines (85 loc) · 2.63 KB
/
create_database.py
File metadata and controls
92 lines (85 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from dotenv import load_dotenv
from loguru import logger
from py_notion.schema.database import (
CheckboxConfiguration,
ContentConfiguration,
CoverConfiguration,
DatabasePropertyConfiguration,
ExternalConfiguration,
IconConfiguration,
MultiSelectConfiguration,
NumberConfiguration,
NumberFormatConfiguration,
NumberFormats,
ParentConfiguration,
RichTextConfiguration,
SelectConfiguration,
SelectOptionsConfiguration,
SelectOptionsListConfig,
TextConfiguration,
TitleConfiguration,
)
from src.py_notion.py_notion import PyNotion
from src.py_notion.schema.database.response.create_database_response_schema import CreateDatabaseResponseSchema
from examples.config import base_config
load_dotenv()
py_notion_client = PyNotion(token=base_config.notion_secret_token)
# # Create database payload
parent_payload = ParentConfiguration(
type="page_id",
page_id=base_config.page_id,
) # The parent is the page where the database will be created.
icon_payload = IconConfiguration(
type="emoji",
emoji="🎮",
) # The icon is the icon that will be displayed on the database.
text = TextConfiguration(content="Game") # The text is the text that will be displayed as the title of the database.
content = ContentConfiguration(
type="text",
plain_text="Game",
href="https://www.google.com",
text=text,
) # The content has other info's of the title.
# Cover schema
cover = CoverConfiguration(type="external", external=ExternalConfiguration(url="https://www.google.com"))
# # Forming select options schema
properties = {
"Name": TitleConfiguration().model_dump(),
"Description": RichTextConfiguration().model_dump(),
"In stock": CheckboxConfiguration().model_dump(),
"Food Group": SelectConfiguration(
select=SelectOptionsListConfig(
options=[
SelectOptionsConfiguration(color="green", name="Code"),
SelectOptionsConfiguration(color="red", name="Game"),
],
),
).model_dump(),
"Cuisines": MultiSelectConfiguration(
multi_select=SelectOptionsListConfig(
options=[
SelectOptionsConfiguration(color="orange", name="Italian"),
SelectOptionsConfiguration(color="blue", name="French"),
],
),
).model_dump(),
"Price": NumberConfiguration(
number=NumberFormatConfiguration(
format=NumberFormats.NUMBER_WITH_COMMAS,
),
).model_dump(),
}
create_database_payload = DatabasePropertyConfiguration(
title=[content],
cover=cover,
parent=parent_payload,
icon=icon_payload,
properties=properties,
)
response: CreateDatabaseResponseSchema | str = py_notion_client.database.create_database(
payload=create_database_payload,
)
if isinstance(response, str):
logger.info(response)
else:
logger.info(response.model_dump_json(indent=4))