44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
#! /usr/bin/env python3
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from models import *
|
|
from categories import add_category
|
|
import sys
|
|
|
|
from prompt_toolkit.completion import FuzzyWordCompleter
|
|
from prompt_toolkit.shortcuts import prompt
|
|
|
|
engine = create_engine("sqlite:///app.db")
|
|
Base = declarative_base()
|
|
|
|
Session = sessionmaker(bind=engine)
|
|
session = Session()
|
|
|
|
categories = session.query(Category).all()
|
|
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)
|
|
cat_id = category_lookup.get(select, None)
|
|
if cat_id:
|
|
t.category_id = cat_id
|
|
session.add(t)
|
|
session.commit()
|
|
else:
|
|
print(f"Creating new category '{select}'")
|
|
add_category(select)
|
|
|
|
print("-" * 20)
|