From: Edward Delaporte Date: Thu, 9 Apr 2026 13:27:15 +0000 (-0500) Subject: Fixup for purchase records X-Git-Url: https://us-delaporte.pagekite.me/gitweb/matrix-shop-bot.git/commitdiff_plain/61e15eca3a7832fb762ab0e7295b2e235b0e093b?hp=dbe3765edb809f29f17baf9f638089eb40f4eda7 Fixup for purchase records --- diff --git a/matrix_shop_bot/db.py b/matrix_shop_bot/db.py index a6d51e3..d56bb6a 100644 --- a/matrix_shop_bot/db.py +++ b/matrix_shop_bot/db.py @@ -50,6 +50,9 @@ class PurchaseRecord(SQLModel, table=True): price: Decimal | None = Field(decimal_places=2) store: str | None = Field(default=None) 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}' def _get_with_id(cls, item_id): return select(cls).where(cls.id == item_id) @@ -61,7 +64,7 @@ class Data(): db_file = os.path.join(os.environ.get('MATRIX_DATA_DIR', ''), f'{MODULE}.db') self.file = db_file - logger.debug(f'Using db file {self.file}') + logger.debug(f'Shop bot is using db file {self.file}') self.engine = self.get_engine() self.last_store_id = None @@ -141,7 +144,6 @@ class Data(): item = session.exec(_get_with_id(Item, item_id)).first() if item: item.purchase_date = dt.today() - results.append(str(item)) # Track purchase history (locally only!) # to generate recommended shopping lists @@ -149,9 +151,16 @@ class Data(): record.name = item.name record.price = item.price record.purchase_date = item.purchase_date - record.store = self._get_pref_store_id() # We bought it here + + shopping_at = self._get_pref_store_id() # We bought it here + cmd = select(Store).where(Store.id == shopping_at) + store = session.exec(cmd).one() + record.store = store.name session.add(record) + results.append(str(record)) + # results.append(str(item)) + session.commit() return results diff --git a/matrix_shop_bot/tasks.py b/matrix_shop_bot/tasks.py index f4775fa..52484a0 100644 --- a/matrix_shop_bot/tasks.py +++ b/matrix_shop_bot/tasks.py @@ -42,8 +42,8 @@ def buy(c, items): @task def list_bought(c, store): result = Data().list_purchases(store) - print(result) - + for item in result: + print(str(item)) @task def clear_all(c, store_id):