diff --git a/back/tgbot/tgbot.py b/back/tgbot/tgbot.py index a580357..1ec9126 100644 --- a/back/tgbot/tgbot.py +++ b/back/tgbot/tgbot.py @@ -97,17 +97,20 @@ class TgBot: self.inv, ) ) + TgBot.app.add_handler(CommandHandler("ter", self.ter, filters.ChatType.PRIVATE)) + TgBot.app.add_handler(CallbackQueryHandler(self.ter_back, "ter_back")) + TgBot.app.add_handler(CallbackQueryHandler(self.ter_next, "ter_next")) + TgBot.app.add_handler( MessageHandler( filters.ChatType.PRIVATE & filters.Regex(r"/ter_(\d.*)"), - self.ter_deep, + self.terdeep, ) ) - TgBot.app.add_handler(CallbackQueryHandler(self.ter_back, "ter_back")) - TgBot.app.add_handler(CallbackQueryHandler(self.ter_next, "ter_next")) TgBot.app.add_handler(CallbackQueryHandler(self.terdeep_back, "terdeep_back")) TgBot.app.add_handler(CallbackQueryHandler(self.terdeep_next, "terdeep_next")) + TgBot.app.add_handler(CallbackQueryHandler(self.stop_inv, "stop_inv")) TgBot.app.add_handler(CallbackQueryHandler(self.get_inv, r"get_inv@(.*?)")) TgBot.app.add_handler(CallbackQueryHandler(self.add_tmc, r"add_tmc@(.*?)")) @@ -250,38 +253,45 @@ class TgBot: await self.inv(update, context) - async def ter_back(self, update: Update, context: CallbackContext): + def set_step(self, update: Update, context: CallbackContext, prefix="ter"): + context.chat_data["step"] = f"{prefix}" + context.chat_data[f"{prefix}_start"] = 0 + context.chat_data[f"{prefix}_count"] = 10 + context.chat_data[f"{prefix}_renew"] = False + + async def step_btn( + self, update: Update, context: CallbackContext, prefix="ter", step_type="plus" + ): query = update.callback_query await query.answer() - context.chat_data["ter_start"] -= context.chat_data["ter_count"] - context.chat_data["ter_renew"] = True - await self.ter(update, context) + step_start = context.chat_data[f"{prefix}_start"] + step_count = context.chat_data[f"{prefix}_count"] - async def ter_next(self, update: Update, context: CallbackContext): - query = update.callback_query - await query.answer() - context.chat_data["ter_start"] += context.chat_data["ter_count"] - context.chat_data["ter_renew"] = True - await self.ter(update, context) + context.chat_data[f"{prefix}_renew"] = True - async def ter(self, update: Update, context: CallbackContext): + if step_type == "plus": + context.chat_data[f"{prefix}_start"] += step_count + elif step_type == "minus": + context.chat_data[f"{prefix}_start"] -= step_count + + func = getattr(self, prefix) + await func(update, context) + + async def paged_data( + self, update: Update, context: CallbackContext, prefix="ter", queryset=[] + ): user = update.effective_user current_step = context.chat_data.get("step", None) logger.info(f"Step {current_step} from user {user.full_name}") - context.chat_data["step"] = "territory" - if "ter_start" not in context.chat_data: - context.chat_data["ter_start"] = 0 - if "ter_count" not in context.chat_data: - context.chat_data["ter_count"] = 10 locations = [] - async for e in Territory.objects.all(): + async for e in queryset: locations.append({"name": e.name, "id": e.id}) - ter_start = context.chat_data["ter_start"] - ter_step = context.chat_data["ter_count"] - ter_end = ter_start + ter_step + step_start = context.chat_data[f"{prefix}_start"] + step_step = context.chat_data[f"{prefix}_count"] + step_end = step_start + step_step text = "\n".join( [ @@ -289,23 +299,28 @@ class TgBot: "Выберите объект:", "\n".join( [ - f"/ter_{i['id']} {i['name']}" - for i in locations[ter_start:ter_end] + f"/{prefix}_{i['id']} {i['name']}" + for i in locations[step_start:step_end] ] ), ] ) keyboard = [] - logger.info((ter_start, ter_end)) - if ter_start > 0: - keyboard.append(InlineKeyboardButton(text="←", callback_data="ter_back")) - if ter_end < len(locations): - keyboard.append(InlineKeyboardButton(text="→", callback_data="ter_next")) + if step_start > 0: + keyboard.append( + InlineKeyboardButton(text="←", callback_data=f"{prefix}_back") + ) + if step_end < len(locations): + keyboard.append( + InlineKeyboardButton(text="→", callback_data=f"{prefix}_next") + ) keyboard = [keyboard] - if "ter_renew" in context.chat_data: + renew = context.chat_data.get(f"{prefix}_renew", None) + logger.info(update.message) + if renew: await update.effective_message.edit_text( text, reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboard), @@ -318,79 +333,39 @@ class TgBot: ), reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboard), ) + + async def ter_back(self, update: Update, context: CallbackContext): + await self.step_btn(update, context, "ter", "minus") + + async def ter_next(self, update: Update, context: CallbackContext): + await self.step_btn(update, context, "ter", "plus") + + async def ter(self, update: Update, context: CallbackContext): + if not update.callback_query: + self.set_step(update, context, "ter") + + await self.paged_data(update, context, "ter", Territory.objects.all()) + async def terdeep_back(self, update: Update, context: CallbackContext): - query = update.callback_query - await query.answer() - context.chat_data["terdeep_start"] -= context.chat_data["terdeep_count"] - context.chat_data["terdeep_renew"] = True - await self.ter(update, context) + await self.step_btn(update, context, "terdeep", "minus") async def terdeep_next(self, update: Update, context: CallbackContext): - query = update.callback_query - await query.answer() - context.chat_data["terdeep_start"] += context.chat_data["terdeep_count"] - context.chat_data["terdeep_renew"] = True - await self.ter(update, context) - - async def ter_deep(self, update: Update, context: CallbackContext): - user = update.effective_user - current_step = context.chat_data.get("step", None) + await self.step_btn(update, context, "terdeep", "plus") - logger.info(f"Step {current_step} from user {user.full_name}") - context.chat_data["step"] = "terdeep" - if "terdeep_start" not in context.chat_data: - context.chat_data["terdeep_start"] = 0 - if "terdeep_count" not in context.chat_data: - context.chat_data["terdeep_count"] = 10 + async def terdeep(self, update: Update, context: CallbackContext): + if not update.callback_query: + self.set_step(update, context, "terdeep") - parent = re.findall(r"/ter_(\d.*)", update.effective_message.text) - ter = await Territory.objects.aget(id=parent[0]) - locations = [] - async for e in TerritoryItem.objects.filter(parent=ter): - locations.append({"name": e.name, "id": e.id}) + parent = context.chat_data.get("terdeep", None) + if len(re.findall(r"/ter_(\d.*)", update.effective_message.text)): + parent = re.findall(r"/ter_(\d.*)", update.effective_message.text)[0] + context.chat_data["terdeep"] = parent + context.chat_data["terdeep_renew"] = False - ter_start = context.chat_data["terdeep_start"] - ter_step = context.chat_data["terdeep_count"] - ter_end = ter_start + ter_step - - text = "\n".join( - [ - "Выберите:", - "\n".join( - [ - f"/terdeep_{i['id']} {i['name']}" - for i in locations[ter_start:ter_end] - ] - ), - ] + await self.paged_data( + update, context, "terdeep", TerritoryItem.objects.filter(parent_id=parent) ) - keyboard = [] - if ter_start > 0: - keyboard.append( - InlineKeyboardButton(text="←", callback_data="terdeep_back") - ) - if ter_end < len(locations): - keyboard.append( - InlineKeyboardButton(text="→", callback_data="terdeep_next") - ) - - keyboard = [keyboard] - - if "terdeep_renew" in context.chat_data: - await update.effective_message.edit_text( - text, - reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboard), - ) - else: - await update.effective_message.reply_text( - text, - reply_parameters=ReplyParameters( - message_id=update.effective_message.message_id - ), - reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboard), - ) - async def inv(self, update: Update, context: CallbackContext): user = update.effective_user current_step = context.chat_data.get("step", None)