]> apps.delaporte.us Git - matrix-shop-bot.git/commitdiff
Add rename commands
authorEdward Delaporte <edward@delaporte.us>
Sun, 14 Jun 2026 01:43:20 +0000 (20:43 -0500)
committerEdward Delaporte <edward@delaporte.us>
Sun, 14 Jun 2026 01:43:20 +0000 (20:43 -0500)
matrix_shop_bot/bot.py
matrix_shop_bot/db.py
matrix_shop_bot/tasks.py

index 53d1ec642b22036717196b81758dac05a2fb03fb..ee56105c77384feea18483b715ad5e267641ea79 100644 (file)
@@ -53,11 +53,13 @@ async def do_shop(match):
       assign ITEM_ID - assign item to last used store id
       assign.item.to.store ITEM_ID STORE_ID - plan to shop for at store for item
       clear.all STORE - clear **all** items for STORE, including un-purchased
+      rename OLD,NEW - rename an item
+      rename.store OLD,NEW - rename a store
       remove ITEM_ID - remove a shopping item from the datatbase
       remove.store STORE_ID - remove a store from the datatbase (requires clearing the list first)
       setup - run one time database setup
       unsorted - list all items not assigned to any store
-      zone ZONE ITEM - list ITEM under given ZONE 
+      zone ZONE ITEM - list ITEM under given ZONE - ZONE may not contain spaces
 
       Example: 
         shop zone dairy sour cream
@@ -160,6 +162,16 @@ async def do_shop(match):
             results.append(str(item))
         return results
 
+    if match.command('rename'):
+        old,new = ' '.join(match.args()[1:]).split(',')
+        Data().rename_item(old,new)
+        return ['Success']
+
+    if match.command('rename.store'):
+        old,new = ' '.join(match.args()[1:]).split(',')
+        Data().rename_store(old,new)
+        return ['Success']
+
     if match.command('remove'):
         item_id = ' '.join(match.args()[1:])
         Data().remove_item(item_id)
index cd8d494cacfe16832b5cceee08c82d0f7c979f2c..cf75f84247051386ebb1cf396d30d4882fe01cb0 100644 (file)
@@ -105,6 +105,12 @@ def _get_with_id(cls, item_id):
 def _get_with_name(cls, name):
     return select(cls).where(func.lower(cls.name) == func.lower(name))
 
+def _rename(cls, session, old, new) -> bool:
+    with session:
+        record = session.exec(_get_with_name(cls, old)).first()
+        record.name = new
+        session.commit()
+    return True
 
 class Data():
     def __init__(self, db_file=None):
@@ -116,6 +122,14 @@ class Data():
         self.engine = self.get_engine()
         self.last_store_id = None
 
+    def rename_store(self, old, new):
+        with Session(self.engine) as session:
+            return _rename(Store, session, old, new)
+
+    def rename_item(self, old, new):
+        with Session(self.engine) as session:
+            return _rename(Item, session, old, new)
+
     def _set_pref_store_id(self, store_id):
         with Session(self.engine) as session:
             prefs = session.exec(select(PreferStore)).all()
index 61bd5b787b7589922a3eed0d6e96f64142e2df33..580eeba57fb3e8c54a9b8faf96387b6d1cdc8ddb 100644 (file)
@@ -84,6 +84,14 @@ def list_unsorted(c):
         print(item)
 
 
+@task
+def rename_item(c, old, new):
+    Data().rename_item(old, new)
+
+@task
+def rename_store(c, old, new):
+    Data().rename_store(old, new)
+
 @task
 def remove_item(c, item_id):
     Data().remove_item(item_id)