From 977f82a2a4cef4cebb4bfac362f7d0efc166fe7c Mon Sep 17 00:00:00 2001 From: fleaz Date: Thu, 19 Mar 2020 00:12:18 +0100 Subject: [PATCH] Fixed some bugs in import --- helper.py | 25 ++++++++++++++++++++++++- importer.py | 14 +++++++------- sort.py | 14 ++++++-------- views/category.tpl | 4 ++-- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/helper.py b/helper.py index 8defd4e..bb076e9 100644 --- a/helper.py +++ b/helper.py @@ -4,6 +4,7 @@ import sys from xdg import XDG_CONFIG_HOME, XDG_DATA_HOME from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker +from models import Category, Transaction CONFIG_DIR = os.path.join(XDG_CONFIG_HOME, "schmeckels") DATA_DIR = os.path.join(XDG_DATA_HOME, "schmeckels") @@ -32,7 +33,6 @@ def get_session(profile_name): else: filename = build_database_filename(profile_name) - print(filename) if os.path.exists(filename) and os.path.isfile(filename): engine = create_engine(f"sqlite:///{filename}") Session = sessionmaker(bind=engine) @@ -40,3 +40,26 @@ def get_session(profile_name): else: print(f"No database for profile '{profile_name}'. Did you run 'init'?") sys.exit(1) + + +def create_category(name, parent, profile): + session = get_session(profile) + c = session.query(Category).filter(Category.name == name).first() + if not c: + c = Category(name=name, parent_id=parent) + session.add(c) + session.commit() + return c.id + + +def add_category(name, profile): + parts = name.split(":") + if len(parts) == 1: + return create_category(name, None, profile) + else: + for i in range(len(parts) - 1): + parent = parts[i] + child = parts[i + 1] + parent_id = create_category(parent, None, profile) + id = create_category(child, parent_id, profile) + return id diff --git a/importer.py b/importer.py index 168050c..cbf0133 100644 --- a/importer.py +++ b/importer.py @@ -13,9 +13,9 @@ import sys @click.command(name="import") @click.option("--filetype", "-t", type=click.Choice(["dkb", "sparkasse-mt940", "bunq-csv"], case_sensitive=False)) @click.option("--profile", "-p") -@click.option("--force", default=False) +@click.option("--force","-f", default=False, is_flag=True) @click.argument("filename", type=click.Path(exists=True)) -def command(filename, profile, filetype): +def command(filename, profile, force, filetype): session = get_session(profile) latest = session.query(Transaction).order_by(desc(Transaction.date)).first() @@ -57,13 +57,13 @@ def command(filename, profile, filetype): print(".", end="", flush=True) elif filetype == "dkb": - with open(click.format_filename(filename), "r", encoding="utf8") as fh: - fh.readline() # Get rid of first line + with open(click.format_filename(filename), encoding='ISO-8859-1') as fh: csv_reader = csv.reader(fh, delimiter=";") + next(csv_reader, None) for line in csv_reader: - date = datetime.strptime(line[0], "%Y-%m-%d") - amount = line[5] - iban = line[4] + date = datetime.strptime(line[0], "%d.%m.%Y") + amount = int(line[7].replace(".","").replace(",", "")) + iban = line[5] name = line[3] description = line[4] diff --git a/sort.py b/sort.py index 54bfbae..0640f25 100644 --- a/sort.py +++ b/sort.py @@ -3,8 +3,7 @@ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from models import Category, Transaction -from categories import add_category -from helper import get_session +from helper import get_session, add_category import sys import click @@ -34,12 +33,11 @@ def command(profile): 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: + if not cat_id: print(f"Creating new category '{select}'") - add_category(select) + cat_id = add_category(select, profile) + t.category_id = cat_id + session.add(t) + session.commit() print("-" * 20) diff --git a/views/category.tpl b/views/category.tpl index 971a209..df8b680 100644 --- a/views/category.tpl +++ b/views/category.tpl @@ -9,7 +9,7 @@ Sender/Empfänger Verwendungszweck Betrag - Kategorie + @@ -19,7 +19,7 @@ {{ t.name }} {{ t.description }} {{ t.pretty_amount() }} - {{ t.category.full_name() if t.category else "-" }} + Remove category % end