49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
#! /usr/bin/env python3
|
|
from models import Transaction, Tag
|
|
from helper import get_session, create_tag
|
|
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)
|
|
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(" Name: {}".format(t.name))
|
|
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, profile, 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)
|