schmeckels/sort.py
2020-04-25 11:07:51 +02:00

49 lines
1.7 KiB
Python

#! /usr/bin/env python3
from models import Transaction
from helper import get_session, add_category, get_list_of_bookable_categories
import sys
import click
from prompt_toolkit.completion import FuzzyWordCompleter
from prompt_toolkit.shortcuts import prompt
@click.command(name="sort")
@click.option("--profile", "-p")
def command(profile):
session = get_session(profile)
categories = get_list_of_bookable_categories(session)
category_lookup = {c.full_name(): c.id for c in categories}
category_names = FuzzyWordCompleter([c.full_name() for c in categories])
unsorted = session.query(Transaction).filter(Transaction.category_id == None).all()
print("Found {} unsorted transcations".format(len(unsorted)))
for t in unsorted:
print(" Name: {}".format(t.name))
print(" VWZ: {}".format(t.description))
print("Betrag: {}".format(t.amount / 100))
try:
select = prompt("Category: ", completer=category_names, complete_while_typing=True)
except KeyboardInterrupt:
print("Goodbye")
sys.exit(0)
if select == "":
print("Skipping")
continue
cat_id = category_lookup.get(select, None)
if not cat_id:
print(f"Creating new category '{select}'")
cat_id = add_category(select, profile, session)
# Update the list of categories
categories = get_list_of_bookable_categories(session)
category_lookup = {c.full_name(): c.id for c in categories}
category_names = FuzzyWordCompleter([c.full_name() for c in categories])
t.category_id = cat_id
session.add(t)
session.commit()
print("-" * 20)