From 9f6293872afc626161c1241a92a14b7308503e2b Mon Sep 17 00:00:00 2001
From: Felix Breidenstein <felix@codemonauts.com>
Date: Sun, 18 Oct 2020 23:11:29 +0200
Subject: [PATCH] Fix sort

---
 autosort.py |  2 +-
 cli         | 12 +++++++-----
 report.py   |  2 +-
 sort.py     | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 validate.py |  1 -
 5 files changed, 58 insertions(+), 8 deletions(-)
 create mode 100644 sort.py

diff --git a/autosort.py b/autosort.py
index 24bff39..a3aaa94 100644
--- a/autosort.py
+++ b/autosort.py
@@ -53,7 +53,7 @@ def apply_rules(t, rules):
                 continue
 
         if desc_regex:
-            if not re.search(desc_regext.description):
+            if not re.search(desc_regex, t.description):
                 continue
 
         return r["tags"].split(",")
diff --git a/cli b/cli
index 18c5a79..db61649 100755
--- a/cli
+++ b/cli
@@ -8,8 +8,9 @@ import importer
 import serve
 import autosort
 import validate
-# import info
-# import report
+import info
+import sort
+#import report
 
 from helper import build_database_filename, create_dirs, build_rules_filename
 from sqlalchemy import create_engine
@@ -27,6 +28,8 @@ def cli():
 @cli.command(name="init")
 @click.argument("profile_name", required=True)
 def init(profile_name):
+    create_dirs()
+
     db_path = build_database_filename(profile_name)
     rule_path = build_rules_filename(profile_name)
 
@@ -44,12 +47,11 @@ def init(profile_name):
 
 
 if __name__ == '__main__':
-    create_dirs()
-    # cli.add_command(sort.command)
+    cli.add_command(sort.command)
     cli.add_command(importer.command)
     cli.add_command(serve.command)
     cli.add_command(autosort.command)
     cli.add_command(validate.command)
-    # cli.add_command(info.command)
+    cli.add_command(info.command)
     # cli.add_command(report.command)
     cli()
diff --git a/report.py b/report.py
index 415a321..36b6853 100644
--- a/report.py
+++ b/report.py
@@ -29,7 +29,7 @@ def command(profile, year):
         for sc in sub_categories:
             transactions = session.query(Transaction).filter(Transaction.category_id == sc.id).all()
             data[mc.name][sc.name] = format_amount(sum([x.amount for x in transactions]))
-    
+
     context = {"year": year, "data": data}
 
     tpl = template("views/report.tpl", context)
diff --git a/sort.py b/sort.py
new file mode 100644
index 0000000..835c6db
--- /dev/null
+++ b/sort.py
@@ -0,0 +1,49 @@
+#! /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)
diff --git a/validate.py b/validate.py
index 6f37935..26f931d 100644
--- a/validate.py
+++ b/validate.py
@@ -56,7 +56,6 @@ def command():
                         except:
                             print(cf.red(f"  Invalid IBAN: '{rule.get('iban')}'"))
 
-
                 print(cf.green(f"  All rules are valid"))
         else:
             print(cf.red(f"  The rule file doesn't exists"))