Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,24 @@ def cover_url() -> str:

@fixture
def example_page_id() -> str:
return "e439b7a7296d45b98805f24c9cfc2115"
return "2cef2075b1dc803a8ec3d142ac105817"


@fixture(scope="session")
def database_id() -> str:
return "2cef2075b1dc8023b0ece0dbf9b278f7"


@fixture(scope="session")
def data_source_id1() -> str:
return "2cef2075b1dc80bab834000b42b068d7"


@fixture
def data_source_id2() -> str:
return "2cef2075b1dc80048d8d000b1b75df8f"


@fixture
def example_page_id_2() -> str:
return "d5bce0a0fe6248d0a120c6c693d9b597"
def block_id() -> str:
return "2cef2075b1dc80a59d6ad9ed97846ff8"
154 changes: 154 additions & 0 deletions docs/get_started/data_sources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
## Retrieve a data source

=== "Async"

```python
async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
```

=== "Sync"

```python
api = NotionAPI(access_token='<NOTION_TOKEN>')
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')
```

## Query

=== "Async"

```python
async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

async for page in data_source.query():
...
```

=== "Sync"

```python
api = NotionAPI(access_token='<NOTION_TOKEN>')
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

for page in data_source.query():
...
```

### Filters

You can use filter classes in `python_notion_api.models.filters` to create property filters and pass them to the query.

=== "Async"

```python
from python_notion_api.models.filters import SelectFilter

async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

await for page in data_source.query(
filters=SelectFilter(property='<PROPERTY_NAME / PROPERTY_ID>', equals='<VALUE>')
):
...
```

=== "Sync"

```python
from python_notion_api.models.filters import SelectFilter

api = NotionAPI(access_token='<NOTION_TOKEN>')
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

for page in data_source.query(
filters=SelectFilter(property='<PROPERTY_NAME / PROPERTY_ID>', equals='<VALUE>')
):
...
```

'and' and 'or' filters are supported:

=== "Async"

```python
from python_notion_api.models.filters import SelectFilter, or_filter, and_filter

async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

await for page in data_source.query(
filters=or_filter([
SelectFilter(property="Select", equals="xxx"),
and_filter([
NumberFilter(property="Number", greater_than=10),
CheckboxFilter(property="Checkbox", equals=True)
])
])
):
...
```

=== "Sync"

```python
from python_notion_api.models.filters import SelectFilter, or_filter, and_filter

api = NotionAPI(access_token='<NOTION_TOKEN>')
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

for page in data_source.query(
filters=or_filter([
SelectFilter(property="Select", equals="xxx"),
and_filter([
NumberFilter(property="Number", greater_than=10),
CheckboxFilter(property="Checkbox", equals=True)
])
])
)
...
```

