This commit is contained in:
Felix Breidenstein 2020-10-18 18:05:05 +02:00
parent 78a0d1a1f7
commit e49e8c51f5
5 changed files with 23 additions and 40 deletions

View file

@ -2,16 +2,12 @@
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from models import Category, Transaction from models import Category, Tag
from helper import get_session, add_category, get_rules from helper import get_session, add_category, get_rules
import sys import sys
import click import click
import colorful as cf import colorful as cf
from prompt_toolkit.completion import FuzzyWordCompleter
from prompt_toolkit.shortcuts import prompt
def print_verbose(verbose, string): def print_verbose(verbose, string):
if verbose: if verbose:
print(string) print(string)
@ -25,7 +21,7 @@ def command(profile, dry_run, verbose):
session = get_session(profile) session = get_session(profile)
rules = get_rules(profile) rules = get_rules(profile)
unsorted = session.query(Transaction).filter(Transaction.category_id == None).all() unsorted = session.query(Transaction).filter(Transaction.tags == None).all()
print("Found {} unsorted transcations".format(len(unsorted))) print("Found {} unsorted transcations".format(len(unsorted)))
new = [] new = []

18
cli
View file

@ -3,9 +3,9 @@ import click
import os import os
import sys import sys
#import importer import importer
#import serve # import serve
import autosort # import autosort
# import validate # import validate
# import info # import info
# import report # import report
@ -38,11 +38,11 @@ def init(profile_name):
if __name__ == '__main__': if __name__ == '__main__':
create_dirs() create_dirs()
cli.add_command(sort.command) # cli.add_command(sort.command)
cli.add_command(importer.command) cli.add_command(importer.command)
cli.add_command(serve.command) # cli.add_command(serve.command)
cli.add_command(autosort.command) # cli.add_command(autosort.command)
cli.add_command(validate.command) # cli.add_command(validate.command)
cli.add_command(info.command) # cli.add_command(info.command)
cli.add_command(report.command) # cli.add_command(report.command)
cli() cli()

View file

@ -24,11 +24,6 @@ def format_amount(cents):
return f"{amount:,.2f}" return f"{amount:,.2f}"
def get_list_of_bookable_categories(session):
categories = session.query(models.Category).all()
return [c for c in categories if c.is_child()]
def create_dirs(): def create_dirs():
for directory in [CONFIG_DIR, DATA_DIR]: for directory in [CONFIG_DIR, DATA_DIR]:
try: try:
@ -90,23 +85,10 @@ def get_rules(profile_name):
sys.exit(1) sys.exit(1)
def create_category(name, parent, profile, session): def create_tag(name, profile, session):
c = session.query(models.Category).filter(models.Category.name == name).first() t = session.query(models.Tag).filter(models.Tag.name == name).first()
if not c: if not t:
c = models.Category(name=name, parent_id=parent) c = models.Tag(name=name)
session.add(c) session.add(t)
session.commit() session.commit()
return c.id return t.id
def add_category(name, profile, session):
parts = name.split(":")
if len(parts) == 1:
return create_category(name, None, profile, session)
else:
for i in range(len(parts) - 1):
parent = parts[i]
child = parts[i + 1]
parent_id = create_category(parent, None, profile, session)
id = create_category(child, parent_id, profile, session)
return id

View file

@ -2,7 +2,6 @@
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.orm import relationship, backref from sqlalchemy.orm import relationship, backref
from helper import format_amount
Base = declarative_base() Base = declarative_base()

View file

@ -33,24 +33,30 @@ def command():
print(cf.green(" Ruleset is empty")) print(cf.green(" Ruleset is empty"))
else: else:
for rule in data: for rule in data:
# TODO: validate tag list
# validate name regex
if rule.get("name"): if rule.get("name"):
try: try:
re.compile(rule["name"]) re.compile(rule["name"])
except: except:
print(cf.red(f" Invalid name regex: '{rule.get('name')}'")) print(cf.red(f" Invalid name regex: '{rule.get('name')}'"))
# validate description regex
if rule.get("description"): if rule.get("description"):
try: try:
re.compile(rule["description"]) re.compile(rule["description"])
except: except:
print(cf.red(f" Invalid description regex: '{rule.get('description')}'")) print(cf.red(f" Invalid description regex: '{rule.get('description')}'"))
# validate IBAN
if rule.get("iban"): if rule.get("iban"):
try: try:
IBAN(rule.get("iban")) IBAN(rule.get("iban"))
except: except:
print(cf.red(f" Invalid IBAN: '{rule.get('iban')}'")) print(cf.red(f" Invalid IBAN: '{rule.get('iban')}'"))
print(cf.green(f" All rules are valid")) print(cf.green(f" All rules are valid"))
else: else:
print(cf.red(f" The rule file doesn't exists")) print(cf.red(f" The rule file doesn't exists"))