]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/commitdiff
WIP add ItemZone
authorEdward Delaporte <edward@delaporte.us>
Fri, 5 Jun 2026 17:54:22 +0000 (12:54 -0500)
committerEdward Delaporte <edward@delaporte.us>
Fri, 5 Jun 2026 17:54:22 +0000 (12:54 -0500)
matrix_shop_bot/db.py

index ce0d46ec2681d31ca64dda6c68bc7ea356d1f3f5..464e8f165c472bd8508ff166b104de5fe4e895b9 100644 (file)
@@ -24,6 +24,7 @@ class ItemStoreLink(SQLModel, table=True):
 
 
 class Item(SQLModel, table=True):
+    '''An item to shop for.'''
     id: int | None = Field(default=None, primary_key=True)
     name: str = Field(default=None, unique=True)
     price: Decimal | None = Field(decimal_places=2)
@@ -32,10 +33,22 @@ class Item(SQLModel, table=True):
     def __str__(self):
         return f'{self.name} (#{self.id})'
 
+class ItemZone(SQLModel, table=True):
+    '''Where an item is usually found.
+
+    Matched only by loose text matching, since items come and go, over time.
+    '''
+    id: int | None = Field(default=None, primary_key=True)
+    name: str = Field(default=None, unique=True)  # milk
+    zone: str = Field(default=None, unique=False) # dairy section
+
+
 class PreferStore(SQLModel, table=True):
+    '''Tracks which store we are shopping at, at the moment.'''
     store_id: int | None = Field(default=None, foreign_key="store.id", primary_key=True)
 
 class Store(SQLModel, table=True):
+    '''A place to shop at.'''
     id: int | None = Field(default=None, primary_key=True)
     name: str = Field(default=None, unique=True)
     shopping_list: list["Item"] = Relationship(back_populates="stores", link_model=ItemStoreLink)
@@ -58,8 +71,8 @@ class Store(SQLModel, table=True):
         return results
 
 
-# May get used later...
 class PurchaseRecord(SQLModel, table=True):
+    '''Tracks when and where items were purchased.'''
     id: int | None = Field(default=None, primary_key=True)
     name: str = Field(default=None)
     price: Decimal | None = Field(decimal_places=2)
@@ -135,6 +148,19 @@ class Data():
         self.assign_store(item_id, store_id)
         return store_id
 
+    def assign_zone(self, item_name, zone_name):
+        item_id = -1
+        with Session(self.engine) as session:
+            try:
+                new = ItemZone(name=item_name)
+                new.zone = zone_name
+                session.add(new)
+                session.commit()
+                item_id = new.id
+            except:
+                pass # Already assigned
+        return item_id
+
     def assign_store(self, item_id, store_id):
         self._set_pref_store_id(store_id)