generated from felixm/defaultpy
27 lines
925 B
Python
27 lines
925 B
Python
from src.models import Transaction, UNKNOWN_CATEGORY
|
|
from src.fzf import iterfzf
|
|
from typing import List
|
|
|
|
|
|
def get_categories(transactions: List[Transaction]) -> List[str]:
|
|
categories = set([t.account2 for t in transactions])
|
|
categories.discard(UNKNOWN_CATEGORY)
|
|
return list(categories)
|
|
|
|
|
|
def add_account2(transactions: List[Transaction]):
|
|
categories = get_categories(transactions)
|
|
unmapped_transactions = filter(lambda t: t.account2 == UNKNOWN_CATEGORY, transactions)
|
|
for t in unmapped_transactions:
|
|
add_account2_interactive(t, categories)
|
|
|
|
|
|
def add_account2_interactive(transaction: Transaction, categories: List[str]):
|
|
t = transaction
|
|
account2 = None
|
|
prompt = f"{t.account1} {t.date} {t.description} {t.debit} > "
|
|
while account2 is None:
|
|
account2 = iterfzf(categories, prompt=prompt)
|
|
transaction.account2 = account2
|
|
print(f"Assigned category '{account2}'.")
|