Lists with more functionality #3
@@ -5,10 +5,7 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from .models import ListModel, set_db, db
 | 
					from .models import ListModel, set_db, db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
MEDIA_DIR = Path(os.getenv("MEDIA_DIR"))
 | 
					PERSISTENCE_DIR = Path(os.getenv("PERSISTENCE_DIR"))
 | 
				
			||||||
DB_DIR = MEDIA_DIR / "lists_db"
 | 
					 | 
				
			||||||
DB_DIR.mkdir(parents=True, exist_ok=True)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
NAME, NEW, ACTION, ITEMADD, ITEMREMOVE, ITEMTOGGLE = range(6)
 | 
					NAME, NEW, ACTION, ITEMADD, ITEMREMOVE, ITEMTOGGLE = range(6)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -19,9 +16,11 @@ class ListHandler(BaseHandler):
 | 
				
			|||||||
    """Create and edit lists"""
 | 
					    """Create and edit lists"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, entry_string, models):
 | 
					    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
 | 
					        self.entry_string = entry_string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        set_db(PERSISTENCE_DIR / "lists.sqlite")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.list_overview_keyboard = [
 | 
					        self.list_overview_keyboard = [
 | 
				
			||||||
            [InlineKeyboardButton("Add item", callback_data="add")],
 | 
					            [InlineKeyboardButton("Add item", callback_data="add")],
 | 
				
			||||||
            [InlineKeyboardButton("Toggle item", callback_data="toggle")],
 | 
					            [InlineKeyboardButton("Toggle item", callback_data="toggle")],
 | 
				
			||||||
@@ -58,10 +57,9 @@ class ListHandler(BaseHandler):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    async def entry_point(self, update, context) -> None:
 | 
					    async def entry_point(self, update, context) -> None:
 | 
				
			||||||
        await super().entry_point(update, context)
 | 
					        await super().entry_point(update, context)
 | 
				
			||||||
        set_db(DB_DIR / f"chat_{update.message.chat_id}.db")
 | 
					 | 
				
			||||||
        with db:
 | 
					        with db:
 | 
				
			||||||
            lists = ListModel.select()
 | 
					            lists = ListModel.select().where(ListModel.chat_id == update.effective_chat.id)
 | 
				
			||||||
        keyboard = [[InlineKeyboardButton(k.name, callback_data=f"list-{k.name}")] for k in lists] + \
 | 
					        keyboard = [[InlineKeyboardButton(k.name, callback_data=f"list-{k.id}")] for k in lists] + \
 | 
				
			||||||
            [[InlineKeyboardButton("New list", callback_data="new")]]
 | 
					            [[InlineKeyboardButton("New list", callback_data="new")]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        reply_markup = InlineKeyboardMarkup(keyboard)
 | 
					        reply_markup = InlineKeyboardMarkup(keyboard)
 | 
				
			||||||
@@ -72,13 +70,13 @@ class ListHandler(BaseHandler):
 | 
				
			|||||||
    async def choose_list(self, update, context: CallbackContext) -> None:
 | 
					    async def choose_list(self, update, context: CallbackContext) -> None:
 | 
				
			||||||
        query = update.callback_query
 | 
					        query = update.callback_query
 | 
				
			||||||
        data = query.data
 | 
					        data = query.data
 | 
				
			||||||
        name = data.replace("list-","")
 | 
					        id = data.replace("list-","")
 | 
				
			||||||
        await query.answer()
 | 
					        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)
 | 
					        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
 | 
					        return ACTION
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -88,7 +86,7 @@ class ListHandler(BaseHandler):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        reply_markup = InlineKeyboardMarkup(self.list_overview_keyboard)
 | 
					        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
 | 
					        return ACTION
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -103,7 +101,7 @@ class ListHandler(BaseHandler):
 | 
				
			|||||||
        name = update.message.text
 | 
					        name = update.message.text
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            with db:
 | 
					            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")]]
 | 
					            keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("To the menu!", callback_data="overview")]]
 | 
				
			||||||
            reply_markup = InlineKeyboardMarkup(keyboard)
 | 
					            reply_markup = InlineKeyboardMarkup(keyboard)
 | 
				
			||||||
            await update.message.reply_text("Thanks. List " + name + " was successfully created.", reply_markup=reply_markup)
 | 
					            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):
 | 
					class ListModel(BaseModel):
 | 
				
			||||||
    name = CharField(default="")
 | 
					    name = CharField(default="")
 | 
				
			||||||
 | 
					    chat_id = IntegerField()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def content(self) -> dict:
 | 
					    def content(self) -> dict:
 | 
				
			||||||
@@ -45,22 +46,6 @@ class ListEntryModel(BaseModel):
 | 
				
			|||||||
    done = BooleanField(default=None, null=True)
 | 
					    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):
 | 
					def set_db(db_path):
 | 
				
			||||||
    db.initialize(SqliteDatabase(db_path))
 | 
					    db.initialize(SqliteDatabase(db_path))
 | 
				
			||||||
    with db:
 | 
					    with db:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user