From 8b639943d820c97787fed92731f0ec8d13e9ac7e Mon Sep 17 00:00:00 2001 From: Edward Delaporte Date: Thu, 9 Apr 2026 16:47:59 -0500 Subject: [PATCH] Shop at now uses a case insensitive string --- matrix_shop_bot/bot.py | 4 +-- matrix_shop_bot/db.py | 56 ++++++++++++++++++++++------------------ matrix_shop_bot/tasks.py | 6 ++--- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/matrix_shop_bot/bot.py b/matrix_shop_bot/bot.py index 37677c5..037de7f 100644 --- a/matrix_shop_bot/bot.py +++ b/matrix_shop_bot/bot.py @@ -109,8 +109,8 @@ async def do_shop(match): 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)) diff --git a/matrix_shop_bot/db.py b/matrix_shop_bot/db.py index d56bb6a..60339ee 100644 --- a/matrix_shop_bot/db.py +++ b/matrix_shop_bot/db.py @@ -4,7 +4,7 @@ from datetime import datetime as dt 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 @@ -43,6 +43,21 @@ class Store(SQLModel, table=True): 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) @@ -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) - 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_name(cls, name): + return select(cls).where(func.lower(cls.name) == func.lower(name)) + class Data(): def __init__(self, db_file=None): @@ -189,34 +207,22 @@ class Data(): 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() - 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: - 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: - 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): diff --git a/matrix_shop_bot/tasks.py b/matrix_shop_bot/tasks.py index 52484a0..6b999bf 100644 --- a/matrix_shop_bot/tasks.py +++ b/matrix_shop_bot/tasks.py @@ -55,12 +55,12 @@ def clear_list(c, store_id): @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) - @task def list(c): '''Use previous store''' -- 2.47.3