use db contexts for better stability
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -3,7 +3,7 @@ from pathlib import Path
|
||||
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler
|
||||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
||||
|
||||
from .models import ListModel, set_db
|
||||
from .models import ListModel, set_db, db
|
||||
|
||||
MEDIA_DIR = Path(os.getenv("MEDIA_DIR"))
|
||||
DB_DIR = MEDIA_DIR / "lists_db"
|
||||
@@ -19,7 +19,7 @@ class ListHandler(BaseHandler):
|
||||
"""Create and edit lists"""
|
||||
|
||||
def __init__(self, entry_string, models):
|
||||
self.models = models
|
||||
self.journal_models = models # not needed here
|
||||
self.entry_string = entry_string
|
||||
self.handler = ConversationHandler(
|
||||
entry_points=[CommandHandler(entry_string, self.entry_point)],
|
||||
@@ -47,7 +47,8 @@ class ListHandler(BaseHandler):
|
||||
async def entry_point(self, update, context) -> None:
|
||||
await super().entry_point(update, context)
|
||||
set_db(DB_DIR / f"{update.message.chat_id}.db")
|
||||
lists = ListModel.select()
|
||||
with db:
|
||||
lists = ListModel.select()
|
||||
keyboard = [[InlineKeyboardButton(k.name, callback_data=f"list-{k.name}")] for k in lists] + \
|
||||
[[InlineKeyboardButton("New list", callback_data="new")]]
|
||||
|
||||
@@ -103,7 +104,8 @@ class ListHandler(BaseHandler):
|
||||
async def new_listname(self, update, context) -> None:
|
||||
name = update.message.text
|
||||
try:
|
||||
ListModel.create(name = name)
|
||||
with db:
|
||||
ListModel.create(name = name)
|
||||
keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("To the menu!", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
self.current_name = name
|
||||
@@ -124,7 +126,8 @@ class ListHandler(BaseHandler):
|
||||
async def list_remove(self, update, context) -> None:
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
list_object = ListModel.get(name = self.current_name)
|
||||
with db:
|
||||
list_object = ListModel.get(name = self.current_name)
|
||||
|
||||
keyboard = [[InlineKeyboardButton(k, callback_data=i)] for i,k in enumerate(list_object.content_list)]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
@@ -136,7 +139,8 @@ class ListHandler(BaseHandler):
|
||||
async def list_clear(self, update, context) -> None:
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
ListModel.get(name = self.current_name).content_list = []
|
||||
with db:
|
||||
ListModel.get(name = self.current_name).content_list = []
|
||||
keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
await query.edit_message_text("List " + self.current_name + " cleared", reply_markup=reply_markup)
|
||||
@@ -146,7 +150,8 @@ class ListHandler(BaseHandler):
|
||||
async def list_delete(self, update, context) -> None:
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
ListModel.get(name = self.current_name).delete_instance()
|
||||
with db:
|
||||
ListModel.get(name = self.current_name).delete_instance()
|
||||
await query.edit_message_text("List " + self.current_name + " deleted")
|
||||
return ConversationHandler.END
|
||||
|
||||
@@ -154,7 +159,8 @@ class ListHandler(BaseHandler):
|
||||
async def list_print(self, update, context) -> None:
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
it = ListModel.get(name = self.current_name).content_list
|
||||
with db:
|
||||
it = ListModel.get(name = self.current_name).content_list
|
||||
if it:
|
||||
content = "·" + "\n· ".join(it)
|
||||
else:
|
||||
@@ -168,7 +174,8 @@ class ListHandler(BaseHandler):
|
||||
|
||||
async def list_add_item(self, update, context) -> None:
|
||||
item = update.message.text
|
||||
ListModel.get(name = self.current_name).content_list = ListModel.get(name = self.current_name).content_list + [item]
|
||||
with db:
|
||||
ListModel.get(name = self.current_name).content_list = ListModel.get(name = self.current_name).content_list + [item]
|
||||
# TODO test me!
|
||||
keyboard = [[InlineKeyboardButton("Add some more", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
@@ -181,10 +188,11 @@ class ListHandler(BaseHandler):
|
||||
ind = int(query.data)
|
||||
await query.answer()
|
||||
|
||||
list_object = ListModel.get(name = self.current_name)
|
||||
old = list_object.content_list
|
||||
name = old.pop(ind)
|
||||
list_object.content_list = old
|
||||
with db:
|
||||
list_object = ListModel.get(name = self.current_name)
|
||||
old = list_object.content_list
|
||||
name = old.pop(ind)
|
||||
list_object.content_list = old
|
||||
|
||||
keyboard = [[InlineKeyboardButton("Remove another", callback_data="remove"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
Reference in New Issue
Block a user