From 345594699614b15c195659652508900ada9513b1 Mon Sep 17 00:00:00 2001 From: Remy Moll Date: Fri, 23 Jun 2023 13:01:07 +0200 Subject: [PATCH] use single database accross chats -> no storage leaks --- bot/commands/list/list.py | 24 +++++++++++------------- bot/commands/list/models.py | 17 +---------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/bot/commands/list/list.py b/bot/commands/list/list.py index 9bf3f31..bcd1a31 100644 --- a/bot/commands/list/list.py +++ b/bot/commands/list/list.py @@ -5,10 +5,7 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup from .models import ListModel, set_db, db -MEDIA_DIR = Path(os.getenv("MEDIA_DIR")) -DB_DIR = MEDIA_DIR / "lists_db" -DB_DIR.mkdir(parents=True, exist_ok=True) - +PERSISTENCE_DIR = Path(os.getenv("PERSISTENCE_DIR")) NAME, NEW, ACTION, ITEMADD, ITEMREMOVE, ITEMTOGGLE = range(6) @@ -19,9 +16,11 @@ class ListHandler(BaseHandler): """Create and edit lists""" def __init__(self, entry_string, models): - self.journal_models = models # not needed here + del models # not needed here, but part of the template self.entry_string = entry_string + set_db(PERSISTENCE_DIR / "lists.sqlite") + self.list_overview_keyboard = [ [InlineKeyboardButton("Add item", callback_data="add")], [InlineKeyboardButton("Toggle item", callback_data="toggle")], @@ -58,10 +57,9 @@ class ListHandler(BaseHandler): async def entry_point(self, update, context) -> None: await super().entry_point(update, context) - set_db(DB_DIR / f"chat_{update.message.chat_id}.db") with db: - lists = ListModel.select() - keyboard = [[InlineKeyboardButton(k.name, callback_data=f"list-{k.name}")] for k in lists] + \ + lists = ListModel.select().where(ListModel.chat_id == update.effective_chat.id) + keyboard = [[InlineKeyboardButton(k.name, callback_data=f"list-{k.id}")] for k in lists] + \ [[InlineKeyboardButton("New list", callback_data="new")]] reply_markup = InlineKeyboardMarkup(keyboard) @@ -72,13 +70,13 @@ class ListHandler(BaseHandler): async def choose_list(self, update, context: CallbackContext) -> None: query = update.callback_query data = query.data - name = data.replace("list-","") + id = data.replace("list-","") await query.answer() - context.user_data["current_list"] = ListModel.get(name = name) + context.user_data["current_list"] = ListModel.get(id = id) reply_markup = InlineKeyboardMarkup(self.list_overview_keyboard) - await query.edit_message_text("Very well. For " + name + " the following actions are available:", reply_markup=reply_markup) + await query.edit_message_text(f"Using {context.user_data['current_list'].name}. Available actions:", reply_markup=reply_markup) return ACTION @@ -88,7 +86,7 @@ class ListHandler(BaseHandler): reply_markup = InlineKeyboardMarkup(self.list_overview_keyboard) - await query.edit_message_text(f"Very well. For {context.user_data['current_list'].name} the following actions are available:", reply_markup=reply_markup) + await query.edit_message_text(f"Using {context.user_data['current_list'].name}. Available actions:", reply_markup=reply_markup) return ACTION @@ -103,7 +101,7 @@ class ListHandler(BaseHandler): name = update.message.text try: with db: - context.user_data["current_list"] = ListModel.create(name = name) + context.user_data["current_list"] = ListModel.create(name = name, chat_id=update.effective_chat.id) keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("To the menu!", callback_data="overview")]] reply_markup = InlineKeyboardMarkup(keyboard) await update.message.reply_text("Thanks. List " + name + " was successfully created.", reply_markup=reply_markup) diff --git a/bot/commands/list/models.py b/bot/commands/list/models.py index a8ed7e1..8d5379c 100644 --- a/bot/commands/list/models.py +++ b/bot/commands/list/models.py @@ -8,6 +8,7 @@ class BaseModel(Model): class ListModel(BaseModel): name = CharField(default="") + chat_id = IntegerField() @property def content(self) -> dict: @@ -45,22 +46,6 @@ class ListEntryModel(BaseModel): done = BooleanField(default=None, null=True) -# class ListModel(BaseModel): -# name = CharField(unique=True) -# content = TextField(default="") # unlimited length, use to serialise list into - -# @property -# def content_list(self): -# return json.loads(self.content or '[]') - -# @content_list.setter -# def content_list(self, list_content): -# self.content = json.dumps(list_content) -# with db: -# self.save() - - - def set_db(db_path): db.initialize(SqliteDatabase(db_path)) with db: