Sort only in leaf categories
This commit is contained in:
parent
820f2daba4
commit
51ade25a98
2 changed files with 18 additions and 12 deletions
11
helper.py
11
helper.py
|
@ -6,18 +6,23 @@ import re
|
||||||
from xdg import XDG_CONFIG_HOME, XDG_DATA_HOME
|
from xdg import XDG_CONFIG_HOME, XDG_DATA_HOME
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from models import Category, Transaction
|
from models import Category
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from yaml import CLoader as Loader, CDumper as Dumper
|
from yaml import CLoader as Loader
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from yaml import Loader, Dumper
|
from yaml import Loader
|
||||||
|
|
||||||
|
|
||||||
CONFIG_DIR = os.path.join(XDG_CONFIG_HOME, "schmeckels")
|
CONFIG_DIR = os.path.join(XDG_CONFIG_HOME, "schmeckels")
|
||||||
DATA_DIR = os.path.join(XDG_DATA_HOME, "schmeckels")
|
DATA_DIR = os.path.join(XDG_DATA_HOME, "schmeckels")
|
||||||
|
|
||||||
|
|
||||||
|
def get_list_of_bookable_categories(session):
|
||||||
|
categories = session.query(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:
|
||||||
|
|
19
sort.py
19
sort.py
|
@ -1,26 +1,21 @@
|
||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
from sqlalchemy import create_engine
|
from models import Transaction
|
||||||
from sqlalchemy.orm import sessionmaker
|
from helper import get_session, add_category, get_list_of_bookable_categories
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
|
||||||
from models import Category, Transaction
|
|
||||||
from helper import get_session, add_category
|
|
||||||
import sys
|
import sys
|
||||||
import click
|
import click
|
||||||
|
|
||||||
from prompt_toolkit.completion import FuzzyWordCompleter
|
from prompt_toolkit.completion import FuzzyWordCompleter
|
||||||
from prompt_toolkit.shortcuts import prompt
|
from prompt_toolkit.shortcuts import prompt
|
||||||
|
|
||||||
|
|
||||||
@click.command(name="sort")
|
@click.command(name="sort")
|
||||||
@click.option("--profile", "-p")
|
@click.option("--profile", "-p")
|
||||||
def command(profile):
|
def command(profile):
|
||||||
session = get_session(profile)
|
session = get_session(profile)
|
||||||
|
categories = get_list_of_bookable_categories(session)
|
||||||
categories = session.query(Category).all()
|
|
||||||
category_lookup = {c.full_name(): c.id for c in categories}
|
category_lookup = {c.full_name(): c.id for c in categories}
|
||||||
category_names = FuzzyWordCompleter([c.full_name() for c in categories])
|
category_names = FuzzyWordCompleter([c.full_name() for c in categories])
|
||||||
|
|
||||||
unsorted = session.query(Transaction).filter(Transaction.category_id == None).all()
|
unsorted = session.query(Transaction).filter(Transaction.category_id is None).all()
|
||||||
print("Found {} unsorted transcations".format(len(unsorted)))
|
print("Found {} unsorted transcations".format(len(unsorted)))
|
||||||
|
|
||||||
for t in unsorted:
|
for t in unsorted:
|
||||||
|
@ -32,10 +27,16 @@ def command(profile):
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Goodbye")
|
print("Goodbye")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
cat_id = category_lookup.get(select, None)
|
cat_id = category_lookup.get(select, None)
|
||||||
if not cat_id:
|
if not cat_id:
|
||||||
print(f"Creating new category '{select}'")
|
print(f"Creating new category '{select}'")
|
||||||
cat_id = add_category(select, profile)
|
cat_id = add_category(select, profile)
|
||||||
|
# Update the list of categories
|
||||||
|
categories = get_list_of_bookable_categories(session)
|
||||||
|
category_lookup = {c.full_name(): c.id for c in categories}
|
||||||
|
category_names = FuzzyWordCompleter([c.full_name() for c in categories])
|
||||||
|
|
||||||
t.category_id = cat_id
|
t.category_id = cat_id
|
||||||
session.add(t)
|
session.add(t)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
Loading…
Add table
Reference in a new issue