notion-cascade-insert 0.0.3__py3-none-any.whl → 0.0.5__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.
@@ -1,4 +1,4 @@
1
- __version__ = "0.0.3"
1
+ __version__ = "0.0.4"
2
2
  """Notion API wrapper for easy database cascade insert (one-to-many) automation"""
3
3
 
4
4
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/index.ipynb.
@@ -9,21 +9,19 @@ d = { 'settings': { 'branch': 'main',
9
9
  'notion_cascade_insert/core.py'),
10
10
  'notion_cascade_insert.core.AutoLogger.__init__': ( 'core.html#autologger.__init__',
11
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
12
  'notion_cascade_insert.core.AutoLogger.handle_update': ( 'core.html#autologger.handle_update',
17
13
  'notion_cascade_insert/core.py'),
18
14
  'notion_cascade_insert.core.AutoLogger.process': ( 'core.html#autologger.process',
19
15
  'notion_cascade_insert/core.py'),
16
+ 'notion_cascade_insert.core.AutoLogger.undo_all': ( 'core.html#autologger.undo_all',
17
+ 'notion_cascade_insert/core.py'),
20
18
  'notion_cascade_insert.core.Database': ('core.html#database', 'notion_cascade_insert/core.py'),
21
19
  'notion_cascade_insert.core.Database.__init__': ( 'core.html#database.__init__',
22
20
  'notion_cascade_insert/core.py'),
23
21
  'notion_cascade_insert.core.Database.check_db_exists': ( 'core.html#database.check_db_exists',
24
22
  'notion_cascade_insert/core.py'),
25
- 'notion_cascade_insert.core.Database.get_schema': ( 'core.html#database.get_schema',
26
- 'notion_cascade_insert/core.py'),
23
+ 'notion_cascade_insert.core.Database.get_data_source_schema': ( 'core.html#database.get_data_source_schema',
24
+ 'notion_cascade_insert/core.py'),
27
25
  'notion_cascade_insert.core.JunctionDB': ( 'core.html#junctiondb',
28
26
  'notion_cascade_insert/core.py'),
29
27
  'notion_cascade_insert.core.JunctionDB.__init__': ( 'core.html#junctiondb.__init__',
@@ -35,6 +33,8 @@ d = { 'settings': { 'branch': 'main',
35
33
  'notion_cascade_insert/core.py'),
36
34
  'notion_cascade_insert.core.LogDB.create_entry': ( 'core.html#logdb.create_entry',
37
35
  'notion_cascade_insert/core.py'),
36
+ 'notion_cascade_insert.core.LogDB.get_entries_for_trigger': ( 'core.html#logdb.get_entries_for_trigger',
37
+ 'notion_cascade_insert/core.py'),
38
38
  'notion_cascade_insert.core.TriggerDB': ( 'core.html#triggerdb',
39
39
  'notion_cascade_insert/core.py'),
40
40
  'notion_cascade_insert.core.TriggerDB.__init__': ( 'core.html#triggerdb.__init__',
@@ -5,6 +5,9 @@
5
5
  # %% auto #0
6
6
  __all__ = ['Database', 'TriggerDB', 'JunctionDB', 'LogDB', 'AutoLogger']
7
7
 
8
+ # %% ../nbs/00_core.ipynb #6211d16e
9
+ from fastcore.basics import patch
10
+
8
11
  # %% ../nbs/00_core.ipynb #68712bbc
9
12
  class Database:
10
13
  """Base class for interacting with a Notion database."""
@@ -18,14 +21,14 @@ class Database:
18
21
  db_info = self.notion.databases.retrieve(self.db_id)
19
22
  self.data_source_id = db_info['data_sources'][0]['id']
20
23
 
21
- def get_schema(self):
24
+ def get_data_source_schema(self):
22
25
  """Retrieve the database schema.
23
26
 
24
27
  Returns:
25
28
  dict: Mapping of property names to their types
26
29
  """
27
- db = self.notion.databases.retrieve(self.db_id)
28
- return {name: props['type'] for name, props in db['properties'].items()}
30
+ table = self.notion.data_sources.query(self.data_source_id)
31
+ return {name: props['type'] for name, props in table['results'][0]['properties'].items()}
29
32
 
30
33
  def check_db_exists(self):
31
34
  "Check if database exists and is accessible"
@@ -59,12 +62,16 @@ class TriggerDB(Database):
59
62
  def get_page_data(self,
60
63
  page_id # Notion page ID to retrieve (the row that trigger change)
61
64
  ):
62
- """Extract status, related item ID, and quantity from a page.
65
+ """Extract status, related item ID, and quantity from the trigged row
63
66
  """
64
67
  page = self.notion.pages.retrieve(page_id)
65
68
  status = page['properties'][self.status_prop]['select']['name'] if page['properties'][self.status_prop]['select'] else None
66
69
  relation_id = page['properties'][self.relation_prop]['relation'][0]['id'] if page['properties'][self.relation_prop]['relation'] else None
67
- qty = page['properties'][self.qty_prop]['number']
70
+
71
+ try:
72
+ qty = page['properties'][self.qty_prop]['number']
73
+ except (KeyError, TypeError):
74
+ qty = page['properties'][self.qty_prop]['formula']['number']
68
75
  return status, relation_id, qty
69
76
 
70
77
  # %% ../nbs/00_core.ipynb #82d6f2e9
@@ -158,39 +165,26 @@ class AutoLogger:
158
165
  if not items: return "No items found in junction"
159
166
  for item_id, amt in items.items(): self.log_db.create_entry(item_id, self.multiplier * qty * amt, page_id, reason)
160
167
  return f"Logged {len(items)} items"
161
-
162
- def cancel(self, page_id):
163
- """Reverse all logs for a trigger (restore inventory)."""
164
- _, relation_id, qty = self.trigger_db.get_page_data(page_id)
165
- items = self.junction_db.get_items(relation_id)
166
- if not items: return "No items to reverse"
167
- for item_id, amt in items.items(): self.log_db.create_entry(item_id, -self.multiplier * qty * amt, page_id, 'cancelled')
168
- return f"Reversed {len(items)} items"
169
-
170
- def adjust_batch(self,
171
- page_id, # Trigger page ID being adjusted
172
- old_qty, # Previous quantity value
173
- new_qty # New quantity value
174
- ):
175
- """Log the difference when batch quantity changes."""
176
- status, relation_id, _ = self.trigger_db.get_page_data(page_id)
177
- if status != self.trigger_status: return f"Status is {status}, not active"
178
- items = self.junction_db.get_items(relation_id)
179
- if not items: return "No items found"
180
- delta = new_qty - old_qty
181
- for item_id, amt in items.items(): self.log_db.create_entry(item_id, self.multiplier * delta * amt, page_id, 'batch_adjusted')
182
- return f"Adjusted {len(items)} items by {delta} batches"
183
-
184
- def handle_update(self,
185
- page_id, # Trigger page ID that was updated
186
- old_status=None, # Previous status value
187
- old_qty=None # Previous quantity value
188
- ):
189
- """Handle any update to trigger page - detects what changed and acts accordingly."""
190
- status, _, qty = self.trigger_db.get_page_data(page_id)
191
- if old_status and old_status == self.trigger_status and status != self.trigger_status: return self.cancel(page_id)
192
- if status == self.trigger_status:
193
- if old_status and old_status != self.trigger_status: return self.process(page_id)
194
- if old_qty and old_qty != qty: return self.adjust_batch(page_id, old_qty, qty)
195
- return "No action needed"
196
168
 
169
+ # %% ../nbs/00_core.ipynb #da5abd7e
170
+ @patch
171
+ def get_entries_for_trigger(self:LogDB, trigger_page_id):
172
+ "Find all log entries linked to a specific trigger page"
173
+ results = self.notion.databases.query(database_id=self.db_id, filter={"property": self.trigger_prop, "relation": {"contains": trigger_page_id}})
174
+ return [(p['id'], p['properties'][self.item_prop]['relation'][0]['id'], p['properties'][self.amt_prop]['number']) for p in results['results']]
175
+
176
+ @patch
177
+ def undo_all(self:AutoLogger, page_id, reason='Undo Changes'):
178
+ "Reverse all existing log entries for a trigger page"
179
+ entries = self.log_db.get_entries_for_trigger(page_id)
180
+ if not entries: return "No entries to undo"
181
+ for _, item_id, amt in entries: self.log_db.create_entry(item_id, -amt, page_id, reason)
182
+ return f"Undone {len(entries)} entries"
183
+
184
+ @patch
185
+ def handle_update(self:AutoLogger, page_id, reason='Update Production'):
186
+ "Handle trigger page updates by undoing and reprocessing"
187
+ status, _, _ = self.trigger_db.get_page_data(page_id)
188
+ self.undo_all(page_id, reason)
189
+ if status == self.trigger_status: return self.process(page_id, reason)
190
+ return f"Undone entries (status now {status})"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: notion_cascade_insert
3
- Version: 0.0.3
3
+ Version: 0.0.5
4
4
  Summary: Cascade inserting for Notion Database Automation (as log)
5
5
  Home-page: https://github.com/amezaikupan/notion_cascade_insert
6
6
  Author: TN
@@ -0,0 +1,10 @@
1
+ notion_cascade_insert/__init__.py,sha256=UOJcVl2OK2kvj_6VbbjMftGdtixim8EJ7fbRVbUByJI,322
2
+ notion_cascade_insert/_modidx.py,sha256=VAD0TLHCIbcd5a8Fgr378dXyw_shDAl2ss-GM4_4LMc,6140
3
+ notion_cascade_insert/core.py,sha256=8nPi-nGtzHZ4xKmb62wKlIPKsahJtTPE8u5Ilrq_0lY,8273
4
+ notion_cascade_insert/webhook.py,sha256=myE6iRY232Zd2uBSfuepPcIPm4mopEYWmFzvfaWxF30,1214
5
+ notion_cascade_insert-0.0.5.dist-info/licenses/LICENSE,sha256=xV8xoN4VOL0uw9X8RSs2IMuD_Ss_a9yAbtGNeBWZwnw,11337
6
+ notion_cascade_insert-0.0.5.dist-info/METADATA,sha256=G2tsRYM0gbMBuLEXmXEvkwVSSQKWqQb4Z_MNLDQP5po,3724
7
+ notion_cascade_insert-0.0.5.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
8
+ notion_cascade_insert-0.0.5.dist-info/entry_points.txt,sha256=XqKv0P7bGQlZ7SqZcyTdvIaQJI8GlMHtcJP9NPOhQbA,64
9
+ notion_cascade_insert-0.0.5.dist-info/top_level.txt,sha256=XRld1ok2pAh3DC9o3ZByqlORHGblJMVMOjE0pQRROE4,22
10
+ notion_cascade_insert-0.0.5.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- notion_cascade_insert/__init__.py,sha256=auXNGZG-UuY84e9PtdBQpmFqSBmZvpdPH-yqwWW6UfY,322
2
- notion_cascade_insert/_modidx.py,sha256=iaCld590eNIbQeklcaoYoqTdF6fuSOpmCkPx-diM9k4,6080
3
- notion_cascade_insert/core.py,sha256=9Xe5ypDZnZZh8lm5fFyEaVz6s5EoCECGZJcQq25_3fw,8808
4
- notion_cascade_insert/webhook.py,sha256=myE6iRY232Zd2uBSfuepPcIPm4mopEYWmFzvfaWxF30,1214
5
- notion_cascade_insert-0.0.3.dist-info/licenses/LICENSE,sha256=xV8xoN4VOL0uw9X8RSs2IMuD_Ss_a9yAbtGNeBWZwnw,11337
6
- notion_cascade_insert-0.0.3.dist-info/METADATA,sha256=J9HdPs-Oatkt_gQPFo23_rqZqy376upc7i3GOuHWLi0,3724
7
- notion_cascade_insert-0.0.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
8
- notion_cascade_insert-0.0.3.dist-info/entry_points.txt,sha256=XqKv0P7bGQlZ7SqZcyTdvIaQJI8GlMHtcJP9NPOhQbA,64
9
- notion_cascade_insert-0.0.3.dist-info/top_level.txt,sha256=XRld1ok2pAh3DC9o3ZByqlORHGblJMVMOjE0pQRROE4,22
10
- notion_cascade_insert-0.0.3.dist-info/RECORD,,