5 Commits

Author SHA1 Message Date
c7675c231f Fixed sending a final message in /import 2023-01-03 21:07:37 +03:00
ae88fccf13 Updated type hint of the handler in the register_state function 2023-01-03 12:34:54 +03:00
e29eefe40b Changes in helper_functions.delete_message
Removed checking if sleep_time is 0
Moved sleeping outside of try block
2023-01-03 12:29:08 +03:00
fdbed91512 Now account name is copyable 2023-01-03 12:12:40 +03:00
f9d163361a Renamed functions in db.get
get_accounts -> get_account_names
get_all_accounts -> get_accounts
2023-01-03 12:08:06 +03:00
3 changed files with 18 additions and 26 deletions

View File

@ -18,7 +18,7 @@ states: dict[tuple[int, int], Handler] = {}
def register_state( def register_state(
message: Message, message: Message,
handler: Callable[[Message], Any], handler: Handler,
) -> None: ) -> None:
states[(message.chat.id, message.from_user.id)] = handler states[(message.chat.id, message.from_user.id)] = handler
@ -40,9 +40,8 @@ async def delete_message(
*, *,
sleep_time: int = 0, sleep_time: int = 0,
) -> bool: ) -> bool:
await asyncio.sleep(sleep_time)
try: try:
if sleep_time != 0:
await asyncio.sleep(sleep_time)
await bot.delete_message(mes.chat.id, mes.id) await bot.delete_message(mes.chat.id, mes.id)
except telebot.apihelper.ApiException: except telebot.apihelper.ApiException:
return False return False
@ -86,10 +85,3 @@ async def send_deleteable_message(
parse_mode="MarkdownV2", parse_mode="MarkdownV2",
reply_markup=markup, reply_markup=markup,
) )
def escape(text: str) -> str:
escaped_chars = "*_~|`[("
for char in escaped_chars:
text = text.replace(char, rf"\\{char}")
return text

View File

