52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
#! /usr/bin/env python3
|
|
import sys
|
|
|
|
import click
|
|
from prompt_toolkit.completion import FuzzyWordCompleter
|
|
from prompt_toolkit.shortcuts import prompt
|
|
|
|
from schmeckels.helper import create_tag, get_session
|
|
from schmeckels.models import Tag, Transaction
|
|
|
|
|
|
@click.command(name="sort")
|
|
def command():
|
|
session = get_session()
|
|
tags = session.query(Tag).all()
|
|
tag_names = FuzzyWordCompleter([x.name for x in tags])
|
|
tag_lookup = {tag.name: tag for tag in tags}
|
|
|
|
unsorted = session.query(Transaction).filter(Transaction.tags == None).all()
|
|
print("Found {} unsorted transcations".format(len(unsorted)))
|
|
|
|
for t in unsorted:
|
|
print("-"*20)
|
|
print(" Name: {}".format(t.name))
|
|
print(" IBAN: {}".format(t.iban))
|
|
print(" Datum: {}".format(t.date))
|
|
print(" VWZ: {}".format(t.description))
|
|
print("Betrag: {}€".format(t.amount / 100))
|
|
try:
|
|
select = prompt("Tag: ", completer=tag_names, complete_while_typing=True)
|
|
except KeyboardInterrupt:
|
|
print("Goodbye")
|
|
sys.exit(0)
|
|
|
|
if select == "":
|
|
print("Skipping")
|
|
continue
|
|
|
|
tag = tag_lookup.get(select, None)
|
|
if not tag:
|
|
print(f"Creating new category '{select}'")
|
|
tag = create_tag(select, session)
|
|
|
|
tags = session.query(Tag).all()
|
|
tag_names = FuzzyWordCompleter([x.name for x in tags])
|
|
tag_lookup = {tag.name: tag for tag in tags}
|
|
|
|
t.tags.append(tag)
|
|
session.add(t)
|
|
session.commit()
|
|
|
|
print("-" * 20)
|