You can read more on filters [here](https://developers.notion.com/reference/post-database-query-filter){:target="_blank"}

### Sorts

You can use `python_notion_api.models.sorts.Sort` class to create sorts and pass them to the query.

=== "Async"

```python
from python_notion_api.models.sorts import Sort

async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

await for page in data_source.query(
sorts=[
Sort(property="Title"),
Sort(property="Date", descending=True)
]
):
```

=== "Sync"

```python
from python_notion_api.models.sorts import Sort

api = NotionAPI(access_token='<NOTION_TOKEN>')
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

for page in data_source.query(
sorts=[
Sort(property="Title"),
Sort(property="Date", descending=True)
]
)
```
138 changes: 0 additions & 138 deletions docs/get_started/databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,141 +14,3 @@
api = NotionAPI(access_token='<NOTION_TOKEN>')
database = api.get_database(database_id='<DATABASE_ID>')
```

## Query

=== "Async"

```python
async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
database = await async_api.get_database(database_id='<DATABASE_ID>')

async for page in database.query():
...
```

=== "Sync"

```python
api = NotionAPI(access_token='<NOTION_TOKEN>')
database = api.get_database(database_id='<DATABASE_ID>')

for page in database.query():
...
```

### Filters

You can use filter classes in `python_notion_api.models.filters` to create property filters and pass them to the query.

=== "Async"

```python
from python_notion_api.models.filters import SelectFilter

async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
database = await async_api.get_database(database_id='<DATABASE_ID>')

await for page in database.query(
filters=SelectFilter(property='<PROPERTY_NAME / PROPERTY_ID>', equals='<VALUE>')
):
...
```

=== "Sync"

```python
from python_notion_api.models.filters import SelectFilter

api = NotionAPI(access_token='<NOTION_TOKEN>')
database = api.get_database(database_id='<DATABASE_ID>')

for page in database.query(
filters=SelectFilter(property='<PROPERTY_NAME / PROPERTY_ID>', equals='<VALUE>')
):
...
```

'and' and 'or' filters are supported:

=== "Async"

```python
from python_notion_api.models.filters import SelectFilter, or_filter, and_filter

async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
database = await async_api.get_database(database_id='<DATABASE_ID>')

await for page in database.query(
filters=or_filter([
SelectFilter(property="Select", equals="xxx"),
and_filter([
NumberFilter(property="Number", greater_than=10),
CheckboxFilter(property="Checkbox", equals=True)
])
])
):
...
```

=== "Sync"

```python
from python_notion_api.models.filters import SelectFilter, or_filter, and_filter

api = NotionAPI(access_token='<NOTION_TOKEN>')
database = api.get_database(database_id='<DATABASE_ID>')

for page in database.query(
filters=or_filter([
SelectFilter(property="Select", equals="xxx"),
and_filter([
NumberFilter(property="Number", greater_than=10),
CheckboxFilter(property="Checkbox", equals=True)
])
])
)
...
```

You can read more on filters [here](https://developers.notion.com/reference/post-database-query-filter){:target="_blank"}

### Sorts

You can use `python_notion_api.models.sorts.Sort` class to create sorts and pass them to the query.

=== "Async"

```python
from python_notion_api.models.sorts import Sort

async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
database = await async_api.get_database(database_id='<DATABASE_ID>')

await for page in database.query(
sorts=[
Sort(property="Title"),
Sort(property="Date", descending=True)
]
):
```

=== "Sync"

```python
from python_notion_api.models.sorts import Sort

api = NotionAPI(access_token='<NOTION_TOKEN>')
database = api.get_database(database_id='<DATABASE_ID>')

for page in database.query(
sorts=[
Sort(property="Title"),
Sort(property="Date", descending=True)
]
)
```
16 changes: 8 additions & 8 deletions docs/get_started/pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
```python
async def main():
async_api = AsyncNotionAPI(access_token='<NOTION_TOKEN>')
database = await async_api.get_database(database_id='<DATABASE_ID>')
data_source = await async_api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

await database.create_page(properties={
await data_source.create_page(properties={
'Number_property': 234,
'Select_property': 'select1',
'Checkbox_property': True,
Expand All @@ -36,9 +36,9 @@

```python
api = NotionAPI(access_token='<NOTION_TOKEN>')
database = api.get_database(database_id='<DATABASE_ID>')
data_source = api.get_data_source(data_source_id='<DATA_SOURCE_ID>')

database.create_page(properties={
data_source.create_page(properties={
'Number_property': 234,
'Select_property': 'select1',
'Checkbox_property': True,
Expand Down Expand Up @@ -130,8 +130,8 @@ In particular, the values of rollups and formulas may be incorrect when retrieve
To use custom page properties, create a subclass of NotionPage. Define a function to get each custom property (these must return a `PropertyValue`) and define the mapping from Notion property names to the function names.

```python
from python_notion_api.api import NotionPage
from python_notion_api.models import RichTextObject
from python_notion_api.sync_api.api import NotionPage
from python_notion_api.models import RichTextObject, RichTextPropertyItem
from python_notion_api.models.values import RichTextPropertyValue

class MyPage(NotionPage):
Expand Down Expand Up @@ -159,14 +159,14 @@ class MyPage(NotionPage):

```

This page class can be passed when querying a database or getting a page.
This page class can be passed when querying a data source or getting a page.

```python
page = api.get_page(page_id='<PAGE_ID>',
cast_cls=MyPage)


for page in database.query(cast_cls=MyPage, filters=NumberFilter(property='Value', equals=1)):
for page in data_source.query(cast_cls=MyPage, filters=NumberFilter(property='Value', equals=1)):
print('Custom processing:', page.get('Value').value)
print('Raw value:', page._direct_get('Value').value)
```
2 changes: 2 additions & 0 deletions python_notion_api/async_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
create_property_iterator,
)
from python_notion_api.async_api.notion_block import NotionBlock
from python_notion_api.async_api.notion_data_source import NotionDataSource
from python_notion_api.async_api.notion_database import NotionDatabase
from python_notion_api.async_api.notion_page import NotionPage

Expand All @@ -14,6 +15,7 @@
"NotionBlock",
"NotionPage",
"NotionDatabase",
"NotionDataSource",
"AsyncPropertyItemIterator",
"AsyncRollupPropertyItemIterator",
"AsyncBlockIterator",
Expand Down
Loading
Loading