diff --git a/worblehat/cli/main.py b/worblehat/cli/main.py index 288bffd..2c64871 100644 --- a/worblehat/cli/main.py +++ b/worblehat/cli/main.py @@ -18,6 +18,7 @@ from worblehat.services.argument_parser import parse_args from worblehat.models import * from .prompt_utils import * +from .subclis.advanced_options import AdvancedOptions from .subclis.bookcase_item import BookcaseItemCli from .subclis.bookcase_shelf_selector import select_bookcase_shelf @@ -88,82 +89,6 @@ class WorblehatCli(NumberedCmd): print(f' {item.name} - {item.amount} copies') - def do_add_bookcase(self, _: str): - while True: - name = input('Name of bookcase> ') - if name == '': - print('Error: name cannot be empty') - continue - - if self.sql_session.scalars( - select(Bookcase) - .where(Bookcase.name == name) - ).one_or_none() is not None: - print(f'Error: a bookcase with name {name} already exists') - continue - - break - - description = input('Description of bookcase> ') - if description == '': - description = None - - bookcase = Bookcase(name, description) - self.sql_session.add(bookcase) - self.sql_session.flush() - - - def do_add_bookcase_shelf(self, arg: str): - bookcase_selector = InteractiveItemSelector( - cls = Bookcase, - sql_session = self.sql_session, - ) - bookcase_selector.cmdloop() - bookcase = bookcase_selector.result - - while True: - column = input('Column> ') - try: - column = int(column) - except ValueError: - print('Error: column must be a number') - continue - break - - while True: - row = input('Row> ') - try: - row = int(row) - except ValueError: - print('Error: row must be a number') - continue - break - - if self.sql_session.scalars( - select(BookcaseShelf) - .where( - BookcaseShelf.bookcase == bookcase, - BookcaseShelf.column == column, - BookcaseShelf.row == row, - ) - ).one_or_none() is not None: - print(f'Error: a bookshelf in bookcase {bookcase.name} with position c{column}-r{row} already exists') - return - - description = input('Description> ') - if description == '': - description = None - - shelf = BookcaseShelf( - row, - column, - bookcase, - description, - ) - self.sql_session.add(shelf) - self.sql_session.flush() - - def _create_bookcase_item(self, isbn: str): bookcase_item = create_bookcase_item_from_isbn(isbn, self.sql_session) if bookcase_item is None: @@ -233,6 +158,11 @@ class WorblehatCli(NumberedCmd): def do_search(self, _: str): print('TODO: implement search') + + def do_advanced(self, _: str): + AdvancedOptions(self.sql_session).cmdloop() + + def do_save(self, _:str): if not self.sql_session_dirty: print('No changes to save.') @@ -274,21 +204,17 @@ class WorblehatCli(NumberedCmd): 'doc': 'Show a bookcase, and its items', }, 4: { - 'f': do_add_bookcase, - 'doc': 'Add a new bookcase', - }, - 5: { - 'f': do_add_bookcase_shelf, - 'doc': 'Add a new bookshelf', - }, - 6: { 'f': do_save, 'doc': 'Save changes', }, - 7: { + 5: { 'f': do_abort, 'doc': 'Abort changes', }, + 6: { + 'f': do_advanced, + 'doc': 'Advanced options', + }, 9: { 'f': do_exit, 'doc': 'Exit', diff --git a/worblehat/cli/subclis/advanced_options.py b/worblehat/cli/subclis/advanced_options.py new file mode 100644 index 0000000..12c30ec --- /dev/null +++ b/worblehat/cli/subclis/advanced_options.py @@ -0,0 +1,111 @@ +from sqlalchemy import select +from sqlalchemy.orm import Session + +from worblehat.cli.prompt_utils import ( + InteractiveItemSelector, + NumberedCmd, + format_date, + prompt_yes_no, +) +from worblehat.models import Bookcase, BookcaseShelf + +class AdvancedOptions(NumberedCmd): + def __init__(self, sql_session: Session): + super().__init__() + self.sql_session = sql_session + + + def do_add_bookcase(self, _: str): + while True: + name = input('Name of bookcase> ') + if name == '': + print('Error: name cannot be empty') + continue + + if self.sql_session.scalars( + select(Bookcase) + .where(Bookcase.name == name) + ).one_or_none() is not None: + print(f'Error: a bookcase with name {name} already exists') + continue + + break + + description = input('Description of bookcase> ') + if description == '': + description = None + + bookcase = Bookcase(name, description) + self.sql_session.add(bookcase) + self.sql_session.flush() + + + def do_add_bookcase_shelf(self, arg: str): + bookcase_selector = InteractiveItemSelector( + cls = Bookcase, + sql_session = self.sql_session, + ) + bookcase_selector.cmdloop() + bookcase = bookcase_selector.result + + while True: + column = input('Column> ') + try: + column = int(column) + except ValueError: + print('Error: column must be a number') + continue + break + + while True: + row = input('Row> ') + try: + row = int(row) + except ValueError: + print('Error: row must be a number') + continue + break + + if self.sql_session.scalars( + select(BookcaseShelf) + .where( + BookcaseShelf.bookcase == bookcase, + BookcaseShelf.column == column, + BookcaseShelf.row == row, + ) + ).one_or_none() is not None: + print(f'Error: a bookshelf in bookcase {bookcase.name} with position c{column}-r{row} already exists') + return + + description = input('Description> ') + if description == '': + description = None + + shelf = BookcaseShelf( + row, + column, + bookcase, + description, + ) + self.sql_session.add(shelf) + self.sql_session.flush() + + + def do_done(self, _: str): + return True + + + funcs = { + 1: { + 'f': do_add_bookcase, + 'doc': 'Add bookcase', + }, + 2: { + 'f': do_add_bookcase_shelf, + 'doc': 'Add bookcase shelf', + }, + 9: { + 'f': do_done, + 'doc': 'Done', + }, + } \ No newline at end of file diff --git a/worblehat/cli/subclis/bookcase_item.py b/worblehat/cli/subclis/bookcase_item.py index 7a3589e..d7de9d7 100644 --- a/worblehat/cli/subclis/bookcase_item.py +++ b/worblehat/cli/subclis/bookcase_item.py @@ -211,7 +211,7 @@ class EditBookcaseCli(NumberedCmd): 'f': do_shelf, 'doc': 'Change shelf', }, - 7: { + 9: { 'f': do_done, 'doc': 'Done', },