Fixed some bugs in import
This commit is contained in:
parent
dbde36a4cb
commit
977f82a2a4
4 changed files with 39 additions and 18 deletions
25
helper.py
25
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
|
||||
|
|
14
importer.py
14
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]
|
||||
|
||||
|
|
14
sort.py
14
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)
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
<th class="px-4 py-2">Sender/Empfänger</th>
|
||||
<th class="px-4 py-2">Verwendungszweck</th>
|
||||
<th class="px-4 py-2">Betrag</th>
|
||||
<th class="px-4 py-2">Kategorie</th>
|
||||
<th class="px-4 py-2"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
@ -19,7 +19,7 @@
|
|||
<td class="border px-4 py-2">{{ t.name }}</td>
|
||||
<td class="border px-4 py-2">{{ t.description }}</td>
|
||||
<td class="border px-4 py-2 {{ 'text-green-500' if t.is_positive() else 'text-red-500' }}">{{ t.pretty_amount() }}</td>
|
||||
<td class="border px-4 py-2">{{ t.category.full_name() if t.category else "-" }}</td>
|
||||
<td class="border px-4 py-2"><a href="#">Remove category</a></td>
|
||||
</tr>
|
||||
% end
|
||||
</tbody>
|
||||
|
|
Loading…
Add table
Reference in a new issue