use single database accross chats -> no storage leaks
This commit is contained in:
parent
d9f2502a8a
commit
3455946996
@ -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)
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user