async def find_from_search_bot(message: types.Message, state: FSMContext): name = re.match('^<b>(.+)</b>', message.html_text).group(1) res = botutils.get_from_wiki(name=name, ret_fields=["name"])["name"] await EditProcess.name.set() async with state.proxy() as data: data['names'] = {word['_id'] : word['name'] for word in res} await message.answer("Which word do you want to change?", reply_markup=botutils.get_replymarkup_names(res))
async def find_by_id(message: types.Message, state: FSMContext): _id = re.match("^/?[i|I][d|D]\s*:?\s*([0-9a-z]{24})", message.text).group(1) res = botutils.get_from_wiki(id=_id, ret_fields=["name"])["_id"] if res: async with state.proxy() as idp: idp["_id"] = res["_id"] await EditProcess._id.set() await message.answer(f"Found : {res['name']}") await message.answer("Are you sure you want to edit this one?", reply_markup=botutils.get_replymarkup_yesno())
async def find_by_name(message: types.Message, state: FSMContext): name = re.match("^/?[n|N]ame\s*:?\s*(.+)", message.text).group(1) res = botutils.get_from_wiki(name=name, ret_fields=["name"])["name"] if len(res) == 0: await message.answer(f"Nothing found for {name}") return await EditProcess.name.set() async with state.proxy() as data: data['names'] = {word['_id'] : word['name'] for word in res} await message.answer("Which word do you want to change? There are last 5 symbols of id in brackets.", reply_markup=botutils.get_replymarkup_names(res))
async def edit_or_delete(message: types.Message, state: FSMContext): async with state.proxy() as data: data["word_info"] = botutils.get_from_wiki(id=data["_id"])["_id"] word_info = data["word_info"] if message.text == 'Edit': logging.debug(f"User {message.from_user.id} is edititng {data['_id']}") del word_info["_id"] await EditProcess.old_field.set() await message.answer("What field do you want to change? (or create a new one)", reply_markup=botutils.get_replymarkup_fields(word_info)) elif message.text == 'Delete': await EditProcess.delete_confirmation.set() await message.answer('Are you sure you want to delete this object?', reply_markup=botutils.get_replymarkup_yesno())
async def process_id(message: types.Message, state: FSMContext): async with state.proxy() as data: if message.text == 'Yes': await EditProcess.old_field.set() logging.debug( f"User {message.from_user.id} is edititng {data['_id']}") word_info = botutils.get_from_wiki(id=data['_id'])["_id"] del word_info["_id"] await message.answer( "What field you wish to change?", reply_markup=botutils.get_replymarkup_fields(word_info)) elif message.text == 'No': await state.finish() await message.answer('Ok', reply_markup=types.ReplyKeyboardRemove())
async def process_name(message: types.Message, state: FSMContext): async with state.proxy() as data: matches = re.match("(.*) \((.+)\)", message.text) if matches is None: return name, _id = matches.groups() for word_id, word_name in data['names'].items(): if word_name == name and str(word_id)[-5:] == _id: data["_id"] = word_id data["word_info"] = botutils.get_from_wiki(id=word_id)["_id"] word_info = data["word_info"] break await EditProcess._id.set() await message.answer(botutils.format_page_info(word_info)) await message.answer("Are you sure you want to edit this one?", reply_markup=botutils.get_replymarkup_yesno())