Compare commits

..

No commits in common. "5d59adb7d286c4b057273794e4710df8a828b321" and "74844da4aeb4d4cdc214ac6fe7a20eb416e337ac" have entirely different histories.

4 changed files with 22 additions and 21 deletions

View File

@ -1,7 +1,5 @@
import asyncio
import functools
import gc
from concurrent.futures import ProcessPoolExecutor
import telebot
from sqlalchemy.future import Engine
@ -307,7 +305,7 @@ async def _add_account5(
password=data["passwd"],
)
encrypted_account = encryption.accounts.encrypt(
encrypted_account = encryption.other_accounts.encrypt(
account,
text,
)
@ -393,7 +391,7 @@ async def _get_account3(
)
account = database.get.get_account_info(engine, mes.from_user.id, name)
account = encryption.accounts.decrypt(
account = encryption.other_accounts.decrypt(
account,
text,
)
@ -536,18 +534,7 @@ async def _export2(
)
accounts = database.get.get_all_accounts(engine, mes.from_user.id)
with ProcessPoolExecutor() as pool:
loop = asyncio.get_running_loop()
tasks = []
for account in accounts:
function = functools.partial(
encryption.accounts.decrypt,
account,
text,
)
tasks.append(loop.run_in_executor(pool, function))
accounts = await asyncio.gather(*tasks)
accounts.sort(key=lambda account: account.name)
accounts = encryption.other_accounts.decrypt_multiple(accounts, text)
json_io = accounts_to_json(accounts)
await bot.send_document(
mes.chat.id,
@ -651,7 +638,7 @@ async def _import3(
if not check_account(account):
failed.append(account.name)
continue
account = encryption.accounts.encrypt(
account = encryption.other_accounts.encrypt(
account,
text,
)

View File

@ -36,9 +36,13 @@ def get_accounts(
def get_all_accounts(engine: Engine, user_id: int) -> list[models.Account]:
"""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,
)
.order_by(models.Account.name)
)
with sqlmodel.Session(engine) as session:
result = session.exec(statement).fetchall()
return result

View File

@ -1,3 +1,3 @@
from . import accounts, master_pass
from . import master_pass, other_accounts
__all__ = ["master_pass", "accounts"]
__all__ = ["master_pass", "other_accounts"]

View File

@ -1,5 +1,6 @@
import base64
import os
from typing import Iterable, Iterator
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
@ -70,3 +71,12 @@ def decrypt(
login=login,
password=password,
)
def decrypt_multiple(
accounts: Iterable[Account], master_pass: str
) -> Iterator[DecryptedAccount]:
"""Decrypts an iterable of accounts using master_pass and
returns an Iterator of decrypted accounts"""
for account in accounts:
yield decrypt(account, master_pass)