Changeset - f5ed6b020878
[Not reviewed]
default
0 1 0
Dennis Fink - 3 years ago 2022-03-21 20:55:40
dennis.fink@c3l.lu
Don't fail if the book has not isbn
1 file changed with 4 insertions and 0 deletions:
0 comments (0 inline, 0 general)
stockcli/books.py
Show inline comments
 
@@ -7,48 +7,52 @@ from rich.table import Table
 
from . import utils
 
from .console import DEFAULT_PADDING, console, error_console, int_prompt, prompt
 
from .style import GreenBoldText
 

	
 

	
 
def add_book_by_barcode(barcode: str) -> None:
 

	
 
    canonical_isbn = isbnlib.canonical(barcode)
 
    if not (isbnlib.is_isbn10(canonical_isbn) or isbnlib.is_isbn13(canonical_isbn)):
 
        logging.error(f"{barcode} is not a valid ISBN!")
 
        error_console.print(f"{barcode} is not a valid ISBN!")
 
        return
 

	
 
    userentity = utils.get_request("objects/userentities?query[]=name=books")
 
    userentity_id = userentity[0]["id"]
 

	
 
    all_books = utils.get_request(
 
        f"objects/userobjects?query[]=userentity_id={userentity_id}"
 
    )
 

	
 
    for book in all_books:
 
        book_metadata = utils.get_request(
 
            f"userfields/userentity-books/{book['id']}", cached=True
 
        )
 

	
 
        if book_metadata["isbn"] is None:
 
            continue
 

	
 
        if isbnlib.canonical(book_metadata["isbn"]) == canonical_isbn:
 

	
 
            book_metadata = utils.get_request(
 
                f"userfields/userentity-books/{book['id']}", cached=False
 
            )
 

	
 
            grid = Table.grid(padding=DEFAULT_PADDING)
 
            grid.add_column(justify="right", no_wrap=True)
 
            grid.add_column(justify="left", style="cyan", no_wrap=True)
 
            grid.add_row(GreenBoldText("Title:"), book_metadata["title"])
 
            grid.add_row(GreenBoldText("Amount:"), book_metadata["amount"])
 
            console.print(
 
                Panel(grid, title="[green bold]Book already found[/green bold]")
 
            )
 

	
 
            add_to_amount = bool(
 
                int_prompt.ask("Add? (Enter 0 to abort)", choices=["0", "1"], default=0)
 
            )
 
            if not add_to_amount:
 
                logging.debug("User aborted task!")
 
                return
 
            else:
 
                book_metadata["amount"] = str(int(book_metadata["amount"]) + 1)
 
                response = utils.put_request(
0 comments (0 inline, 0 general)