Update Interactive to new DB interface

Take the opportunity to improve the loop structure, so fix current
out-of-bound error.
This commit is contained in:
Luís Murta 2024-01-21 11:08:55 +00:00
parent 2a68ddd152
commit f966868736
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94

View File

@ -20,10 +20,8 @@ class Interactive:
def __init__(self, manager: Manager) -> None: def __init__(self, manager: Manager) -> None:
self.manager = manager self.manager = manager
with self.manager.db.session() as session: self.categories = self.manager.database.select(Category)
self.categories = session.get(Category) self.tags = self.manager.database.select(Tag)
self.tags = session.get(Tag)
session.expunge_all()
def intro(self) -> None: def intro(self) -> None:
print( print(
@ -34,28 +32,34 @@ class Interactive:
def start(self) -> None: def start(self) -> None:
self.intro() self.intro()
with self.manager.db.session() as session: with self.manager.database.session as session:
uncategorized = session.uncategorized() uncategorized = session.select(
Transaction, lambda: ~Transaction.category.has()
)
uncategorized.sort()
n = len(uncategorized) n = len(uncategorized)
print(f"{n} left to categorize") print(f"{n} left to categorize")
i = 0 i = 0
new = [] new = []
next = uncategorized[i]
print(next) while (command := input("$ ")) != "quit" and i < len(uncategorized):
while (command := input("$ ")) != "quit": current = uncategorized[i] if len(new) == 0 else new.pop()
print(current)
match command: match command:
case "help": case "help":
print(self.help) print(self.help)
case "skip": case "skip":
if len(uncategorized) == 0:
i += 1 i += 1
case "quit": case "quit":
break break
case "split": case "split":
new = self.split(next) new = self.split(current)
session.insert(new) session.insert(new)
case other: case other:
@ -66,33 +70,32 @@ class Interactive:
if other.startswith("note:"): if other.startswith("note:"):
# TODO adding notes to a splitted transaction won't allow # TODO adding notes to a splitted transaction won't allow
# categorization # categorization
next.note = Note(other[len("note:") :].strip()) current.note = Note(other[len("note:") :].strip())
else: else:
ct = other.split(":") ct = other.split(":")
if (category := ct[0]) not in [ if (category := ct[0]) not in [
c.name for c in self.categories c.name for c in self.categories
]: ]:
print(self.help, self.categories) print(self.help, self.categories)
continue
tags = [] tags = []
if len(ct) > 1: if len(ct) > 1:
tags = ct[1:] tags = ct[1:]
next.category = TransactionCategory(category, self.selector) current.category = TransactionCategory(
category, self.selector
)
for tag in tags: for tag in tags:
if tag not in [t.name for t in self.tags]: if tag not in [t.name for t in self.tags]:
session.insert([Tag(tag)]) session.insert([Tag(tag)])
self.tags = session.get(Tag) self.tags = session.get(Tag)
next.tags.add(TransactionTag(tag)) current.tags.add(TransactionTag(tag))
if len(new) == 0:
i += 1 i += 1
session.commit()
next = uncategorized[i] if len(new) == 0 else new.pop()
print(next)
def split(self, original: Transaction) -> list[SplitTransaction]: def split(self, original: Transaction) -> list[SplitTransaction]:
total = original.amount total = original.amount
new = [] new = []