def retrieve_accounts(bank_name): w = Weboob() w.load_backends(caps=(CapBank, )) bank_backend = w.get_backend(bank_name) if bank_backend: bank = Bank.get_or_create(name=bank_name) accounts = bank_backend.iter_accounts() for account in accounts: logger.info( f'[Accounts] Retrieving account {account.label} - {account.balance} from {bank_name}' ) db_account = Account.get_or_none(bank=bank[0], account_id=account.id) if db_account is None: db_account = Account.create(bank=bank[0], account_id=account.id, user=1, label=account.label, balance=account.balance) else: db_account.label = account.label db_account.balance = account.balance db_account.save() transactions = bank_backend.iter_history(account) for transaction in transactions: db_transanction = Transaction.get_or_create( account=db_account.id, label=transaction.label, category=transaction.category, amount=transaction.amount, date=transaction.date)
def search_account(account_id, bank=None): account = Account.select().where(Account.label.contains(account_id)) if bank is not None: bank = Bank.get_or_none(name=bank) account = account.where(Account.bank == bank) return account
def get_account_by_balance(number, bank=None): accounts = Account.select().where(Account.balance <= number) if bank is not None: bank = Bank.get_or_none(name=bank) if bank is not None: accounts = accounts.where(Account.bank == bank) return accounts
def get_accounts(bank=None): accounts = Account.select() if bank is not None: db_bank = Bank.get_or_none(name=bank) if db_bank is None: raise BankNotFoundError(bank) accounts = accounts.where(Account.bank == db_bank) return accounts
def get_bank_accounts(bank_name=None): banks = get_banks(bank_name) if banks: if bank_name is not None: account = Account.select().where(Account.bank.name == bank_name) return account.get_small_data() else: accounts = [] for bank in banks: this_bank = get_banks(bank["name"])[0] account_selected = Account.select().where( Account.bank == this_bank["id"]) print(account_selected) p = { bank["name"]: [account.get_small_data() for account in account_selected] } accounts.append(p) return accounts
def search_transaction(transaction_amount, bank=None, account_id=None): transaction = Transaction.select().where( Transaction.amount.contains(transaction_amount)) # account = Account.select().where(Account.label.contains(account_id)) # if bank is not None: # bank = Bank.get_or_none(name=bank) # account = account.where(Account.bank == bank) if account_id is not None: account_id = Account.get_or_none(name=account_id) transaction = transaction.where(Transaction.account == account_id) return transaction
def get_accounts_types(bank=None, db_user=None): accounts_types = Account.select(fn.Distinct(Account.label.strip(Account.account_id))) if db_user is not None: accounts_types = accounts_types.where(Account.user == db_user) if bank is not None: db_bank = Bank.get_or_none(name=bank) if db_bank is None: raise BankNotFoundError(bank) accounts_types = accounts_types.where(Account.bank == db_bank) return [account.strip("()") for account in accounts_types]
def get_accounts(bank=None, name=None, db_user=None): accounts = Account.select() if name is not None: accounts = accounts.where(Account.label.contains(name)) if db_user is not None: accounts = accounts.where(Account.user == db_user) if bank is not None: db_bank = Bank.get_or_none(name=bank) if db_bank is None: raise BankNotFoundError(bank) accounts = accounts.where(Account.bank == db_bank) return accounts
def sum_of_balances(user_id): sums_of_balances = Account.select( fn.SUM(Account.balance).alias('sum_of_balances')).where( Account.user == user_id) for i in sums_of_balances: return (i.sum_of_balances)
def retrieve_accounts(): w = Weboob() w.load_backends(caps=(CapBank, )) for i in w.load_backends(CapBank).keys(): Bank.get_or_create(name=i) user1 = User.get_or_none(username='******', password='******') user2 = User.get_or_none(username='******', password='******') if user1 is None: user1 = User.create(username='******', password='******') if user2 is None: user2 = User.create(username='******', password='******') for bank in Bank.select(): bank_backend = w.get_backend(bank.name) accounts = bank_backend.iter_accounts() for account in accounts: logger.info( f'[Accounts] Retrieving account {account.id} - {account.label} - {account.balance} from {bank.name}' ) db_account = Account.get_or_none(bank=bank, account_id=account.id) if db_account is None: if Account.select().where(Account.bank == 3): db_account = Account.create(bank=bank, account_id=account.id, user=user2.id, label=account.label, balance=account.balance) else: db_account = Account.create(bank=bank, account_id=account.id, user=user1.id, label=account.label, balance=account.balance) else: db_account.label = account.label db_account.balance = account.balance db_account.save() transactions = bank_backend.iter_history(bank) for transaction in transactions: logger.info( f'[Transactions] Retrieving account {transaction.label} - {transaction.amount} from {bank.name}' ) db_transaction = Transaction.get_or_none( account=db_account, label=transaction.label) if db_transaction is None: db_transaction = Transaction.create( account=db_account, label=transaction.label, amount=transaction.amount, vu=False) else: db_transaction.label = transaction.label db_transaction.amount = transaction.amount db_transaction.save()