notion-cascade-insert 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- notion_cascade_insert/__init__.py +10 -0
- notion_cascade_insert/_modidx.py +49 -0
- notion_cascade_insert/core.py +178 -0
- notion_cascade_insert/webhook.py +37 -0
- notion_cascade_insert-0.0.1.dist-info/METADATA +132 -0
- notion_cascade_insert-0.0.1.dist-info/RECORD +10 -0
- notion_cascade_insert-0.0.1.dist-info/WHEEL +5 -0
- notion_cascade_insert-0.0.1.dist-info/entry_points.txt +2 -0
- notion_cascade_insert-0.0.1.dist-info/licenses/LICENSE +201 -0
- notion_cascade_insert-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
__version__ = "0.0.1"
|
|
2
|
+
"""Notion API wrapper for easy database cascade insert (one-to-many) automation"""
|
|
3
|
+
|
|
4
|
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/index.ipynb.
|
|
5
|
+
|
|
6
|
+
# %% auto #0
|
|
7
|
+
__all__ = []
|
|
8
|
+
|
|
9
|
+
# %% ../nbs/index.ipynb #90c30996
|
|
10
|
+
__all__ = ['Database', 'TriggerDB', 'JunctionDB', 'LogDB', 'AutoLogger', 'NotionWebhook']
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Autogenerated by nbdev
|
|
2
|
+
|
|
3
|
+
d = { 'settings': { 'branch': 'main',
|
|
4
|
+
'doc_baseurl': '/notion_cascade_insert',
|
|
5
|
+
'doc_host': 'https://amezaikupan.github.io',
|
|
6
|
+
'git_url': 'https://github.com/amezaikupan/notion_cascade_insert',
|
|
7
|
+
'lib_path': 'notion_cascade_insert'},
|
|
8
|
+
'syms': { 'notion_cascade_insert.core': { 'notion_cascade_insert.core.AutoLogger': ( 'core.html#autologger',
|
|
9
|
+
'notion_cascade_insert/core.py'),
|
|
10
|
+
'notion_cascade_insert.core.AutoLogger.__init__': ( 'core.html#autologger.__init__',
|
|
11
|
+
'notion_cascade_insert/core.py'),
|
|
12
|
+
'notion_cascade_insert.core.AutoLogger.adjust_batch': ( 'core.html#autologger.adjust_batch',
|
|
13
|
+
'notion_cascade_insert/core.py'),
|
|
14
|
+
'notion_cascade_insert.core.AutoLogger.cancel': ( 'core.html#autologger.cancel',
|
|
15
|
+
'notion_cascade_insert/core.py'),
|
|
16
|
+
'notion_cascade_insert.core.AutoLogger.handle_update': ( 'core.html#autologger.handle_update',
|
|
17
|
+
'notion_cascade_insert/core.py'),
|
|
18
|
+
'notion_cascade_insert.core.AutoLogger.process': ( 'core.html#autologger.process',
|
|
19
|
+
'notion_cascade_insert/core.py'),
|
|
20
|
+
'notion_cascade_insert.core.Database': ('core.html#database', 'notion_cascade_insert/core.py'),
|
|
21
|
+
'notion_cascade_insert.core.Database.__init__': ( 'core.html#database.__init__',
|
|
22
|
+
'notion_cascade_insert/core.py'),
|
|
23
|
+
'notion_cascade_insert.core.Database.get_schema': ( 'core.html#database.get_schema',
|
|
24
|
+
'notion_cascade_insert/core.py'),
|
|
25
|
+
'notion_cascade_insert.core.JunctionDB': ( 'core.html#junctiondb',
|
|
26
|
+
'notion_cascade_insert/core.py'),
|
|
27
|
+
'notion_cascade_insert.core.JunctionDB.__init__': ( 'core.html#junctiondb.__init__',
|
|
28
|
+
'notion_cascade_insert/core.py'),
|
|
29
|
+
'notion_cascade_insert.core.JunctionDB.get_items': ( 'core.html#junctiondb.get_items',
|
|
30
|
+
'notion_cascade_insert/core.py'),
|
|
31
|
+
'notion_cascade_insert.core.LogDB': ('core.html#logdb', 'notion_cascade_insert/core.py'),
|
|
32
|
+
'notion_cascade_insert.core.LogDB.__init__': ( 'core.html#logdb.__init__',
|
|
33
|
+
'notion_cascade_insert/core.py'),
|
|
34
|
+
'notion_cascade_insert.core.LogDB.create_entry': ( 'core.html#logdb.create_entry',
|
|
35
|
+
'notion_cascade_insert/core.py'),
|
|
36
|
+
'notion_cascade_insert.core.TriggerDB': ( 'core.html#triggerdb',
|
|
37
|
+
'notion_cascade_insert/core.py'),
|
|
38
|
+
'notion_cascade_insert.core.TriggerDB.__init__': ( 'core.html#triggerdb.__init__',
|
|
39
|
+
'notion_cascade_insert/core.py'),
|
|
40
|
+
'notion_cascade_insert.core.TriggerDB.get_page_data': ( 'core.html#triggerdb.get_page_data',
|
|
41
|
+
'notion_cascade_insert/core.py')},
|
|
42
|
+
'notion_cascade_insert.webhook': { 'notion_cascade_insert.webhook.NotionWebhook': ( 'webhook.html#notionwebhook',
|
|
43
|
+
'notion_cascade_insert/webhook.py'),
|
|
44
|
+
'notion_cascade_insert.webhook.NotionWebhook.__init__': ( 'webhook.html#notionwebhook.__init__',
|
|
45
|
+
'notion_cascade_insert/webhook.py'),
|
|
46
|
+
'notion_cascade_insert.webhook.NotionWebhook.parent_db_id': ( 'webhook.html#notionwebhook.parent_db_id',
|
|
47
|
+
'notion_cascade_insert/webhook.py'),
|
|
48
|
+
'notion_cascade_insert.webhook.NotionWebhook.updated_props': ( 'webhook.html#notionwebhook.updated_props',
|
|
49
|
+
'notion_cascade_insert/webhook.py')}}}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"""Notion Automation for multiple page creation"""
|
|
2
|
+
|
|
3
|
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.
|
|
4
|
+
|
|
5
|
+
# %% auto #0
|
|
6
|
+
__all__ = ['Database', 'TriggerDB', 'JunctionDB', 'LogDB', 'AutoLogger']
|
|
7
|
+
|
|
8
|
+
# %% ../nbs/00_core.ipynb #68712bbc
|
|
9
|
+
class Database:
|
|
10
|
+
"""Base class for interacting with a Notion database."""
|
|
11
|
+
def __init__(self,
|
|
12
|
+
db_id, # Notion database ID
|
|
13
|
+
notion # Notion client instance
|
|
14
|
+
):
|
|
15
|
+
self.db_id, self.notion = db_id, notion
|
|
16
|
+
|
|
17
|
+
def get_schema(self):
|
|
18
|
+
"""Retrieve the database schema.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
dict: Mapping of property names to their types
|
|
22
|
+
"""
|
|
23
|
+
db = self.notion.databases.retrieve(self.db_id)
|
|
24
|
+
return {name: props['type'] for name, props in db['properties'].items()}
|
|
25
|
+
|
|
26
|
+
# %% ../nbs/00_core.ipynb #52b04240
|
|
27
|
+
class TriggerDB(Database):
|
|
28
|
+
"""
|
|
29
|
+
Database that triggers logging when updated.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self,
|
|
33
|
+
db_id, # Notion database ID
|
|
34
|
+
notion, # Notion client instance
|
|
35
|
+
status_prop, # Name of status property
|
|
36
|
+
relation_prop, # Name of relation property
|
|
37
|
+
qty_prop # Name of the quantity property
|
|
38
|
+
):
|
|
39
|
+
"""Initialize a TriggerDB instance.
|
|
40
|
+
"""
|
|
41
|
+
super().__init__(db_id, notion)
|
|
42
|
+
self.status_prop, self.relation_prop, self.qty_prop = status_prop, relation_prop, qty_prop
|
|
43
|
+
|
|
44
|
+
#| exec_doc
|
|
45
|
+
def get_page_data(self,
|
|
46
|
+
page_id # Notion page ID to retrieve (the row that trigger change)
|
|
47
|
+
):
|
|
48
|
+
"""Extract status, related item ID, and quantity from a page.
|
|
49
|
+
"""
|
|
50
|
+
page = self.notion.pages.retrieve(page_id)
|
|
51
|
+
status = page['properties'][self.status_prop]['select']['name'] if page['properties'][self.status_prop]['select'] else None
|
|
52
|
+
relation_id = page['properties'][self.relation_prop]['relation'][0]['id'] if page['properties'][self.relation_prop]['relation'] else None
|
|
53
|
+
qty = page['properties'][self.qty_prop]['number']
|
|
54
|
+
return status, relation_id, qty
|
|
55
|
+
|
|
56
|
+
# %% ../nbs/00_core.ipynb #82d6f2e9
|
|
57
|
+
class JunctionDB(Database):
|
|
58
|
+
"""Database that connects trigger items to log items with amounts.
|
|
59
|
+
|
|
60
|
+
Acts as a many-to-many relationship table, storing which items are affected
|
|
61
|
+
by a trigger and their respective amounts or multipliers.
|
|
62
|
+
"""
|
|
63
|
+
def __init__(self,
|
|
64
|
+
db_id, # Notion database ID
|
|
65
|
+
notion, # Notion client instance
|
|
66
|
+
relation_prop, # Name of relation property to trigger items
|
|
67
|
+
item_prop, # Name of relation property to log items
|
|
68
|
+
amount_prop # Name of amount propert
|
|
69
|
+
):
|
|
70
|
+
|
|
71
|
+
super().__init__(db_id, notion)
|
|
72
|
+
self.relation_prop, self.item_prop, self.amount_prop = relation_prop, item_prop, amount_prop
|
|
73
|
+
|
|
74
|
+
def get_items(self,
|
|
75
|
+
relation_id # Trigger item's relation ID
|
|
76
|
+
):
|
|
77
|
+
"""Get all items and their amounts for a given relation.
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
dict: Mapping of item IDs to their amounts, empty dict if no relation_id
|
|
81
|
+
"""
|
|
82
|
+
if not relation_id: return {}
|
|
83
|
+
res = self.notion.data_sources.query(data_source_id=self.db_id, filter={"property": self.relation_prop, "relation": {"contains": relation_id}})
|
|
84
|
+
return {r['properties'][self.item_prop]['relation'][0]['id']: r['properties'][self.amount_prop]['number'] for r in res['results']}
|
|
85
|
+
|
|
86
|
+
#| export
|
|
87
|
+
class LogDB(Database):
|
|
88
|
+
"""Database where transaction logs are written.
|
|
89
|
+
|
|
90
|
+
Records all inventory or quantity changes with item references, amounts,
|
|
91
|
+
triggering events, and reasons for the change.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
def __init__(self,
|
|
95
|
+
db_id, # Notion database ID
|
|
96
|
+
notion, # Notion client instance
|
|
97
|
+
item_prop, # Name of item relation property
|
|
98
|
+
amount_prop, # Name of the amount property
|
|
99
|
+
trigger_prop, # Name of the trigger relation property
|
|
100
|
+
reason_prop # Name of the reason select property
|
|
101
|
+
):
|
|
102
|
+
"""Initialize a LogDB instance.
|
|
103
|
+
"""
|
|
104
|
+
super().__init__(db_id, notion)
|
|
105
|
+
self.item_prop, self.amount_prop, self.trigger_prop, self.reason_prop = item_prop, amount_prop, trigger_prop, reason_prop
|
|
106
|
+
|
|
107
|
+
def create_entry(self,
|
|
108
|
+
item_id, # ID of item being logged
|
|
109
|
+
amount, # Amount to log
|
|
110
|
+
trigger_id, # ID of triggering page
|
|
111
|
+
reason # Reason for log entry
|
|
112
|
+
):
|
|
113
|
+
"""Create a new log entry.
|
|
114
|
+
"""
|
|
115
|
+
self.notion.pages.create(parent={"database_id": self.db_id}, properties={self.item_prop: {"relation": [{"id": item_id}]}, self.amount_prop: {"number": amount}, self.trigger_prop: {"relation": [{"id": trigger_id}]}, self.reason_prop: {"select": {"name": reason}}})
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
# %% ../nbs/00_core.ipynb #b5e31e9a
|
|
120
|
+
class AutoLogger:
|
|
121
|
+
"""Orchestrates automatic logging from trigger to log via junction.
|
|
122
|
+
|
|
123
|
+
Monitors trigger database changes and automatically creates corresponding
|
|
124
|
+
log entries by looking up related items through the junction database.
|
|
125
|
+
"""
|
|
126
|
+
def __init__(self,
|
|
127
|
+
trigger_db, # TriggerDB instance
|
|
128
|
+
junction_db, # JunctionDB instance
|
|
129
|
+
log_db, # LogDB instance
|
|
130
|
+
trigger_status, # Status value that triggers logging
|
|
131
|
+
multiplier=-1 # Amount multiplier (typically -1 for deduction)
|
|
132
|
+
):
|
|
133
|
+
self.trigger_db, self.junction_db, self.log_db = trigger_db, junction_db, log_db
|
|
134
|
+
self.trigger_status, self.multiplier = trigger_status, multiplier
|
|
135
|
+
|
|
136
|
+
def process(self, page_id):
|
|
137
|
+
"""Process a trigger page and create log entries."""
|
|
138
|
+
status, relation_id, qty = self.trigger_db.get_page_data(page_id)
|
|
139
|
+
if status != self.trigger_status: return f"Status is {status}, not {self.trigger_status}"
|
|
140
|
+
items = self.junction_db.get_items(relation_id)
|
|
141
|
+
if not items: return "No items found in junction"
|
|
142
|
+
for item_id, amt in items.items(): self.log_db.create_entry(item_id, self.multiplier * qty * amt, page_id, 'auto_log')
|
|
143
|
+
return f"Logged {len(items)} items"
|
|
144
|
+
|
|
145
|
+
def cancel(self, page_id):
|
|
146
|
+
"""Reverse all logs for a trigger (restore inventory)."""
|
|
147
|
+
_, relation_id, qty = self.trigger_db.get_page_data(page_id)
|
|
148
|
+
items = self.junction_db.get_items(relation_id)
|
|
149
|
+
if not items: return "No items to reverse"
|
|
150
|
+
for item_id, amt in items.items(): self.log_db.create_entry(item_id, -self.multiplier * qty * amt, page_id, 'cancelled')
|
|
151
|
+
return f"Reversed {len(items)} items"
|
|
152
|
+
|
|
153
|
+
def adjust_batch(self,
|
|
154
|
+
page_id, # Trigger page ID being adjusted
|
|
155
|
+
old_qty, # Previous quantity value
|
|
156
|
+
new_qty # New quantity value
|
|
157
|
+
):
|
|
158
|
+
"""Log the difference when batch quantity changes."""
|
|
159
|
+
status, relation_id, _ = self.trigger_db.get_page_data(page_id)
|
|
160
|
+
if status != self.trigger_status: return f"Status is {status}, not active"
|
|
161
|
+
items = self.junction_db.get_items(relation_id)
|
|
162
|
+
if not items: return "No items found"
|
|
163
|
+
delta = new_qty - old_qty
|
|
164
|
+
for item_id, amt in items.items(): self.log_db.create_entry(item_id, self.multiplier * delta * amt, page_id, 'batch_adjusted')
|
|
165
|
+
return f"Adjusted {len(items)} items by {delta} batches"
|
|
166
|
+
|
|
167
|
+
def handle_update(self,
|
|
168
|
+
page_id, # Trigger page ID that was updated
|
|
169
|
+
old_status=None, # Previous status value
|
|
170
|
+
old_qty=None # Previous quantity value
|
|
171
|
+
):
|
|
172
|
+
"""Handle any update to trigger page - detects what changed and acts accordingly."""
|
|
173
|
+
status, _, qty = self.trigger_db.get_page_data(page_id)
|
|
174
|
+
if old_status and old_status == self.trigger_status and status != self.trigger_status: return self.cancel(page_id)
|
|
175
|
+
if status == self.trigger_status:
|
|
176
|
+
if old_status and old_status != self.trigger_status: return self.process(page_id)
|
|
177
|
+
if old_qty and old_qty != qty: return self.adjust_batch(page_id, old_qty, qty)
|
|
178
|
+
return "No action needed"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Helper function for dealing with Notion Webhook"""
|
|
2
|
+
|
|
3
|
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_webhook.ipynb.
|
|
4
|
+
|
|
5
|
+
# %% auto #0
|
|
6
|
+
__all__ = ['NotionWebhook']
|
|
7
|
+
|
|
8
|
+
# %% ../nbs/01_webhook.ipynb #2ec50454
|
|
9
|
+
class NotionWebhook:
|
|
10
|
+
"""Handler for Notion webhook payloads."""
|
|
11
|
+
|
|
12
|
+
def __init__(self,
|
|
13
|
+
data # Dictionary containing the webhook payload from Notion
|
|
14
|
+
):
|
|
15
|
+
"""Initialize the webhook handler with payload data.
|
|
16
|
+
"""
|
|
17
|
+
self.data = data
|
|
18
|
+
self.type = data['type']
|
|
19
|
+
self.entity_id = data['entity']['id']
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def parent_db_id(self):
|
|
23
|
+
"""Get the parent database ID with hyphens removed.
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
String containing the parent database ID without hyphens, or empty string if not found
|
|
27
|
+
"""
|
|
28
|
+
return self.data.get('data', {}).get('parent', {}).get('id', '').replace('-', '')
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def updated_props(self):
|
|
32
|
+
"""Get the list of properties that were updated in this webhook event.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
List of property names that were updated, or empty list if none
|
|
36
|
+
"""
|
|
37
|
+
return self.data.get('data', {}).get('updated_properties', [])
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: notion_cascade_insert
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Cascade inserting for Notion Database Automation (as log)
|
|
5
|
+
Home-page: https://github.com/amezaikupan/notion_cascade_insert
|
|
6
|
+
Author: Solveit
|
|
7
|
+
Author-email: nobody@fast.ai
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Keywords: nbdev jupyter notebook python
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Requires-Python: >=3.9
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: fastcore
|
|
22
|
+
Requires-Dist: fastapi
|
|
23
|
+
Requires-Dist: notion_client
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Dynamic: author
|
|
26
|
+
Dynamic: author-email
|
|
27
|
+
Dynamic: classifier
|
|
28
|
+
Dynamic: description
|
|
29
|
+
Dynamic: description-content-type
|
|
30
|
+
Dynamic: home-page
|
|
31
|
+
Dynamic: keywords
|
|
32
|
+
Dynamic: license
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
Dynamic: provides-extra
|
|
35
|
+
Dynamic: requires-dist
|
|
36
|
+
Dynamic: requires-python
|
|
37
|
+
Dynamic: summary
|
|
38
|
+
|
|
39
|
+
# Notion Cascade Insert
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
|
|
43
|
+
|
|
44
|
+
## Installing
|
|
45
|
+
|
|
46
|
+
``` python
|
|
47
|
+
! pip install notion_cascade_insert
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Requirement already satisfied: notion_cascade_insert in /app/data/.local/lib/python3.12/site-packages (0.0.1)
|
|
51
|
+
Requirement already satisfied: fastcore in /usr/local/lib/python3.12/site-packages (from notion_cascade_insert) (1.12.2)
|
|
52
|
+
Requirement already satisfied: fastapi in /usr/local/lib/python3.12/site-packages (from notion_cascade_insert) (0.128.0)
|
|
53
|
+
Requirement already satisfied: notion_client in /app/data/.local/lib/python3.12/site-packages (from notion_cascade_insert) (2.7.0)
|
|
54
|
+
Requirement already satisfied: starlette<0.51.0,>=0.40.0 in /app/data/.local/lib/python3.12/site-packages (from fastapi->notion_cascade_insert) (0.50.0)
|
|
55
|
+
Requirement already satisfied: pydantic>=2.7.0 in /usr/local/lib/python3.12/site-packages (from fastapi->notion_cascade_insert) (2.12.5)
|
|
56
|
+
Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.12/site-packages (from fastapi->notion_cascade_insert) (4.15.0)
|
|
57
|
+
Requirement already satisfied: annotated-doc>=0.0.2 in /usr/local/lib/python3.12/site-packages (from fastapi->notion_cascade_insert) (0.0.4)
|
|
58
|
+
Requirement already satisfied: anyio<5,>=3.6.2 in /usr/local/lib/python3.12/site-packages (from starlette<0.51.0,>=0.40.0->fastapi->notion_cascade_insert) (4.12.1)
|
|
59
|
+
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.12/site-packages (from anyio<5,>=3.6.2->starlette<0.51.0,>=0.40.0->fastapi->notion_cascade_insert) (3.11)
|
|
60
|
+
Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/site-packages (from pydantic>=2.7.0->fastapi->notion_cascade_insert) (0.7.0)
|
|
61
|
+
Requirement already satisfied: pydantic-core==2.41.5 in /usr/local/lib/python3.12/site-packages (from pydantic>=2.7.0->fastapi->notion_cascade_insert) (2.41.5)
|
|
62
|
+
Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/site-packages (from pydantic>=2.7.0->fastapi->notion_cascade_insert) (0.4.2)
|
|
63
|
+
Requirement already satisfied: packaging in /usr/local/lib/python3.12/site-packages (from fastcore->notion_cascade_insert) (25.0)
|
|
64
|
+
Requirement already satisfied: httpx>=0.23.0 in /usr/local/lib/python3.12/site-packages (from notion_client->notion_cascade_insert) (0.28.1)
|
|
65
|
+
Requirement already satisfied: certifi in /usr/local/lib/python3.12/site-packages (from httpx>=0.23.0->notion_client->notion_cascade_insert) (2026.1.4)
|
|
66
|
+
Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/site-packages (from httpx>=0.23.0->notion_client->notion_cascade_insert) (1.0.9)
|
|
67
|
+
Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/site-packages (from httpcore==1.*->httpx>=0.23.0->notion_client->notion_cascade_insert) (0.16.0)
|
|
68
|
+
|
|
69
|
+
## How to use
|
|
70
|
+
|
|
71
|
+
### Building blocks
|
|
72
|
+
|
|
73
|
+
This package is built to emulate the `Notion Automation` function but
|
|
74
|
+
with one to many relation. I built this to track the transactions (hence
|
|
75
|
+
the log) that involves calculating in case where there is an big object
|
|
76
|
+
that map to many other objects, like when you have a recipe and you want
|
|
77
|
+
to get the ingredients, or when you have a lists of guests and you want
|
|
78
|
+
to get their details… This helps you manage the ammoun of stuff that
|
|
79
|
+
relate to that big object.
|
|
80
|
+
|
|
81
|
+
There are 4 of the pipelines:
|
|
82
|
+
|
|
83
|
+
1. **TriggerDB**: monitors a database for status changes
|
|
84
|
+
2. **JunctionDB**: looks up related items and amounts
|
|
85
|
+
3. **LogDB**: writes transaction logs
|
|
86
|
+
4. **AutoLogger**: orchestrates the flow
|
|
87
|
+
|
|
88
|
+
We can than use them to connect to `Notion Webhook` and create the
|
|
89
|
+
functions that we want.
|
|
90
|
+
|
|
91
|
+
### Example
|
|
92
|
+
|
|
93
|
+
<!-- <img src="img/mermaid-diagram-2026-01-24-213107.png"> -->
|
|
94
|
+
|
|
95
|
+
<img src="img/mermaid-diagram-2026-01-24-213107.png" style="width: 100%; height: auto; max-height: none;">
|
|
96
|
+
|
|
97
|
+
Let’s say we’re building a Bakery Inventory management database, and we
|
|
98
|
+
want our `Production Plan` database to automatically log the used
|
|
99
|
+
ingredients in a recipe that we want to make. This is a one-to-many
|
|
100
|
+
behavior, which Notion don’t support at the momment. For this, we would
|
|
101
|
+
do something like:
|
|
102
|
+
|
|
103
|
+
``` python
|
|
104
|
+
from notion_cascade_insert.core import TriggerDB, JunctionDB, LogDB, AutoLogger
|
|
105
|
+
from notion_cascade_insert.webhook import NotionWebhook
|
|
106
|
+
from fastapi import FastAPI, Request
|
|
107
|
+
from notion_client import Client
|
|
108
|
+
import os
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
``` python
|
|
112
|
+
notion = Client(auth=os.getenv("NOTION_TOKEN"))
|
|
113
|
+
|
|
114
|
+
trigger = TriggerDB(os.getenv("PRODUCTION_PLAN_DB_ID"), notion, "Status", "Recipes", "Batches to make")
|
|
115
|
+
junction = JunctionDB(os.getenv("RECIPE_INGREDIENTS_DB_ID"), notion, "Recipes", "Ingredient Inventory", "Amount per batch")
|
|
116
|
+
log = LogDB(os.getenv("INGREDIENT_TRANSACTION_DB"), notion, "Ingredient", "Amount", "Production Plan", "Reason")
|
|
117
|
+
db_logger = AutoLogger(trigger, junction, log, "In Process", -1)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
This will create your Ingredient Logger! Then you can set up your server
|
|
121
|
+
like so:
|
|
122
|
+
|
|
123
|
+
``` python
|
|
124
|
+
app = FastAPI()
|
|
125
|
+
|
|
126
|
+
@app.post("/webhook")
|
|
127
|
+
async def webhook(request: Request):
|
|
128
|
+
hook = NotionWebhook(await request.json())
|
|
129
|
+
if hook.parent_db_id == os.getenv("PRODUCTION_PLAN_DB_ID"):
|
|
130
|
+
if hook.type == 'page.created': return {"result": db_logger.process(hook.entity_id)}
|
|
131
|
+
return {"status": "received"}
|
|
132
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
notion_cascade_insert/__init__.py,sha256=PTPiHewYdSbDtv-VQRxIRCW4-vkVdIAIDhtFvHAUomE,322
|
|
2
|
+
notion_cascade_insert/_modidx.py,sha256=Z2tOwCLdEpsBT0EAcjJwRLYQwbtx8vDvvHsXdB7m4ow,5806
|
|
3
|
+
notion_cascade_insert/core.py,sha256=_7tTLut_SlwA8r2B-FBBW3ChQPOmu7CZz3RtFUEWRrE,8193
|
|
4
|
+
notion_cascade_insert/webhook.py,sha256=myE6iRY232Zd2uBSfuepPcIPm4mopEYWmFzvfaWxF30,1214
|
|
5
|
+
notion_cascade_insert-0.0.1.dist-info/licenses/LICENSE,sha256=xV8xoN4VOL0uw9X8RSs2IMuD_Ss_a9yAbtGNeBWZwnw,11337
|
|
6
|
+
notion_cascade_insert-0.0.1.dist-info/METADATA,sha256=IV187x-EVC-uvLHjCSoIlWU52BchOVIhsI4xcgUI1Cc,6419
|
|
7
|
+
notion_cascade_insert-0.0.1.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
|
|
8
|
+
notion_cascade_insert-0.0.1.dist-info/entry_points.txt,sha256=XqKv0P7bGQlZ7SqZcyTdvIaQJI8GlMHtcJP9NPOhQbA,64
|
|
9
|
+
notion_cascade_insert-0.0.1.dist-info/top_level.txt,sha256=XRld1ok2pAh3DC9o3ZByqlORHGblJMVMOjE0pQRROE4,22
|
|
10
|
+
notion_cascade_insert-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2022, fastai
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
notion_cascade_insert
|