def get_user_transactions(user_name, bank=None, account=None, limits=None): db_user = User.get_or_none(username=user_name) if db_user is None: raise UserNotFoundError(user_name) else: transactions = (Transaction.select() .join(Account) .join(User, on=(User.id == Account.user_id)) .where(User.username == user_name) .order_by(Transaction.date)) if bank is not None: db_bank = Bank.get_or_none(name=bank) if db_bank is None: raise BankNotFoundError(bank) else: transactions = transactions.where(Account.bank.id == db_bank.id) if account is not None: db_account = Account.select().where(Account.label.contains(account)) if len(db_account) == 0: raise AccountNotFoundError else: transactions = transactions.where(Account.label.contains(account)) if limits is not None: transactions = transactions.limit(limits) return [transaction.get_small_data() for transaction in transactions]
def get_banks(bank_name=None): banks = Bank.select() if bank_name is not None: db_bank = Bank.get_or_none(name=bank_name) if db_bank is None: raise BankNotFoundError(bank_name) else: return [db_bank.get_small_data()] return [bank.get_small_data() for bank in banks]
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_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_user_accounts(user_name, bank=None): db_user = User.get_or_none(username=user_name) if db_user is None: raise UserNotFoundError(user_name) else: accounts = Account.select().where(Account.user == db_user.id) print(accounts) if bank is not None: db_bank = Bank.get_or_none(name=bank) if db_bank is None: raise BankNotFoundError(bank) else: accounts = accounts.where(Account.bank == db_bank.id) return [account.get_small_data() for account in accounts]
def get_transactions_by_account_type(account_type, bank=None, db_user=None): transactions = (Transaction.select() .join(Account) .where(Account.label.contains(account_type))) if db_user is not None: transactions = transactions.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) transactions = transactions.where(Account.bank == db_bank) return transactions
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 get_bank_users(bank_name=None): nb_users = (User.select(fn.count(fn.Distinct(User.id)), Bank.name).join( Account, on=(User.id == Account.user_id)).join(Bank).group_by(Bank)) if bank_name is not None: db_bank = Bank.get_or_none(name=bank_name) if db_bank is None: raise BankNotFoundError(bank_name) else: nb_users = nb_users.where(Bank.name == bank_name) print(nb_users) return [{ "name": nb_user.account.bank.name, "number of users": nb_user.count } for nb_user in nb_users]
def get_bank_transactions(bank_name=None): transactions = (Transaction.select(fn.COUNT(fn.Distinct( Transaction.id)), fn.sum(Transaction.amount), Bank.name).join( Account, on=(Transaction.account_id == Account.id)).join(Bank).group_by( Bank.name)) if bank_name is not None: db_bank = Bank.get_or_none(name=bank_name) if db_bank is None: raise BankNotFoundError(bank_name) else: transactions = transactions.where(Bank.name == bank_name) return [{ "name": transaction.account.bank.name, "number of transactions": transaction.count, "total amount": transaction.sum } for transaction in transactions]