From c1aa007b1a38467d06a22d56374d19b7c635aa9b Mon Sep 17 00:00:00 2001 From: Kseninia Mikhaylova Date: Fri, 19 Jul 2024 16:54:50 +0300 Subject: [PATCH] add part of territories --- back/tgbot/tgbot.py | 152 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 144 insertions(+), 8 deletions(-) diff --git a/back/tgbot/tgbot.py b/back/tgbot/tgbot.py index 75b74a6..2d6c61c 100644 --- a/back/tgbot/tgbot.py +++ b/back/tgbot/tgbot.py @@ -31,7 +31,7 @@ from django.db import models from .apps import TgBotUpdater from .models import TgItem, TmcElement, TmcField -from tmc.models import CustomTable, BaseCustomField +from tmc.models import CustomTable, BaseCustomField, Territory, TerritoryItem logger = logging.getLogger(__name__) @@ -84,6 +84,7 @@ class TgBot: TgBot.app.add_handler( CommandHandler("start", self.start, filters.ChatType.PRIVATE) ) + TgBot.app.add_handler(CommandHandler("my", self.my, filters.ChatType.PRIVATE)) TgBot.app.add_handler(CommandHandler("inv", self.inv, filters.ChatType.PRIVATE)) TgBot.app.add_handler( @@ -96,6 +97,15 @@ class TgBot: self.inv, ) ) + TgBot.app.add_handler( + MessageHandler( + filters.ChatType.PRIVATE & filters.Regex(r"/ter_(\d.*)"), + self.ter_deep, + ) + ) + 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(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@(.*?)")) @@ -110,6 +120,7 @@ class TgBot: "Это бот для проведения инвентаризации\n" "/my \-\- продолжить инвентаризацию\n" "/inv \-\- начать новую инвентаризацию" + "/ter \-\- список территорий" ), # reply_markup=ForceReply(selective=True), reply_parameters=ReplyParameters(message_id=update.message.message_id), @@ -178,6 +189,20 @@ class TgBot: ) ] + async def ter_back(self, update: Update, context: CallbackContext): + query = update.callback_query + await query.answer() + context.chat_data["territory_start"] -= context.chat_data["territory_count"] + context.chat_data["territory_renew"] = True + await self.inv(update, context) + + async def ter_next(self, update: Update, context: CallbackContext): + query = update.callback_query + await query.answer() + context.chat_data["territory_start"] += context.chat_data["territory_count"] + context.chat_data["territory_renew"] = True + await self.inv(update, context) + async def stop_inv(self, update: Update, context: CallbackContext): query = update.callback_query await update.effective_message.edit_reply_markup(InlineKeyboardMarkup([])) @@ -237,13 +262,122 @@ class TgBot: await self.inv(update, context) + async def ter(self, update: Update, context: CallbackContext): + 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 "territory_start" not in context.chat_data: + context.chat_data["territory_start"] = 10 + if "territory_count" not in context.chat_data: + context.chat_data["territory_count"] = 10 + + locations = [] + async for e in Territory.objects.all(): + locations.append({"name": e.name, "id": e.id}) + + ter_start = context.chat_data["territory_start"] + ter_step = context.chat_data["territory_count"] + ter_end = ter_start + ter_step + + text = "\n".join( + [ + self.format_username(user), + "Выберите объект:", + "\n".join( + [ + f"/ter_{i['id']} {i['name']}" + for i in locations[ter_start:ter_end] + ] + ), + ] + ) + + keyboard = [] + 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")) + + keyboard = [keyboard] + + if "territory_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 ter_deep(self, update: Update, context: CallbackContext): + 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 "territory_start" not in context.chat_data: + # context.chat_data["territory_start"] = 10 + # if "territory_count" not in context.chat_data: + # context.chat_data["territory_count"] = 10 + + 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}) + logger.info(parent[0]) + # ter_start = context.chat_data["territory_start"] + # ter_step = context.chat_data["territory_count"] + ter_start = 0 + ter_step = 10 + ter_end = ter_start + ter_step + + text = "\n".join( + [ + "Выберите:", + "\n".join( + [ + f"/ter_{i['id']} {i['name']}" + for i in locations[ter_start:ter_end] + ] + ), + ] + ) + + keyboard = [] + 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")) + + keyboard = [keyboard] + + if "territory_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) - logger.info( - f"Step {current_step} from user {user.full_name} in {update.message}" - ) + logger.info(f"Step {current_step} from user {user.full_name}") if update.effective_message.text == "/inv": context.chat_data["step"] = "name" @@ -254,9 +388,10 @@ class TgBot: ), ) - elif current_step == "name": + if current_step == "name": if not context.chat_data.get("inv", None): - inv = await TgItem.objects.acreate(user_id=user.id) + loc = await TerritoryItem.objects.aget(id=35) + inv = await TgItem.objects.acreate(user_id=user.id, location=loc) inv.name = update.message.text await inv.asave() else: @@ -363,6 +498,7 @@ class TgBot: ), reply_markup=ReplyKeyboardRemove(), ) + elif current_step == "add_element_data": inv = await TgItem.objects.aget(id=context.chat_data["inv"]) tmc_element = await inv.tmc.aget(id=context.chat_data["tmc"]) @@ -377,7 +513,7 @@ class TgBot: await element.asave() await inv.asave() - + await update.effective_message.reply_markdown_v2( "Изображение сохранено" if update.message.photo else "Текст сохранен" ) @@ -393,7 +529,7 @@ class TgBot: await self.inv(update, context) else: - logger.info(update.message.entities) + # logger.info(update.message.entities) logger.info(f"no step for update {update}") if "step" in context.chat_data and context.chat_data["step"] == current_step: