ter and terdeep command
This commit is contained in:
parent
6a4333e7c1
commit
92a1b96e37
|
@ -97,17 +97,20 @@ class TgBot:
|
||||||
self.inv,
|
self.inv,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
TgBot.app.add_handler(CommandHandler("ter", self.ter, filters.ChatType.PRIVATE))
|
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(
|
TgBot.app.add_handler(
|
||||||
MessageHandler(
|
MessageHandler(
|
||||||
filters.ChatType.PRIVATE & filters.Regex(r"/ter_(\d.*)"),
|
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_back, "terdeep_back"))
|
||||||
TgBot.app.add_handler(CallbackQueryHandler(self.terdeep_next, "terdeep_next"))
|
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.stop_inv, "stop_inv"))
|
||||||
TgBot.app.add_handler(CallbackQueryHandler(self.get_inv, r"get_inv@(.*?)"))
|
TgBot.app.add_handler(CallbackQueryHandler(self.get_inv, r"get_inv@(.*?)"))
|
||||||
TgBot.app.add_handler(CallbackQueryHandler(self.add_tmc, r"add_tmc@(.*?)"))
|
TgBot.app.add_handler(CallbackQueryHandler(self.add_tmc, r"add_tmc@(.*?)"))
|
||||||
|
@ -250,38 +253,45 @@ class TgBot:
|
||||||
|
|
||||||
await self.inv(update, context)
|
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
|
query = update.callback_query
|
||||||
await query.answer()
|
await query.answer()
|
||||||
context.chat_data["ter_start"] -= context.chat_data["ter_count"]
|
step_start = context.chat_data[f"{prefix}_start"]
|
||||||
context.chat_data["ter_renew"] = True
|
step_count = context.chat_data[f"{prefix}_count"]
|
||||||
await self.ter(update, context)
|
|
||||||
|
|
||||||
async def ter_next(self, update: Update, context: CallbackContext):
|
context.chat_data[f"{prefix}_renew"] = True
|
||||||
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)
|
|
||||||
|
|
||||||
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
|
user = update.effective_user
|
||||||
current_step = context.chat_data.get("step", None)
|
current_step = context.chat_data.get("step", None)
|
||||||
|
|
||||||
logger.info(f"Step {current_step} from user {user.full_name}")
|
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 = []
|
locations = []
|
||||||
async for e in Territory.objects.all():
|
async for e in queryset:
|
||||||
locations.append({"name": e.name, "id": e.id})
|
locations.append({"name": e.name, "id": e.id})
|
||||||
|
|
||||||
ter_start = context.chat_data["ter_start"]
|
step_start = context.chat_data[f"{prefix}_start"]
|
||||||
ter_step = context.chat_data["ter_count"]
|
step_step = context.chat_data[f"{prefix}_count"]
|
||||||
ter_end = ter_start + ter_step
|
step_end = step_start + step_step
|
||||||
|
|
||||||
text = "\n".join(
|
text = "\n".join(
|
||||||
[
|
[
|
||||||
|
@ -289,23 +299,28 @@ class TgBot:
|
||||||
"Выберите объект:",
|
"Выберите объект:",
|
||||||
"\n".join(
|
"\n".join(
|
||||||
[
|
[
|
||||||
f"/ter_{i['id']} {i['name']}"
|
f"/{prefix}_{i['id']} {i['name']}"
|
||||||
for i in locations[ter_start:ter_end]
|
for i in locations[step_start:step_end]
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
keyboard = []
|
keyboard = []
|
||||||
logger.info((ter_start, ter_end))
|
if step_start > 0:
|
||||||
if ter_start > 0:
|
keyboard.append(
|
||||||
keyboard.append(InlineKeyboardButton(text="←", callback_data="ter_back"))
|
InlineKeyboardButton(text="←", callback_data=f"{prefix}_back")
|
||||||
if ter_end < len(locations):
|
)
|
||||||
keyboard.append(InlineKeyboardButton(text="→", callback_data="ter_next"))
|
if step_end < len(locations):
|
||||||
|
keyboard.append(
|
||||||
|
InlineKeyboardButton(text="→", callback_data=f"{prefix}_next")
|
||||||
|
)
|
||||||
|
|
||||||
keyboard = [keyboard]
|
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(
|
await update.effective_message.edit_text(
|
||||||
text,
|
text,
|
||||||
reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboard),
|
reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboard),
|
||||||
|
@ -318,79 +333,39 @@ class TgBot:
|
||||||
),
|
),
|
||||||
reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboard),
|
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):
|
async def terdeep_back(self, update: Update, context: CallbackContext):
|
||||||
query = update.callback_query
|
await self.step_btn(update, context, "terdeep", "minus")
|
||||||
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 terdeep_next(self, update: Update, context: CallbackContext):
|
async def terdeep_next(self, update: Update, context: CallbackContext):
|
||||||
query = update.callback_query
|
await self.step_btn(update, context, "terdeep", "plus")
|
||||||
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):
|
async def terdeep(self, update: Update, context: CallbackContext):
|
||||||
user = update.effective_user
|
if not update.callback_query:
|
||||||
current_step = context.chat_data.get("step", None)
|
self.set_step(update, context, "terdeep")
|
||||||
|
|
||||||
logger.info(f"Step {current_step} from user {user.full_name}")
|
parent = context.chat_data.get("terdeep", None)
|
||||||
context.chat_data["step"] = "terdeep"
|
if len(re.findall(r"/ter_(\d.*)", update.effective_message.text)):
|
||||||
if "terdeep_start" not in context.chat_data:
|
parent = re.findall(r"/ter_(\d.*)", update.effective_message.text)[0]
|
||||||
context.chat_data["terdeep_start"] = 0
|
context.chat_data["terdeep"] = parent
|
||||||
if "terdeep_count" not in context.chat_data:
|
context.chat_data["terdeep_renew"] = False
|
||||||
context.chat_data["terdeep_count"] = 10
|
|
||||||
|
|
||||||
parent = re.findall(r"/ter_(\d.*)", update.effective_message.text)
|
await self.paged_data(
|
||||||
ter = await Territory.objects.aget(id=parent[0])
|
update, context, "terdeep", TerritoryItem.objects.filter(parent_id=parent)
|
||||||
locations = []
|
|
||||||
async for e in TerritoryItem.objects.filter(parent=ter):
|
|
||||||
locations.append({"name": e.name, "id": e.id})
|
|
||||||
|
|
||||||
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]
|
|
||||||
]
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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):
|
async def inv(self, update: Update, context: CallbackContext):
|
||||||
user = update.effective_user
|
user = update.effective_user
|
||||||
current_step = context.chat_data.get("step", None)
|
current_step = context.chat_data.get("step", None)
|
||||||
|
|
Loading…
Reference in New Issue