]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/commitdiff
Shop at now uses a case insensitive string
authorEdward Delaporte <edward@delaporte.us>
Thu, 9 Apr 2026 21:47:59 +0000 (16:47 -0500)
committerEdward Delaporte <edward@delaporte.us>
Sun, 7 Jun 2026 00:29:23 +0000 (19:29 -0500)
matrix_shop_bot/bot.py
matrix_shop_bot/db.py
matrix_shop_bot/tasks.py

index 37677c53bec9e3762b3327f87f27f545bea3aeec..037de7fdcb461d6dc5b09a8ad536ec89272d00e8 100644 (file)
@@ -109,8 +109,8 @@ async def do_shop(match):
 
 
     if match.command('at'):
 
 
     if match.command('at'):
-        store_id = ' '.join(match.args()[1])
-        items = Data().get_shopping_list_for_store(store_id)
+        store_name = ' '.join(match.args()[1:])
+        items = Data().get_shopping_list(store_name)
         results = ['Shopping List']
         for item in items:
             results.append(str(item))
         results = ['Shopping List']
         for item in items:
             results.append(str(item))
index d56bb6a0c7529b178a8dab60164ffbcbca07b63a..60339ee13e9d290c71d6ea395046b733a63502d2 100644 (file)
@@ -4,7 +4,7 @@ from datetime import datetime as dt
 from decimal import Decimal
 from typing import List
 
 from decimal import Decimal
 from typing import List
 
-from sqlmodel import Field, Session, SQLModel, create_engine, select, text, Relationship
+from sqlmodel import Field, Session, SQLModel, create_engine, select, text, Relationship, func
 
 from sqlite3 import IntegrityError
 
 
 from sqlite3 import IntegrityError
 
@@ -43,6 +43,21 @@ class Store(SQLModel, table=True):
     def __str__(self):
         return f'{self.name} (#{self.id})'
 
     def __str__(self):
         return f'{self.name} (#{self.id})'
 
+    def get_pretty_shopping_list(self) -> List[str]:
+        results = []
+        results.append(f' ## {self.name}')
+        items = self.shopping_list
+        if not items:
+            results.append(f'-- Nothing to shop for at {self.name} --')
+        else:
+            for item in items:
+                if(item.purchase_date):
+                    results.append(f' - [x] {item}')
+                else:
+                    results.append(f' - [ ] {item}')
+        return results
+
+
 # May get used later...
 class PurchaseRecord(SQLModel, table=True):
     id: int | None = Field(default=None, primary_key=True)
 # May get used later...
 class PurchaseRecord(SQLModel, table=True):
     id: int | None = Field(default=None, primary_key=True)
@@ -52,11 +67,14 @@ class PurchaseRecord(SQLModel, table=True):
     purchase_date: dt | None = Field(default=None)
     def __str__(self):
         date = self.purchase_date.strftime(DATE_FORMAT)
     purchase_date: dt | None = Field(default=None)
     def __str__(self):
         date = self.purchase_date.strftime(DATE_FORMAT)
-        return f'{self.name} at {self.store} on {date}'
+        return f'Bought {self.name} at {self.store} on {date}'
 
 def _get_with_id(cls, item_id):
     return select(cls).where(cls.id == item_id)
 
 
 def _get_with_id(cls, item_id):
     return select(cls).where(cls.id == item_id)
 
+def _get_with_name(cls, name):
+    return select(cls).where(func.lower(cls.name) == func.lower(name))
+
 
 class Data():
     def __init__(self, db_file=None):
 
 class Data():
     def __init__(self, db_file=None):
@@ -189,34 +207,22 @@ class Data():
                 return [f'No unsorted items!']
             return items
 
                 return [f'No unsorted items!']
             return items
 
-
-    def get_shopping_list_for_store(self, store_id) -> List[str]:
-        self._set_pref_store_id(store_id)
-        return self.get_shopping_list(store_id)
-
     def get_last_shopping_list(self) -> List[str]:
         store_id = self._get_pref_store_id()
     def get_last_shopping_list(self) -> List[str]:
         store_id = self._get_pref_store_id()
-        return self.get_shopping_list(store_id)
+        with Session(self.engine) as session:
+            store = session.exec(_get_with_id(Store, store_id)).first()
+            if store:
+                return store.get_pretty_shopping_list()
+        return [f'Last shopping list store not found.']
 
 
-    def get_shopping_list(self, store_id) -> List[str]:
-        results = []
+    def get_shopping_list(self, store_name) -> List[str]:
+        retsults = []
         with Session(self.engine) as session:
         with Session(self.engine) as session:
-            cmd = select(Store).where(Store.id == store_id)
-            store = session.exec(cmd).first()
+            store = session.exec(_get_with_name(Store, store_name)).first()
             if not store:
             if not store:
-                return [f'Store {store_id} not found.']
-
-            results.append(f' ## {store.name}')
-
-            items = store.shopping_list
-            if not items:
-                results.append(f'-- Nothing to shop for at {store.name} --')
-            else:
-                for item in items:
-                    if(item.purchase_date):
-                        results.append(f' - [x] {item}')
-                    else:
-                        results.append(f' - [ ] {item}')
+                return [f'Store {store_name} not found.']
+            results = store.get_pretty_shopping_list()
+        self._set_pref_store_id(store.id)
         return results
 
     def remove_item(self, item_id):
         return results
 
     def remove_item(self, item_id):
index 52484a0c58378cc1a09247b3696c953c32c1880d..6b999bfb192720fa4814f2568c80bd893468d92d 100644 (file)
@@ -55,12 +55,12 @@ def clear_list(c, store_id):
 
 
 @task
 
 
 @task
-def list_for_store(c, store_id):
-    items = Data().get_shopping_list_for_store(store_id)
+def at(c, store_name):
+    '''Select store to shop at'''
+    items = Data().get_shopping_list(store_name)
     for item in items:
         print(item)
 
     for item in items:
         print(item)
 
-
 @task
 def list(c):
     '''Use previous store'''
 @task
 def list(c):
     '''Use previous store'''