@ -20,7 +20,6 @@ from . import markups
from .helper_functions import ( from .helper_functions import (
base_handler, base_handler,
delete_message, delete_message,
escape,
get_state, get_state,
register_state, register_state,
send_deleteable_message, send_deleteable_message,
@ -36,7 +35,7 @@ async def get_accounts(
mes: Message, mes: Message,
) -> None: ) -> None:
await base_handler(bot, mes) await base_handler(bot, mes)
accounts = db.get.get_accounts( accounts = db.get.get_account_names(
engine, engine,
mes.from_user.id, mes.from_user.id,
to_sort=True, to_sort=True,
@ -210,7 +209,7 @@ async def _add_account2(
mes.chat.id, mes.chat.id,
"Не корректное название аккаунта", "Не корректное название аккаунта",
) )
if text in db.get.get_accounts(engine, mes.from_user.id): if text in db.get.get_account_names(engine, mes.from_user.id):
return await send_tmp_message( return await send_tmp_message(
bot, mes.chat.id, "Аккаунт с таким именем уже существует" bot, mes.chat.id, "Аккаунт с таким именем уже существует"
) )
@ -332,7 +331,7 @@ async def get_account(bot: AsyncTeleBot, engine: Engine, mes: Message) -> None:
if master_pass is None: if master_pass is None:
return await send_tmp_message(bot, mes.chat.id, "Нет мастер пароля") return await send_tmp_message(bot, mes.chat.id, "Нет мастер пароля")
accounts = db.get.get_accounts( accounts = db.get.get_account_names(
engine, engine,
mes.from_user.id, mes.from_user.id,
to_sort=True, to_sort=True,
@ -355,7 +354,7 @@ async def _get_account2(
if text == "/cancel": if text == "/cancel":
return await send_tmp_message(bot, mes.chat.id, "Успешная отмена") return await send_tmp_message(bot, mes.chat.id, "Успешная отмена")
if text not in db.get.get_accounts(engine, mes.from_user.id): if text not in db.get.get_account_names(engine, mes.from_user.id):
return await send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта") return await send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
bot_mes = await bot.send_message(mes.chat.id, "Отправьте мастер пароль") bot_mes = await bot.send_message(mes.chat.id, "Отправьте мастер пароль")
@ -394,9 +393,9 @@ async def _get_account3(
await send_deleteable_message( await send_deleteable_message(
bot, bot,
mes.chat.id, mes.chat.id,
f"Название:\n{escape(account.name)}\n" f"Название:\n`{account.name}`\n"
f"Логин:\n`{account.login}`\nПароль:\n`{account.password}`\nНажмите " f"Логин:\n`{account.login}`\nПароль:\n`{account.password}`\nНажмите "
"на логин или пароль, чтобы скопировать", "на название, логин или пароль, чтобы скопировать",
) )
del text, mes del text, mes
@ -414,7 +413,7 @@ async def delete_account(
if master_pass is None: if master_pass is None:
return await send_tmp_message(bot, mes.chat.id, "Нет мастер пароля") return await send_tmp_message(bot, mes.chat.id, "Нет мастер пароля")
accounts = db.get.get_accounts( accounts = db.get.get_account_names(
engine, engine,
mes.from_user.id, mes.from_user.id,
to_sort=True, to_sort=True,
@ -443,7 +442,7 @@ async def _delete_account2(
if text == "/cancel": if text == "/cancel":
return await send_tmp_message(bot, mes.chat.id, "Успешная отмена") return await send_tmp_message(bot, mes.chat.id, "Успешная отмена")
if text not in db.get.get_accounts(engine, mes.from_user.id): if text not in db.get.get_account_names(engine, mes.from_user.id):
return await send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта") return await send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
bot_mes = await bot.send_message( bot_mes = await bot.send_message(
@ -502,7 +501,7 @@ async def export(bot: AsyncTeleBot, engine: Engine, mes: Message) -> None:
if master_password_from_db is None: if master_password_from_db is None:
return await send_tmp_message(bot, mes.chat.id, "Нет мастер пароля") return await send_tmp_message(bot, mes.chat.id, "Нет мастер пароля")
if not db.get.get_accounts(engine, mes.from_user.id): if not db.get.get_account_names(engine, mes.from_user.id):
return await send_tmp_message(bot, mes.chat.id, "Нет аккаунтов") return await send_tmp_message(bot, mes.chat.id, "Нет аккаунтов")
bot_mes = await bot.send_message(mes.chat.id, "Отправьте мастер пароль") bot_mes = await bot.send_message(mes.chat.id, "Отправьте мастер пароль")
@ -529,7 +528,7 @@ async def _export2(
"Не подходит мастер пароль", "Не подходит мастер пароль",
) )
accounts = db.get.get_all_accounts(engine, mes.from_user.id) accounts = db.get.get_accounts(engine, mes.from_user.id)
with ProcessPoolExecutor() as pool: with ProcessPoolExecutor() as pool:
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
tasks = [] tasks = []
@ -651,11 +650,12 @@ async def _import3(
failed.append(account.name) failed.append(account.name)
if failed: if failed:
mes_text = "Не удалось добавить:\n" + "\n".join(failed) await send_deleteable_message(
bot, mes.chat.id, "Не удалось добавить:\n" + "\n".join(failed)
)
else: else:
mes_text = "Успех" await send_tmp_message(bot, mes.chat.id, "Успех")
await send_tmp_message(bot, mes.chat.id, mes_text, 10)
del text, mes, accounts del text, mes, accounts
gc.collect() gc.collect()

View File

@ -17,7 +17,7 @@ def get_master_pass(
return result return result
def get_accounts( def get_account_names(
engine: Engine, engine: Engine,
user_id: int, user_id: int,
*, *,
@ -34,7 +34,7 @@ def get_accounts(
return result return result
def get_all_accounts(engine: Engine, user_id: int) -> list[models.Account]: def get_accounts(engine: Engine, user_id: int) -> list[models.Account]:
"""Returns a list of accounts of a user""" """Returns a list of accounts of a user"""
statement = sqlmodel.select(models.Account).where( statement = sqlmodel.select(models.Account).where(
models.Account.user_id == user_id, models.Account.user_id == user_id,